001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.jaxws.builder;
017:
018: import java.io.File;
019: import java.io.OutputStream;
020: import java.lang.reflect.InvocationTargetException;
021: import java.net.JarURLConnection;
022: import java.net.URI;
023: import java.net.URL;
024: import java.util.ArrayList;
025:
026: import org.apache.geronimo.kernel.repository.ListableRepository;
027: import org.apache.geronimo.kernel.repository.Maven2Repository;
028:
029: public class JAXWSToolsCLI {
030:
031: enum Command {
032: WSGEN, WSIMPORT
033: };
034:
035: private static final String USAGE_MSG = "Usage: jaxws-tools <toolName> <tool options>\n\n"
036: + "where <toolName> is:\n"
037: + " wsgen - generate portable artifacts from class\n"
038: + " wsimport - generate portable artifacts from WSDL\n";
039:
040: public static void main(String[] args) throws Throwable {
041: if (args.length == 0) {
042: System.err.println(USAGE_MSG);
043: System.exit(1);
044: }
045:
046: Command cmd = null;
047: if (args[0].equalsIgnoreCase("wsgen")) {
048: cmd = Command.WSGEN;
049: } else if (args[0].equalsIgnoreCase("wsimport")) {
050: cmd = Command.WSIMPORT;
051: } else {
052: System.err.println("Error: Unsupported toolName ["
053: + args[0] + "].");
054: System.err.println();
055: System.err.println(USAGE_MSG);
056: System.exit(1);
057: }
058:
059: String geroninoHome = getGeronimoHome();
060: String[] arguments = getCmdArguments(args);
061: boolean rs = run(cmd, geroninoHome, arguments, System.out);
062: System.exit((rs) ? 0 : 1);
063: }
064:
065: static boolean run(Command cmd, String geronimoHome, String[] args,
066: OutputStream out) throws Exception {
067: String repository = System.getProperty(
068: "Xorg.apache.geronimo.repository.boot.path",
069: "repository");
070: Maven2Repository mavenRepository = new Maven2Repository(
071: (new File(geronimoHome, repository)).getCanonicalFile());
072: ArrayList<ListableRepository> repositories = new ArrayList<ListableRepository>(
073: 1);
074: repositories.add(mavenRepository);
075:
076: JAXWSTools tools = new JAXWSTools();
077: tools.setUseSunSAAJ();
078: tools.setOverrideContextClassLoader(true);
079:
080: File[] jars = tools.getClasspath(repositories);
081: URL[] jarUrls = JAXWSTools.toURL(jars);
082:
083: String javaClassPath = System.getProperty("java.class.path");
084: System
085: .setProperty("java.class.path", JAXWSTools
086: .toString(jars));
087:
088: try {
089: if (cmd.equals(Command.WSGEN)) {
090: return tools.invokeWsgen(jarUrls, out, args);
091: } else if (cmd.equals(Command.WSIMPORT)) {
092: return tools.invokeWsimport(jarUrls, out, args);
093: } else {
094: throw new IllegalArgumentException("Invalid command: "
095: + cmd);
096: }
097: } catch (InvocationTargetException e) {
098: Throwable exception = e.getTargetException();
099: if (exception instanceof Exception) {
100: throw (Exception) exception;
101: } else {
102: throw e;
103: }
104: } finally {
105: System.setProperty("java.class.path", javaClassPath);
106: }
107: }
108:
109: private static String[] getCmdArguments(String[] args) {
110: String[] cmdArgs = new String[args.length - 1];
111: System.arraycopy(args, 1, cmdArgs, 0, args.length - 1);
112: return cmdArgs;
113: }
114:
115: private static String getGeronimoHome() {
116: String geronimoHome = System
117: .getProperty("org.apache.geronimo.home.dir");
118: if (geronimoHome != null) {
119: return geronimoHome;
120: }
121:
122: // guess from the location of the jar
123: URL url = JAXWSToolsCLI.class.getClassLoader().getResource(
124: "META-INF/startup-jar");
125: if (url != null) {
126: try {
127: JarURLConnection jarConnection = (JarURLConnection) url
128: .openConnection();
129: url = jarConnection.getJarFileURL();
130:
131: URI baseURI = new URI(url.toString()).resolve("..");
132: File dir = new File(baseURI);
133: return dir.getAbsolutePath();
134: } catch (Exception ignored) {
135: // ignore
136: }
137: }
138:
139: // cannot determine the directory, return parent directory
140: return "..";
141: }
142:
143: }
|