001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.deployment;
020:
021: import org.apache.axis2.context.ConfigurationContext;
022: import org.apache.axis2.deployment.repository.util.DeploymentFileData;
023: import org.apache.axis2.deployment.util.Utils;
024: import org.apache.axis2.description.AxisOperation;
025: import org.apache.axis2.description.AxisService;
026: import org.apache.axis2.description.AxisServiceGroup;
027: import org.apache.axis2.description.WSDL2Constants;
028: import org.apache.axis2.description.java2wsdl.AnnotationConstants;
029: import org.apache.axis2.engine.MessageReceiver;
030: import org.apache.axis2.util.Loader;
031: import org.apache.axis2.wsdl.WSDLConstants;
032: import org.apache.axis2.Constants;
033: import org.apache.axis2.AxisFault;
034: import org.apache.axis2.i18n.Messages;
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037: import org.codehaus.jam.JAnnotation;
038: import org.codehaus.jam.JClass;
039: import org.codehaus.jam.JamClassIterator;
040: import org.codehaus.jam.JamService;
041: import org.codehaus.jam.JamServiceFactory;
042: import org.codehaus.jam.JamServiceParams;
043:
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.io.PrintWriter;
047: import java.io.StringWriter;
048: import java.lang.reflect.Method;
049: import java.net.URL;
050: import java.util.ArrayList;
051: import java.util.HashMap;
052: import java.util.Iterator;
053: import java.util.zip.ZipEntry;
054: import java.util.zip.ZipInputStream;
055:
056: public class POJODeployer implements Deployer {
057:
058: private static Log log = LogFactory.getLog(POJODeployer.class);
059:
060: private ConfigurationContext configCtx;
061:
062: //To initialize the deployer
063: public void init(ConfigurationContext configCtx) {
064: this .configCtx = configCtx;
065: }//Will process the file and add that to axisConfig
066:
067: public void deploy(DeploymentFileData deploymentFileData) {
068: ClassLoader threadClassLoader = null;
069: try {
070: threadClassLoader = Thread.currentThread()
071: .getContextClassLoader();
072: String extension = DeploymentFileData
073: .getFileExtension(deploymentFileData.getName());
074: if ("class".equals(extension)) {
075: File file = deploymentFileData.getFile();
076: File parentFile = file.getParentFile();
077: if (file != null) {
078: ClassLoader classLoader = Utils
079: .getClassLoader(configCtx
080: .getAxisConfiguration()
081: .getSystemClassLoader(), parentFile);
082:
083: Thread.currentThread().setContextClassLoader(
084: classLoader);
085: String className = file.getName();
086: className = className.replaceAll(".class", "");
087: JamServiceFactory factory = JamServiceFactory
088: .getInstance();
089: JamServiceParams jam_service_parms = factory
090: .createServiceParams();
091: jam_service_parms.addClassLoader(classLoader);
092: jam_service_parms.includeClass(className);
093: JamService service = factory
094: .createService(jam_service_parms);
095: JamClassIterator jClassIter = service.getClasses();
096: while (jClassIter.hasNext()) {
097: JClass jclass = (JClass) jClassIter.next();
098: if (jclass.getQualifiedName().equals(className)) {
099: /**
100: * Schema genertaion done in two stage 1. Load all the methods and
101: * create type for methods parameters (if the parameters are Bean
102: * then it will create Complex types for those , and if the
103: * parameters are simple type which decribe in SimpleTypeTable
104: * nothing will happen) 2. In the next stage for all the methods
105: * messages and port types will be creteated
106: */
107: JAnnotation annotation = jclass
108: .getAnnotation(AnnotationConstants.WEB_SERVICE);
109: if (annotation != null) {
110: // try to see whether JAX-WS jars in the class path , if so use them
111: // to process annotated pojo else use annogen to process the pojo class
112: AxisService axisService;
113: axisService = createAxisService(
114: classLoader, className,
115: deploymentFileData.getFile()
116: .toURL());
117: configCtx.getAxisConfiguration()
118: .addService(axisService);
119: } else {
120: AxisService axisService = createAxisServiceUsingAnnogen(
121: className, classLoader,
122: deploymentFileData.getFile()
123: .toURL());
124: configCtx.getAxisConfiguration()
125: .addService(axisService);
126: }
127: }
128: }
129: }
130:
131: } else if ("jar".equals(extension)) {
132: ArrayList classList;
133: FileInputStream fin = null;
134: ZipInputStream zin = null;
135: try {
136: fin = new FileInputStream(deploymentFileData
137: .getAbsolutePath());
138: zin = new ZipInputStream(fin);
139: ZipEntry entry;
140: classList = new ArrayList();
141: while ((entry = zin.getNextEntry()) != null) {
142: String name = entry.getName();
143: if (name.endsWith(".class")) {
144: classList.add(name);
145: }
146: }
147: zin.close();
148: fin.close();
149: } catch (Exception e) {
150: throw new DeploymentException(e);
151: } finally {
152: if (zin != null) {
153: zin.close();
154: }
155: if (fin != null) {
156: fin.close();
157: }
158: }
159: ArrayList axisServiceList = new ArrayList();
160: for (int i = 0; i < classList.size(); i++) {
161: String className = (String) classList.get(i);
162: ClassLoader classLoader = Utils
163: .createClassLoader(
164: new URL[] { deploymentFileData
165: .getFile().toURL() },
166: configCtx.getAxisConfiguration()
167: .getSystemClassLoader(),
168: true,
169: (File) configCtx
170: .getAxisConfiguration()
171: .getParameterValue(
172: Constants.Configuration.ARTIFACTS_TEMP_DIR));
173: Thread.currentThread().setContextClassLoader(
174: classLoader);
175: className = className.replaceAll(".class", "");
176: className = className.replaceAll("/", ".");
177: JamServiceFactory factory = JamServiceFactory
178: .getInstance();
179: JamServiceParams jam_service_parms = factory
180: .createServiceParams();
181: jam_service_parms.addClassLoader(classLoader);
182: jam_service_parms.includeClass(className);
183: JamService service = factory
184: .createService(jam_service_parms);
185: JamClassIterator jClassIter = service.getClasses();
186: while (jClassIter.hasNext()) {
187: JClass jclass = (JClass) jClassIter.next();
188: if (jclass.getQualifiedName().equals(className)) {
189: /**
190: * Schema genertaion done in two stage 1. Load all the methods and
191: * create type for methods parameters (if the parameters are Bean
192: * then it will create Complex types for those , and if the
193: * parameters are simple type which decribe in SimpleTypeTable
194: * nothing will happen) 2. In the next stage for all the methods
195: * messages and port types will be creteated
196: */
197: JAnnotation annotation = jclass
198: .getAnnotation(AnnotationConstants.WEB_SERVICE);
199: if (annotation != null) {
200: AxisService axisService;
201: axisService = createAxisService(
202: classLoader, className,
203: deploymentFileData.getFile()
204: .toURL());
205: axisServiceList.add(axisService);
206: }
207: }
208: }
209: }
210: if (axisServiceList.size() > 0) {
211: AxisServiceGroup serviceGroup = new AxisServiceGroup();
212: serviceGroup.setServiceGroupName(deploymentFileData
213: .getName());
214: for (int i = 0; i < axisServiceList.size(); i++) {
215: AxisService axisService = (AxisService) axisServiceList
216: .get(i);
217: serviceGroup.addService(axisService);
218: }
219: configCtx.getAxisConfiguration().addServiceGroup(
220: serviceGroup);
221: } else {
222: log.info("No annotated class found in the jar: "
223: + deploymentFileData.getFile().getName());
224: }
225: }
226: } catch (Exception e) {
227: StringWriter errorWriter = new StringWriter();
228: PrintWriter error_ptintWriter = new PrintWriter(errorWriter);
229: e.printStackTrace(error_ptintWriter);
230: String serviceStatus = "Error:\n" + errorWriter.toString();
231: configCtx.getAxisConfiguration().getFaultyServices().put(
232: deploymentFileData.getFile().getAbsolutePath(),
233: serviceStatus);
234: } catch (Throwable t) {
235: StringWriter errorWriter = new StringWriter();
236: PrintWriter error_ptintWriter = new PrintWriter(errorWriter);
237: t.printStackTrace(error_ptintWriter);
238: String serviceStatus = "Error:\n" + errorWriter.toString();
239: configCtx.getAxisConfiguration().getFaultyServices().put(
240: deploymentFileData.getFile().getAbsolutePath(),
241: serviceStatus);
242: } finally {
243: if (threadClassLoader != null) {
244: Thread.currentThread().setContextClassLoader(
245: threadClassLoader);
246: }
247: }
248: }
249:
250: private AxisService createAxisService(ClassLoader classLoader,
251: String className, URL serviceLocation)
252: throws ClassNotFoundException, InstantiationException,
253: IllegalAccessException, AxisFault {
254: AxisService axisService;
255: try {
256: Class claxx = Class
257: .forName("org.apache.axis2.jaxws.description.DescriptionFactory");
258: Method mthod = claxx.getMethod("createAxisService",
259: new Class[] { Class.class });
260: Class pojoClass = Loader.loadClass(classLoader, className);
261: axisService = (AxisService) mthod.invoke(claxx,
262: new Object[] { pojoClass });
263: Utils.fillAxisService(axisService, configCtx
264: .getAxisConfiguration(), new ArrayList(),
265: new ArrayList());
266: setMessageReceivers(axisService);
267:
268: } catch (Exception e) {
269: // Seems like the jax-ws jars missin in the class path .
270: // lets tryu annogen
271: axisService = createAxisServiceUsingAnnogen(className,
272: classLoader, serviceLocation);
273: }
274: return axisService;
275: }
276:
277: private AxisService createAxisServiceUsingAnnogen(String className,
278: ClassLoader classLoader, URL serviceLocation)
279: throws ClassNotFoundException, InstantiationException,
280: IllegalAccessException, AxisFault {
281: HashMap messageReciverMap = new HashMap();
282: Class inOnlyMessageReceiver = Loader
283: .loadClass("org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver");
284: MessageReceiver messageReceiver = (MessageReceiver) inOnlyMessageReceiver
285: .newInstance();
286: messageReciverMap.put(WSDL2Constants.MEP_URI_IN_ONLY,
287: messageReceiver);
288: Class inoutMessageReceiver = Loader
289: .loadClass("org.apache.axis2.rpc.receivers.RPCMessageReceiver");
290: MessageReceiver inOutmessageReceiver = (MessageReceiver) inoutMessageReceiver
291: .newInstance();
292: messageReciverMap.put(WSDL2Constants.MEP_URI_IN_OUT,
293: inOutmessageReceiver);
294: messageReciverMap.put(WSDL2Constants.MEP_URI_ROBUST_IN_ONLY,
295: inOutmessageReceiver);
296: AxisService axisService = AxisService.createService(className,
297: configCtx.getAxisConfiguration(), messageReciverMap,
298: null, null, classLoader);
299: axisService.setFileName(serviceLocation);
300: return axisService;
301: }
302:
303: public void setMessageReceivers(AxisService service) {
304: Iterator iterator = service.getOperations();
305: while (iterator.hasNext()) {
306: AxisOperation operation = (AxisOperation) iterator.next();
307: String MEP = operation.getMessageExchangePattern();
308: if (MEP != null) {
309: try {
310: if (WSDLConstants.WSDL20_2006Constants.MEP_URI_IN_ONLY
311: .equals(MEP)
312: || WSDLConstants.WSDL20_2004_Constants.MEP_URI_IN_ONLY
313: .equals(MEP)
314: || WSDL2Constants.MEP_URI_IN_ONLY
315: .equals(MEP)) {
316: Class inOnlyMessageReceiver = Loader
317: .loadClass("org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver");
318: MessageReceiver messageReceiver = (MessageReceiver) inOnlyMessageReceiver
319: .newInstance();
320: operation.setMessageReceiver(messageReceiver);
321: } else {
322: Class inoutMessageReceiver = Loader
323: .loadClass("org.apache.axis2.rpc.receivers.RPCMessageReceiver");
324: MessageReceiver inOutmessageReceiver = (MessageReceiver) inoutMessageReceiver
325: .newInstance();
326: operation
327: .setMessageReceiver(inOutmessageReceiver);
328: }
329: } catch (ClassNotFoundException e) {
330: log.error(e.getMessage(), e);
331: } catch (InstantiationException e) {
332: log.error(e.getMessage(), e);
333: } catch (IllegalAccessException e) {
334: log.error(e.getMessage(), e);
335: }
336: }
337: }
338: }
339:
340: public void setDirectory(String directory) {
341: }
342:
343: public void setExtension(String extension) {
344: }
345:
346: public void unDeploy(String fileName) {
347: if (fileName.endsWith(".class")) {
348: String className = fileName.replaceAll(".class", "");
349: try {
350: AxisServiceGroup serviceGroup = configCtx
351: .getAxisConfiguration().removeServiceGroup(
352: className);
353: configCtx.removeServiceGroupContext(serviceGroup);
354: log.info(Messages.getMessage(
355: DeploymentErrorMsgs.SERVICE_REMOVED, fileName));
356: } catch (AxisFault axisFault) {
357: //May be a faulty service
358: configCtx.getAxisConfiguration().removeFaultyService(
359: fileName);
360: }
361: } else if (fileName.endsWith(".jar")) {
362: try {
363: AxisServiceGroup serviceGroup = configCtx
364: .getAxisConfiguration().removeServiceGroup(
365: fileName);
366: configCtx.removeServiceGroupContext(serviceGroup);
367: log.info(Messages.getMessage(
368: DeploymentErrorMsgs.SERVICE_REMOVED, fileName));
369: } catch (AxisFault axisFault) {
370: //May be a faulty service
371: configCtx.getAxisConfiguration().removeFaultyService(
372: fileName);
373: }
374: }
375: }
376: }
|