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.Method;
021: import java.net.MalformedURLException;
022: import java.net.URL;
023: import java.net.URLClassLoader;
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.SortedSet;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.geronimo.kernel.repository.Artifact;
031: import org.apache.geronimo.kernel.repository.ListableRepository;
032: import org.apache.geronimo.kernel.repository.Repository;
033: import org.apache.geronimo.kernel.repository.Version;
034:
035: public class JAXWSTools {
036:
037: private static final Log LOG = LogFactory.getLog(JAXWSTools.class);
038:
039: private final static String[][] LIBS = {
040: { "org.apache.axis2", "axis2-jaxws-api" },
041: { "org.apache.geronimo.specs", "geronimo-saaj_1.3_spec" },
042: { "javax.xml.bind", "jaxb-api" },
043: { "com.sun.xml.bind", "jaxb-impl" },
044: { "com.sun.xml.bind", "jaxb-xjc" },
045: { "com.sun.xml.ws", "jaxws-tools" },
046: { "com.sun.xml.ws", "jaxws-rt" },
047: { "org.apache.geronimo.javamail",
048: "geronimo-javamail_1.4_mail" },
049: { "org.apache.geronimo.specs",
050: "geronimo-activation_1.1_spec" },
051: { "org.apache.geronimo.specs",
052: "geronimo-annotation_1.0_spec" },
053: { "org.apache.geronimo.specs",
054: "geronimo-ws-metadata_2.0_spec" },
055: { "org.apache.geronimo.specs", "geronimo-ejb_3.0_spec" },
056: { "org.apache.geronimo.specs",
057: "geronimo-interceptor_3.0_spec" },
058: { "org.apache.geronimo.specs", "geronimo-stax-api_1.0_spec" },
059: { "org.apache.geronimo.specs", "geronimo-jpa_3.0_spec" },
060: { "org.apache.geronimo.specs",
061: "geronimo-j2ee-connector_1.5_spec" },
062: { "org.apache.geronimo.specs", "geronimo-jms_1.1_spec" },
063: { "org.apache.geronimo.specs", "geronimo-jta_1.1_spec" },
064: { "org.apache.geronimo.specs",
065: "geronimo-j2ee-management_1.1_spec" }, };
066:
067: private final static Artifact SUN_SAAJ_IMPL_ARTIFACT = new Artifact(
068: "com.sun.xml.messaging.saaj", "saaj-impl", (Version) null,
069: "jar");
070: private final static Artifact AXIS2_SAAJ_IMPL_ARTIFACT = new Artifact(
071: "org.apache.axis2", "axis2-saaj", (Version) null, "jar");
072: private final static String TOOLS = "tools.jar";
073:
074: private Artifact saajImpl;
075: private boolean overrideContextClassLoader;
076:
077: public JAXWSTools() {
078: }
079:
080: public void setUseSunSAAJ() {
081: this .saajImpl = SUN_SAAJ_IMPL_ARTIFACT;
082: }
083:
084: public void setUseAxis2SAAJ() {
085: this .saajImpl = AXIS2_SAAJ_IMPL_ARTIFACT;
086: }
087:
088: public void setOverrideContextClassLoader(
089: boolean overrideContextClassLoader) {
090: this .overrideContextClassLoader = overrideContextClassLoader;
091: }
092:
093: public boolean getOverrideContextClassLoader() {
094: return this .overrideContextClassLoader;
095: }
096:
097: public static URL[] toURL(File[] jars) throws MalformedURLException {
098: URL[] urls = new URL[jars.length];
099: for (int i = 0; i < jars.length; i++) {
100: urls[i] = jars[i].toURL();
101: }
102: return urls;
103: }
104:
105: public static String toString(File[] jars) {
106: StringBuffer buf = new StringBuffer();
107: for (int i = 0; i < jars.length; i++) {
108: buf.append(jars[i].getAbsolutePath());
109: if (i + 1 < jars.length) {
110: buf.append(File.pathSeparatorChar);
111: }
112: }
113: return buf.toString();
114: }
115:
116: public File[] getClasspath(
117: Collection<? extends Repository> repositories)
118: throws Exception {
119: ArrayList<File> jars = new ArrayList<File>();
120: for (String[] lib : LIBS) {
121: Artifact artifact = new Artifact(lib[0], lib[1],
122: (Version) null, "jar");
123: jars.add(getLocation(repositories, artifact));
124: }
125: if (this .saajImpl != null) {
126: jars.add(getLocation(repositories, this .saajImpl));
127: }
128: addToolsJarLocation(jars);
129:
130: return jars.toArray(new File[jars.size()]);
131: }
132:
133: private static File getLocation(
134: Collection<? extends Repository> repositories,
135: Artifact artifactQuery) throws Exception {
136: File file = null;
137:
138: for (Repository arepository : repositories) {
139: if (arepository instanceof ListableRepository) {
140: ListableRepository repository = (ListableRepository) arepository;
141: SortedSet artifactSet = repository.list(artifactQuery);
142: // if we have exactly one artifact found
143: if (artifactSet.size() == 1) {
144: file = repository
145: .getLocation((Artifact) artifactSet.first());
146: return file.getAbsoluteFile();
147: } else if (artifactSet.size() > 1) {// if we have more than 1 artifacts found use the latest one.
148: file = repository
149: .getLocation((Artifact) artifactSet.last());
150: return file.getAbsoluteFile();
151: }
152: }
153: }
154:
155: throw new Exception("Missing artifact in repositories: "
156: + artifactQuery.toString());
157: }
158:
159: private static void addToolsJarLocation(ArrayList<File> jars) {
160: //create a new File then check exists()
161: String jreHomePath = System.getProperty("java.home");
162: String javaHomePath = "";
163: int jreHomePathLength = jreHomePath.length();
164: if (jreHomePathLength > 0) {
165: int i = jreHomePath.substring(0, jreHomePathLength - 1)
166: .lastIndexOf(java.io.File.separator);
167: javaHomePath = jreHomePath.substring(0, i);
168: }
169: File jdkhomelib = new File(javaHomePath, "lib");
170: if (!jdkhomelib.exists()) {
171: LOG.warn("Missing " + jdkhomelib.getAbsolutePath()
172: + ". This may be required for wsgen to run. ");
173: } else {
174: File tools = new File(jdkhomelib, TOOLS);
175: if (!tools.exists()) {
176: LOG.warn("Missing tools.jar in"
177: + jdkhomelib.getAbsolutePath()
178: + ". This may be required for wsgen to run. ");
179: } else {
180: jars.add(tools.getAbsoluteFile());
181: }
182: }
183: }
184:
185: public boolean invokeWsgen(URL[] jars, OutputStream os,
186: String[] arguments) throws Exception {
187: return invoke("wsgen", jars, os, arguments);
188:
189: }
190:
191: public boolean invokeWsimport(URL[] jars, OutputStream os,
192: String[] arguments) throws Exception {
193: return invoke("wsimport", jars, os, arguments);
194: }
195:
196: private boolean invoke(String toolName, URL[] jars,
197: OutputStream os, String[] arguments) throws Exception {
198: URLClassLoader loader = new URLClassLoader(jars, ClassLoader
199: .getSystemClassLoader());
200: if (this .overrideContextClassLoader) {
201: ClassLoader oldClassLoader = Thread.currentThread()
202: .getContextClassLoader();
203: Thread.currentThread().setContextClassLoader(loader);
204: try {
205: return invoke(toolName, loader, os, arguments);
206: } finally {
207: Thread.currentThread().setContextClassLoader(
208: oldClassLoader);
209: }
210: } else {
211: return invoke(toolName, loader, os, arguments);
212: }
213: }
214:
215: private boolean invoke(String toolName, ClassLoader loader,
216: OutputStream os, String[] arguments) throws Exception {
217: LOG.debug("Invoking " + toolName);
218: Class clazz = loader
219: .loadClass("com.sun.tools.ws.spi.WSToolsObjectFactory");
220: Method method = clazz.getMethod("newInstance");
221: Object factory = method.invoke(null);
222: Method method2 = clazz.getMethod(toolName, OutputStream.class,
223: String[].class);
224:
225: Boolean result = (Boolean) method2.invoke(factory, os,
226: arguments);
227:
228: return result;
229: }
230:
231: }
|