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.geronimo.cxf.ejb;
020    
021    import java.net.URL;
022    import java.util.List;
023    
024    import javax.naming.InitialContext;
025    import javax.naming.NamingException;
026    import javax.xml.ws.WebServiceContext;
027    import javax.xml.ws.WebServiceException;
028    
029    import org.apache.cxf.Bus;
030    import org.apache.cxf.binding.soap.SoapBinding;
031    import org.apache.cxf.binding.soap.interceptor.MustUnderstandInterceptor;
032    import org.apache.cxf.binding.soap.saaj.SAAJInInterceptor;
033    import org.apache.cxf.endpoint.Endpoint;
034    import org.apache.cxf.interceptor.Interceptor;
035    import org.apache.cxf.jaxws.handler.logical.LogicalHandlerInInterceptor;
036    import org.apache.cxf.jaxws.handler.soap.SOAPHandlerInterceptor;
037    import org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean;
038    import org.apache.geronimo.cxf.CXFEndpoint;
039    import org.apache.geronimo.cxf.CXFServiceConfiguration;
040    import org.apache.geronimo.cxf.GeronimoJaxWsImplementorInfo;
041    import org.apache.geronimo.jaxws.JAXWSAnnotationProcessor;
042    import org.apache.geronimo.jaxws.JAXWSUtils;
043    import org.apache.geronimo.jaxws.JNDIResolver;
044    import org.apache.openejb.DeploymentInfo;
045    
046    public class EJBEndpoint extends CXFEndpoint {
047        
048        public EJBEndpoint(Bus bus,
049                           URL configurationBaseUrl,
050                           Class instance) {
051            super(bus, instance);
052                    
053            String bindingURI = null;
054            if (this.portInfo.getProtocolBinding() != null) {
055                bindingURI = JAXWSUtils.getBindingURI(this.portInfo.getProtocolBinding());
056            }
057            implInfo = new GeronimoJaxWsImplementorInfo((Class)implementor, bindingURI);
058    
059            serviceFactory = new JaxWsServiceFactoryBean(implInfo);       
060            serviceFactory.setBus(bus);
061    
062            String wsdlLocation = null;
063            if (this.portInfo.getWsdlFile() != null) {
064                wsdlLocation = this.portInfo.getWsdlFile();
065            } else {
066                wsdlLocation = implInfo.getWsdlLocation();
067            }        
068            URL wsdlURL = getWsdlURL(configurationBaseUrl, wsdlLocation);
069    
070            // install as first to overwrite annotations (wsdl-file, wsdl-port, wsdl-service)
071            CXFServiceConfiguration configuration = 
072                new CXFServiceConfiguration(this.portInfo, wsdlURL);
073            serviceFactory.getConfigurations().add(0, configuration);
074    
075            service = serviceFactory.create();        
076        }
077        
078        protected Class getImplementorClass() {
079            return (Class)this.implementor;
080        }
081        
082        protected void init() {
083            // configure handlers
084            try {
085                initHandlers();
086            } catch (Exception e) {
087                throw new WebServiceException("Error configuring handlers", e);
088            }
089                    
090            DeploymentInfo deploymentInfo = 
091                (DeploymentInfo)bus.getExtension(DeploymentInfo.class);
092            
093            service.setInvoker(new EJBMethodInvoker(this, this.bus, deploymentInfo));  
094            
095            Endpoint endpoint = getEndpoint();
096            
097            /* 
098             * Remove interceptors that perform handler processing since
099             * handler processing must happen within the EJB container.
100             */        
101            removeHandlerInterceptors(bus.getInInterceptors());
102            removeHandlerInterceptors(endpoint.getInInterceptors());
103            removeHandlerInterceptors(endpoint.getBinding().getInInterceptors());
104            removeHandlerInterceptors(endpoint.getService().getInInterceptors());
105            
106            // install SAAJ interceptor        
107            if (endpoint.getBinding() instanceof SoapBinding &&
108                !this.implInfo.isWebServiceProvider()) {
109                endpoint.getService().getInInterceptors().add(new SAAJInInterceptor());
110            }        
111        }
112            
113        private static void removeHandlerInterceptors(List<Interceptor> interceptors) {
114            for (Interceptor interceptor : interceptors) {
115                if (interceptor instanceof MustUnderstandInterceptor ||
116                    interceptor instanceof LogicalHandlerInInterceptor ||
117                    interceptor instanceof SOAPHandlerInterceptor) {
118                    interceptors.remove(interceptor);
119                }
120            } 
121        }
122        
123        public synchronized void injectHandlers() {
124            if (this.annotationProcessor != null) {
125                // assume injection was already done
126                return;
127            }
128            
129            WebServiceContext wsContext = null;
130            try {
131                InitialContext ctx = new InitialContext();
132                wsContext = (WebServiceContext) ctx.lookup("java:comp/WebServiceContext");
133            } catch (NamingException e) {
134                throw new WebServiceException("Failed to lookup WebServiceContext", e);
135            }
136            
137            this.annotationProcessor = new JAXWSAnnotationProcessor(new JNDIResolver(), wsContext);
138            super.injectHandlers();
139        }
140        
141        public void stop() {
142            // call handler preDestroy
143            destroyHandlers();
144    
145            // shutdown server
146            super.stop();
147        }
148        
149    }