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.jaxws.server.endpoint.lifecycle.impl;
020:
021: import java.lang.reflect.Method;
022:
023: import javax.xml.ws.WebServiceContext;
024:
025: import org.apache.axis2.Constants;
026: import org.apache.axis2.context.ServiceContext;
027: import org.apache.axis2.description.AxisService;
028: import org.apache.axis2.description.Parameter;
029: import org.apache.axis2.jaxws.ExceptionFactory;
030: import org.apache.axis2.jaxws.context.WebServiceContextImpl;
031: import org.apache.axis2.jaxws.context.factory.MessageContextFactory;
032: import org.apache.axis2.jaxws.context.utils.ContextUtils;
033: import org.apache.axis2.jaxws.core.MessageContext;
034: import org.apache.axis2.jaxws.description.ServiceDescription;
035: import org.apache.axis2.jaxws.handler.SoapMessageContext;
036: import org.apache.axis2.jaxws.i18n.Messages;
037: import org.apache.axis2.jaxws.lifecycle.BaseLifecycleManager;
038: import org.apache.axis2.jaxws.lifecycle.LifecycleException;
039: import org.apache.axis2.jaxws.runtime.description.injection.ResourceInjectionServiceRuntimeDescription;
040: import org.apache.axis2.jaxws.runtime.description.injection.ResourceInjectionServiceRuntimeDescriptionFactory;
041: import org.apache.axis2.jaxws.server.endpoint.injection.ResourceInjector;
042: import org.apache.axis2.jaxws.server.endpoint.injection.WebServiceContextInjector;
043: import org.apache.axis2.jaxws.server.endpoint.injection.factory.ResourceInjectionFactory;
044: import org.apache.axis2.jaxws.server.endpoint.lifecycle.EndpointLifecycleManager;
045: import org.apache.axis2.jaxws.injection.ResourceInjectionException;
046: import org.apache.axis2.util.Loader;
047: import org.apache.commons.logging.Log;
048: import org.apache.commons.logging.LogFactory;
049:
050: public class EndpointLifecycleManagerImpl extends BaseLifecycleManager
051: implements EndpointLifecycleManager {
052: public static final String WEBSERVICE_MESSAGE_CONTEXT = "javax.xml.ws.WebServiceContext";
053: private static final Log log = LogFactory
054: .getLog(EndpointLifecycleManagerImpl.class);
055:
056: public EndpointLifecycleManagerImpl(Object endpointInstance) {
057: this .instance = endpointInstance;
058: }
059:
060: public EndpointLifecycleManagerImpl() {
061: }
062:
063: /* (non-Javadoc)
064: * @see org.apache.axis2.jaxws.server.endpoint.lifecycle.EndpointLifecycleManager#createServiceInstance(org.apache.axis2.jaxws.core.MessageContext, java.lang.Class)
065: */
066: public Object createServiceInstance(MessageContext mc,
067: Class serviceImplClass) throws LifecycleException,
068: ResourceInjectionException {
069: org.apache.axis2.context.MessageContext msgContext = mc
070: .getAxisMessageContext();
071:
072: // Get the ServiceDescription and injectionDesc which contain
073: // cached information
074: ServiceDescription serviceDesc = mc.getEndpointDescription()
075: .getServiceDescription();
076: ResourceInjectionServiceRuntimeDescription injectionDesc = null;
077: if (serviceDesc != null) {
078: injectionDesc = ResourceInjectionServiceRuntimeDescriptionFactory
079: .get(serviceDesc, serviceImplClass);
080: }
081:
082: // See if there is an existing service object
083: ServiceContext serviceContext = msgContext.getServiceContext();
084: Object serviceimpl = serviceContext
085: .getProperty(ServiceContext.SERVICE_OBJECT);
086: if (serviceimpl != null) {
087: this .instance = serviceimpl;
088:
089: if (log.isDebugEnabled()) {
090: log
091: .debug("Service Instance found in the service context, reusing the instance");
092: }
093:
094: // If resource injection is needed, create the SOAPMessageContext and update the WebServiceContext
095: // Create MessageContext for current invocation.
096: if (injectionDesc != null
097: && injectionDesc.hasResourceAnnotation()) {
098: javax.xml.ws.handler.MessageContext soapMessageContext = createSOAPMessageContext(mc);
099: //Get WebServiceContext from ServiceContext
100: WebServiceContext ws = (WebServiceContext) serviceContext
101: .getProperty(WEBSERVICE_MESSAGE_CONTEXT);
102: //Add the MessageContext for current invocation
103: if (ws != null) {
104: updateWebServiceContext(ws, soapMessageContext);
105: }
106: }
107:
108: //since service impl is there in service context , take that from there
109: return serviceimpl;
110: } else {
111: // create a new service impl class for that service
112: serviceimpl = createServiceInstance(msgContext
113: .getAxisService(), serviceImplClass);
114: this .instance = serviceimpl;
115:
116: if (log.isDebugEnabled()) {
117: log.debug("New Service Instance created");
118: }
119:
120: // If resource injection is needed, create the SOAPMessageContext and build the WebServiceContext
121: // Create MessageContext for current invocation.
122: if (injectionDesc != null
123: && injectionDesc.hasResourceAnnotation()) {
124: javax.xml.ws.handler.MessageContext soapMessageContext = createSOAPMessageContext(mc);
125: // Create WebServiceContext
126: WebServiceContextImpl wsContext = new WebServiceContextImpl();
127: //Add MessageContext for this request.
128: wsContext.setSoapMessageContext(soapMessageContext);
129: // Inject WebServiceContext
130: injectWebServiceContext(mc, wsContext, serviceimpl);
131: serviceContext.setProperty(WEBSERVICE_MESSAGE_CONTEXT,
132: wsContext);
133: }
134:
135: //Invoke PostConstruct
136: if (injectionDesc != null
137: && injectionDesc.getPostConstructMethod() != null) {
138: invokePostConstruct(injectionDesc
139: .getPostConstructMethod());
140: }
141: serviceContext.setProperty(ServiceContext.SERVICE_OBJECT,
142: serviceimpl);
143: return serviceimpl;
144: }
145: }
146:
147: private Object createServiceInstance(AxisService service,
148: Class serviceImplClass) {
149: if (log.isDebugEnabled()) {
150: log.debug("Creating new instance of service endpoint");
151: }
152:
153: if (serviceImplClass == null) {
154: throw ExceptionFactory.makeWebServiceException(Messages
155: .getMessage("EndpointControllerErr5"));
156: }
157:
158: Object instance = null;
159: try {
160: // allow alternative definition of makeNewServiceObject
161: if (service != null
162: && service
163: .getParameter(Constants.SERVICE_OBJECT_SUPPLIER) != null) {
164: ClassLoader classLoader = service.getClassLoader();
165:
166: Parameter serviceObjectParam = service
167: .getParameter(Constants.SERVICE_OBJECT_SUPPLIER);
168: Class serviceObjectMaker = Loader.loadClass(
169: classLoader, ((String) serviceObjectParam
170: .getValue()).trim());
171:
172: // Find static getServiceObject() method, call it if there
173: Method method = serviceObjectMaker.getMethod(
174: "getServiceObject",
175: new Class[] { AxisService.class });
176: if (method != null) {
177: return method.invoke(serviceObjectMaker
178: .newInstance(), new Object[] { service });
179: }
180: }
181: instance = serviceImplClass.newInstance();
182: } catch (IllegalAccessException e) {
183: throw ExceptionFactory.makeWebServiceException(Messages
184: .getMessage("EndpointControllerErr6",
185: serviceImplClass.getName()));
186: } catch (InstantiationException e) {
187: throw ExceptionFactory.makeWebServiceException(Messages
188: .getMessage("EndpointControllerErr6",
189: serviceImplClass.getName()));
190: } catch (Exception e) {
191: throw ExceptionFactory.makeWebServiceException(Messages
192: .getMessage("EndpointControllerErr6",
193: serviceImplClass.getName()));
194: }
195:
196: return instance;
197: }
198:
199: private javax.xml.ws.handler.MessageContext createSOAPMessageContext(
200: MessageContext mc) {
201: SoapMessageContext soapMessageContext = (SoapMessageContext) MessageContextFactory
202: .createSoapMessageContext(mc);
203: ContextUtils.addProperties(soapMessageContext, mc);
204: return soapMessageContext;
205: }
206:
207: private void injectWebServiceContext(MessageContext mc,
208: WebServiceContext wsContext, Object serviceInstance)
209: throws ResourceInjectionException {
210: ResourceInjector ri = ResourceInjectionFactory
211: .createResourceInjector(WebServiceContext.class);
212: ri.inject(wsContext, serviceInstance);
213: }
214:
215: private void updateWebServiceContext(WebServiceContext wsContext,
216: javax.xml.ws.handler.MessageContext soapMessageContext)
217: throws ResourceInjectionException {
218: WebServiceContextInjector wci = (WebServiceContextInjector) ResourceInjectionFactory
219: .createResourceInjector(WebServiceContext.class);
220: wci.addMessageContext(wsContext, soapMessageContext);
221:
222: }
223:
224: }
|