/*=============================================================================*
* Copyright 2006 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*=============================================================================*/
package org.apache.muse.tools.generator.util;
import java.io.File;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.muse.core.Environment;
import org.apache.muse.tools.generator.Wsdl2JavaConstants;
import org.apache.muse.util.CommandLine;
import org.apache.muse.util.messages.Messages;
import org.apache.muse.util.messages.MessagesFactory;
import org.apache.muse.ws.wsdl.WsdlUtils;
import org.w3c.dom.Document;
/**
* Assorted utility methods that are shared by the command line apps (wsdl2java and wsdlmerge).
* Logging is handled here for the org.apache.muse.tools.generator package using
* java.util.logging classes.
*
*
* @author Andrew Eberbach (aeberbac)
*/
public class AbstractCommandLineApp {
public static final String TOP_LEVEL_PACKAGE = "org.apache.muse.tools";
private static Messages _MESSAGES = MessagesFactory.get(AbstractCommandLineApp.class);
private static Logger _logger = Logger.getLogger(AbstractCommandLineApp.class.getPackage().getName());
/**
* Tell the logger to info() this message.
*
* @param message The message to log.
*/
public static void handleMessage(String message) {
_logger.info(message);
}
/**
* Something bad happened, log the message and exit
* with status 1. Log this as severe().
*
* @param message The error message
*/
public static void handleErrorAndExit(String message) {
_logger.severe(message);
System.exit(1);
}
/**
* Something bad happened, log the message and
* associated exception. Log this as severe().
*
* @param message
* @param exception
*/
public static void handleErrorAndExit(String message, Exception exception) {
_logger.log(Level.SEVERE, message, exception);
System.exit(1);
}
/**
* For cleanliness wrap System.exit(0) exiting
* normally.
*
*/
public static void handleExit() {
System.exit(0);
}
/**
* Check an exception to see if it has a message and
* if it has one then make it into the standard
* Messages filler object and return it.
*
* @param e
* The exception we're interest in
* @return
* A filler for Messages with either an empty string or the
* message from the exception
*
* @see Messages
*/
public static Object[] getFiller(Exception e) {
String message = e.getMessage();
Object[] filler = new Object[] {
message == null?new String():message
};
return filler;
}
/**
* Tries to parse the WSDL Document from the given file. Throws an IllegalArgumentException
* if the file is null or doesn't exist.
*
* @param wsdlFile The WSDL File to parse
* @param descriptorFile
* @return A Document representing the parsed WSDL
*/
public static Document[] getWSDLDocuments(File wsdlFile, File deploymentDescriptorFile, Document deploymentDescriptorDocument) throws Exception {
if(wsdlFile != null) {
return new Document[] { getWSDLDocument(wsdlFile) };
}
if(deploymentDescriptorFile == null && wsdlFile == null) {
throw new NullPointerException(_MESSAGES.get("NullWSDLBuiltinDescriptor"));
}
File[] wsdlFiles = DeploymentDescriptorHelper.getWsdls(deploymentDescriptorFile, deploymentDescriptorDocument);
Document[] wsdlDocuments = new Document[wsdlFiles.length];
for(int i=0; i < wsdlFiles.length; i++) {
wsdlDocuments[i] = getWSDLDocument(wsdlFiles[i]);
}
return wsdlDocuments;
}
/**
* Given a <code>File</code> try to load it into a <code>Definition</code>
* throwing an Exception if something is amiss.
*
* @param wsdlFile
* The WSDL file we're parsing
*
* @return
* A <code>Definition</code> of the parsed WSDL
*
* @throws Exception
*/
public static Document getWSDLDocument(File wsdlFile) throws Exception {
if (wsdlFile == null) {
throw new IllegalArgumentException(_MESSAGES.get("NullWSDL"));
}
if(!wsdlFile.exists()) {
throw new IllegalArgumentException(_MESSAGES.get("NonExistantWSDL"));
}
File parent = new File(wsdlFile.getAbsoluteFile().getParent());
Environment environment = new LocalEnvironment(parent.getAbsoluteFile());
try {
Document wsdl = WsdlUtils.createWSDL(
environment,
wsdlFile.getName(),
true);
WsdlUtils.removeSchemaReferences(wsdl.getDocumentElement());
return wsdl;
} catch (Exception e) {
Object[] filler = getFiller(e);
throw new RuntimeException(_MESSAGES.get("FailedLoadingWSDL", filler), e);
}
}
/**
* Check to see if the given arguments object has no arguments in it.
* This is true if the number of arguments (read: number of arguments that
* are not flags and are not associated with flags) plus the number
* of flags is 0.
*
* @param arguments
* The arguments we're checking
* @return
* true if there are no arguments in the object
*/
public static boolean hasNoArguments(CommandLine arguments) {
return (arguments.getNumberOfArguments() + arguments.getNumberOfFlags()) == 0;
}
/**
* Set up the java.util.Logger that this class (and the descendants
* in the Logger heirarchy) will use. Handles command line parameters
* for increasing verbosity or for turning off logging entirely.
*
* @param arguments Command line arguments
*/
protected static void createLogger(CommandLine arguments) {
ConsoleHandler handler = new ConsoleHandler();
handler.setFormatter(new SimpleLogFormatter());
_logger = Logger.getLogger(TOP_LEVEL_PACKAGE);
_logger.setLevel(Level.INFO);
_logger.setUseParentHandlers(false);
_logger.addHandler(handler);
if(arguments.hasFlag(Wsdl2JavaConstants.VERBOSE_FLAG)) {
_logger.setLevel(Level.ALL);
} else if (arguments.hasFlag(Wsdl2JavaConstants.QUIET_FLAG)) {
_logger.setLevel(Level.OFF);
} else {
_logger.setLevel(Level.FINE);
}
}
/**
* Check to see if the overwrite flag was specified. This
* is just a call to see if the arguments contain the flag.
*
* @param arguments Command line arguments
* @return true if the arguments contain the flag, false otherwise
*/
protected static boolean checkOverwriteArg(CommandLine arguments) {
return arguments.hasFlag(Wsdl2JavaConstants.OVERWRITE_FLAG);
}
}