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.axis2.pojo;
017:
018: import java.net.URL;
019:
020: import javax.naming.Context;
021: import javax.xml.ws.WebServiceException;
022:
023: import org.apache.axis2.context.ConfigurationContext;
024: import org.apache.axis2.context.MessageContext;
025: import org.apache.axis2.context.ServiceContext;
026: import org.apache.axis2.description.AxisService;
027: import org.apache.axis2.jaxws.registry.FactoryRegistry;
028: import org.apache.axis2.jaxws.server.endpoint.lifecycle.factory.EndpointLifecycleManagerFactory;
029: import org.apache.axis2.transport.http.HTTPConstants;
030: import org.apache.axis2.transport.http.HTTPTransportUtils;
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.apache.geronimo.axis2.Axis2WebServiceContainer;
034: import org.apache.geronimo.jaxws.JAXWSAnnotationProcessor;
035: import org.apache.geronimo.jaxws.PortInfo;
036: import org.apache.geronimo.jaxws.annotations.AnnotationHolder;
037:
038: /**
039: * @version $Rev$ $Date$
040: */
041: public class POJOWebServiceContainer extends Axis2WebServiceContainer {
042:
043: private static final Log LOG = LogFactory
044: .getLog(POJOWebServiceContainer.class);
045:
046: private Object endpointInstance;
047: private String contextRoot;
048: private AnnotationHolder holder;
049:
050: public POJOWebServiceContainer(PortInfo portInfo,
051: String endpointClassName, ClassLoader classLoader,
052: Context context, URL configurationBaseUrl,
053: AnnotationHolder holder, String contextRoot) {
054: super (portInfo, endpointClassName, classLoader, context,
055: configurationBaseUrl);
056: this .holder = holder;
057: this .contextRoot = contextRoot;
058: }
059:
060: @Override
061: public void init() throws Exception {
062: super .init();
063:
064: /*
065: * This replaces EndpointLifecycleManagerFactory for all web services.
066: * This should be ok as we do our own endpoint instance management and injection.
067: */
068: FactoryRegistry.setFactory(
069: EndpointLifecycleManagerFactory.class,
070: new POJOEndpointLifecycleManagerFactory());
071:
072: String servicePath = trimContext(getServicePath(this .contextRoot));
073: this .configurationContext.setServicePath(servicePath);
074: //need to setContextRoot after servicePath as cachedServicePath is only built
075: //when setContextRoot is called.
076: String rootContext = trimContext(this .contextRoot);
077: this .configurationContext.setContextRoot(rootContext);
078:
079: // instantiate and inject resources into service
080: try {
081: this .endpointInstance = this .holder.newInstance(
082: this .endpointClass.getName(), this .endpointClass
083: .getClassLoader(), this .context);
084: } catch (Exception e) {
085: throw new WebServiceException(
086: "Service resource injection failed", e);
087: }
088:
089: this .annotationProcessor = new JAXWSAnnotationProcessor(
090: this .jndiResolver, new POJOWebServiceContext());
091:
092: // configure and inject handlers
093: try {
094: configureHandlers();
095: injectHandlers();
096: } catch (Exception e) {
097: throw new WebServiceException("Error configuring handlers",
098: e);
099: }
100:
101: }
102:
103: @Override
104: protected void processPOSTRequest(Request request,
105: Response response, AxisService service,
106: MessageContext msgContext) throws Exception {
107: String contentType = request
108: .getHeader(HTTPConstants.HEADER_CONTENT_TYPE);
109: String soapAction = request
110: .getHeader(HTTPConstants.HEADER_SOAP_ACTION);
111: if (soapAction == null) {
112: soapAction = "\"\"";
113: }
114:
115: ConfigurationContext configurationContext = msgContext
116: .getConfigurationContext();
117: configurationContext
118: .fillServiceContextAndServiceGroupContext(msgContext);
119:
120: setMsgContextProperties(request, response, service, msgContext);
121:
122: ServiceContext serviceContext = msgContext.getServiceContext();
123: serviceContext.setProperty(ServiceContext.SERVICE_OBJECT,
124: this .endpointInstance);
125:
126: try {
127: HTTPTransportUtils.processHTTPPostRequest(msgContext,
128: request.getInputStream(), response
129: .getOutputStream(), contentType,
130: soapAction, request.getURI().getPath());
131: } finally {
132: // de-associate JAX-WS MessageContext with the thread
133: // (association happens in POJOEndpointLifecycleManager.createService() call)
134: POJOWebServiceContext.clear();
135: }
136: }
137:
138: @Override
139: public void destroy() {
140: // call handler preDestroy
141: destroyHandlers();
142:
143: // call service preDestroy
144: if (this .endpointInstance != null) {
145: try {
146: this .holder.destroyInstance(this .endpointInstance);
147: } catch (Exception e) {
148: LOG.warn("Error calling @PreDestroy method", e);
149: }
150: }
151:
152: super.destroy();
153: }
154: }
|