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.ejb;
019    
020    import java.net.URL;
021    
022    import javax.naming.Context;
023    import javax.naming.InitialContext;
024    import javax.naming.NamingException;
025    import javax.xml.ws.WebServiceContext;
026    import javax.xml.ws.WebServiceException;
027    
028    import org.apache.axis2.util.JavaUtils;
029    import org.apache.geronimo.axis2.Axis2WebServiceContainer;
030    import org.apache.geronimo.axis2.AxisServiceGenerator;
031    import org.apache.geronimo.jaxws.JAXWSAnnotationProcessor;
032    import org.apache.geronimo.jaxws.JNDIResolver;
033    import org.apache.geronimo.jaxws.PortInfo;
034    import org.apache.openejb.DeploymentInfo;
035    
036    /**
037     * @version $Rev$ $Date$
038     */
039    public class EJBWebServiceContainer extends Axis2WebServiceContainer {
040    
041        private String contextRoot = null;
042        private DeploymentInfo deploymnetInfo;
043        
044        public EJBWebServiceContainer(PortInfo portInfo,
045                                      String endpointClassName,
046                                      ClassLoader classLoader,
047                                      Context context,
048                                      URL configurationBaseUrl,
049                                      DeploymentInfo deploymnetInfo) {
050            super(portInfo, endpointClassName, classLoader, context, configurationBaseUrl);
051            this.deploymnetInfo = deploymnetInfo;
052        }
053        
054        @Override
055        public void init() throws Exception { 
056            super.init();
057            
058            // configure handlers
059            try {
060                configureHandlers();
061            } catch (Exception e) {
062                throw new WebServiceException("Error configuring handlers", e);
063            }
064        }
065        
066        @Override
067        protected AxisServiceGenerator createServiceGenerator() {
068            AxisServiceGenerator serviceGenerator = super.createServiceGenerator();
069            EJBMessageReceiver messageReceiver = 
070                new EJBMessageReceiver(this, this.endpointClass, this.deploymnetInfo);
071            serviceGenerator.setMessageReceiver(messageReceiver);
072            return serviceGenerator;
073        }
074        
075        @Override
076        protected void initContextRoot(Request request) {       
077            String servicePath = portInfo.getLocation();
078            
079            if (contextRoot == null || "".equals(contextRoot)) {
080                String[] parts = JavaUtils.split(request.getContextPath(), '/');
081                if (parts != null) {
082                    for (int i = 0; i < parts.length; i++) {
083                        if (parts[i].length() > 0) {
084                            contextRoot = parts[i];
085                            break;
086                        }
087                    }
088                }
089                if (contextRoot == null || request.getContextPath().equals("/")) {
090                    contextRoot = "/";
091                } else { //when contextRoot is not "/"
092                    //set the servicePath here for EJB.
093                    //check if portInfo.getLocation() contains contextRoot, if so, strip it.
094                    int i = servicePath.indexOf(contextRoot);
095                    if (i > -1) {
096                        servicePath = servicePath.substring(i + contextRoot.length() + 1);
097                        servicePath.trim();
098                    }
099                }
100                configurationContext.setServicePath(servicePath);
101                
102                //need to setContextRoot after servicePath as cachedServicePath is only built 
103                //when setContextRoot is called.
104                configurationContext.setContextRoot(contextRoot);  
105            } 
106        }
107        
108        public synchronized void injectHandlers() {
109            if (this.annotationProcessor != null) {
110                // assume injection was already done
111                return;
112            }
113            
114            WebServiceContext wsContext = null;
115            try {
116                InitialContext ctx = new InitialContext();
117                wsContext = (WebServiceContext) ctx.lookup("java:comp/WebServiceContext");
118            } catch (NamingException e) {
119                throw new WebServiceException("Failed to lookup WebServiceContext", e);
120            }
121            
122            this.annotationProcessor = new JAXWSAnnotationProcessor(new JNDIResolver(), wsContext);
123            super.injectHandlers();
124        }
125        
126        @Override
127        public void destroy() {
128            // call handler preDestroy
129            destroyHandlers();
130            
131            super.destroy();
132        }
133    }