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 */
017
018 package org.apache.geronimo.jaxws.builder;
019
020 import java.io.File;
021 import java.io.OutputStream;
022 import java.lang.reflect.InvocationTargetException;
023 import java.net.JarURLConnection;
024 import java.net.URI;
025 import java.net.URL;
026 import java.util.ArrayList;
027
028 import org.apache.geronimo.kernel.repository.ListableRepository;
029 import org.apache.geronimo.kernel.repository.Maven2Repository;
030
031 public class JAXWSToolsCLI {
032
033 enum Command { WSGEN, WSIMPORT };
034
035 private static final String USAGE_MSG =
036 "Usage: jaxws-tools <toolName> <tool options>\n\n" +
037 "where <toolName> is:\n" +
038 " wsgen - generate portable artifacts from class\n" +
039 " wsimport - generate portable artifacts from WSDL\n";
040
041 public static void main(String[] args) throws Throwable {
042 if (args.length == 0) {
043 System.err.println(USAGE_MSG);
044 System.exit(1);
045 }
046
047 Command cmd = null;
048 if (args[0].equalsIgnoreCase("wsgen")) {
049 cmd = Command.WSGEN;
050 } else if (args[0].equalsIgnoreCase("wsimport")) {
051 cmd = Command.WSIMPORT;
052 } else {
053 System.err.println("Error: Unsupported toolName [" + 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, OutputStream out) throws Exception {
066 String repository = System.getProperty("Xorg.apache.geronimo.repository.boot.path", "repository");
067 Maven2Repository mavenRepository = new Maven2Repository((new File(geronimoHome, repository)).getCanonicalFile());
068 ArrayList<ListableRepository> repositories = new ArrayList<ListableRepository>(1);
069 repositories.add(mavenRepository);
070
071 JAXWSTools tools = new JAXWSTools();
072 tools.setUseSunSAAJ();
073 tools.setOverrideContextClassLoader(true);
074
075 File [] jars;
076 try {
077 jars = tools.getClasspath(repositories);
078 } catch (Exception e) {
079 //if we cannot find SUN's SAAJ impl, try Axis2's.
080 tools.setUseAxis2SAAJ();
081 jars = tools.getClasspath(repositories);
082 }
083
084 URL[] jarUrls = JAXWSTools.toURL(jars);
085
086 String javaClassPath = System.getProperty("java.class.path");
087 System.setProperty("java.class.path", JAXWSTools.toString(jars));
088
089 try {
090 if (cmd.equals(Command.WSGEN)) {
091 return tools.invokeWsgen(jarUrls, out, args);
092 } else if (cmd.equals(Command.WSIMPORT)) {
093 return tools.invokeWsimport(jarUrls, out, args);
094 } else {
095 throw new IllegalArgumentException("Invalid command: " + 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.getProperty("org.apache.geronimo.home.dir");
117 if (geronimoHome != null) {
118 return geronimoHome;
119 }
120
121 // guess from the location of the jar
122 URL url = JAXWSToolsCLI.class.getClassLoader().getResource("META-INF/startup-jar");
123 if (url != null) {
124 try {
125 JarURLConnection jarConnection = (JarURLConnection) url.openConnection();
126 url = jarConnection.getJarFileURL();
127
128 URI baseURI = new URI(url.toString()).resolve("..");
129 File dir = new File(baseURI);
130 return dir.getAbsolutePath();
131 } catch (Exception ignored) {
132 // ignore
133 }
134 }
135
136 // cannot determine the directory, return parent directory
137 return "..";
138 }
139
140 }