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