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:
020: package org.apache.axis2.engine;
021:
022: import org.apache.axis2.AxisFault;
023: import org.apache.axis2.Constants;
024: import org.apache.axis2.service.Lifecycle;
025: import org.apache.axis2.context.ServiceContext;
026: import org.apache.axis2.context.ServiceGroupContext;
027: import org.apache.axis2.description.AxisService;
028: import org.apache.axis2.description.AxisServiceGroup;
029: import org.apache.axis2.description.Parameter;
030: import org.apache.axis2.util.Loader;
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: import java.lang.reflect.InvocationTargetException;
035: import java.lang.reflect.Method;
036: import java.util.Iterator;
037:
038: /**
039: * If the service implementation has an init method with 1 or 2 message context as its parameters, then
040: * the DependencyManager calls the init method with appropriate parameters.
041: */
042: public class DependencyManager {
043: private static final Log log = LogFactory
044: .getLog(DependencyManager.class);
045: public final static String SERVICE_INIT_METHOD = "init";
046: public final static String SERVICE_DESTROY_METHOD = "destroy";
047:
048: /**
049: * Initialize a new service object. Essentially, check to see if the object wants to receive
050: * an init() call - if so, call it.
051: *
052: * @param obj the service object
053: * @param serviceContext the active ServiceContext
054: * @throws AxisFault if there's a problem initializing
055: *
056: * @deprecated please use initServiceObject()
057: */
058: public static void initServiceClass(Object obj,
059: ServiceContext serviceContext) throws AxisFault {
060: initServiceObject(obj, serviceContext);
061: }
062:
063: /**
064: * Initialize a new service object. Essentially, check to see if the object wants to receive
065: * an init() call - if so, call it.
066: *
067: * @param obj the service object
068: * @param serviceContext the active ServiceContext
069: * @throws AxisFault if there's a problem initializing
070: */
071: public static void initServiceObject(Object obj,
072: ServiceContext serviceContext) throws AxisFault {
073: // This is the way to do things into the future.
074: if (obj instanceof Lifecycle) {
075: ((Lifecycle) obj).init(serviceContext);
076: return;
077: }
078:
079: // ...however, we also still support the old way for now. Note that introspecting for
080: // a method like this is something like 10 times slower than the above instanceof check.
081:
082: Class classToLoad = obj.getClass();
083: // We can not call classToLoad.getDeclaredMethed() , since there
084: // can be insatnce where mutiple services extends using one class
085: // just for init and other reflection methods
086: Method method = null;
087: try {
088: method = classToLoad.getMethod(SERVICE_INIT_METHOD,
089: new Class[] { ServiceContext.class });
090: } catch (Exception e) {
091: //We do not need to inform this to user , since this something
092: // Axis2 is checking to support Session. So if the method is
093: // not there we should ignore that
094: }
095: if (method != null) {
096: try {
097: method.invoke(obj, new Object[] { serviceContext });
098: } catch (IllegalAccessException e) {
099: log.info("Exception trying to call "
100: + SERVICE_INIT_METHOD, e);
101: } catch (IllegalArgumentException e) {
102: log.info("Exception trying to call "
103: + SERVICE_INIT_METHOD, e);
104: } catch (InvocationTargetException e) {
105: log.info("Exception trying to call "
106: + SERVICE_INIT_METHOD, e);
107: }
108: }
109: }
110:
111: /**
112: * To init all the services in application scope
113: *
114: * @param serviceGroupContext the ServiceGroupContext from which to extract all the services
115: * @throws AxisFault if there's a problem initializing
116: */
117: public static void initService(
118: ServiceGroupContext serviceGroupContext) throws AxisFault {
119: AxisServiceGroup serviceGroup = serviceGroupContext
120: .getDescription();
121: Iterator serviceItr = serviceGroup.getServices();
122: while (serviceItr.hasNext()) {
123: AxisService axisService = (AxisService) serviceItr.next();
124: ServiceContext serviceContext = serviceGroupContext
125: .getServiceContext(axisService);
126: AxisService service = serviceContext.getAxisService();
127: ClassLoader classLoader = service.getClassLoader();
128: Parameter implInfoParam = service
129: .getParameter(Constants.SERVICE_CLASS);
130: if (implInfoParam != null) {
131: try {
132: Class implClass = Loader.loadClass(classLoader,
133: ((String) implInfoParam.getValue()).trim());
134: Object serviceImpl = implClass.newInstance();
135: serviceContext.setProperty(
136: ServiceContext.SERVICE_OBJECT, serviceImpl);
137: initServiceObject(serviceImpl, serviceContext);
138: } catch (Exception e) {
139: AxisFault.makeFault(e);
140: }
141: }
142: }
143: }
144:
145: /**
146: * Notify a service object that it's on death row.
147: * @param serviceContext the active ServiceContext
148: */
149: public static void destroyServiceObject(
150: ServiceContext serviceContext) {
151: Object obj = serviceContext
152: .getProperty(ServiceContext.SERVICE_OBJECT);
153: if (obj != null) {
154: // If this is a Lifecycle object, just call it.
155: if (obj instanceof Lifecycle) {
156: ((Lifecycle) obj).destroy(serviceContext);
157: return;
158: }
159:
160: // For now, we also use "raw" introspection to try and find the destroy method.
161:
162: Class classToLoad = obj.getClass();
163: Method method = null;
164: try {
165: method = classToLoad.getMethod(SERVICE_DESTROY_METHOD,
166: new Class[] { ServiceContext.class });
167: } catch (NoSuchMethodException e) {
168: //We do not need to inform this to user , since this something
169: // Axis2 is checking to support Session. So if the method is
170: // not there we should ignore that
171: }
172:
173: if (method != null) {
174: try {
175: method.invoke(obj, new Object[] { serviceContext });
176: } catch (IllegalAccessException e) {
177: log.info("Exception trying to call "
178: + SERVICE_DESTROY_METHOD, e);
179: } catch (InvocationTargetException e) {
180: log.info("Exception trying to call "
181: + SERVICE_DESTROY_METHOD, e);
182: }
183: }
184:
185: }
186: }
187: }
|