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.pojo;
020    
021    import java.net.URL;
022    
023    import javax.xml.ws.WebServiceException;
024    
025    import org.apache.cxf.Bus;
026    import org.apache.cxf.jaxws.JAXWSMethodInvoker;
027    import org.apache.cxf.jaxws.context.WebServiceContextImpl;
028    import org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean;
029    import org.apache.geronimo.cxf.CXFEndpoint;
030    import org.apache.geronimo.cxf.CXFServiceConfiguration;
031    import org.apache.geronimo.cxf.GeronimoJaxWsImplementorInfo;
032    import org.apache.geronimo.jaxws.JAXWSAnnotationProcessor;
033    import org.apache.geronimo.jaxws.JAXWSUtils;
034    import org.apache.geronimo.jaxws.JNDIResolver;
035    import org.apache.geronimo.jaxws.annotations.AnnotationException;
036    
037    public class POJOEndpoint extends CXFEndpoint {
038      
039        public POJOEndpoint(Bus bus, URL configurationBaseUrl, Object instance) {
040            super(bus, instance);
041            
042            String bindingURI = null;
043            if (this.portInfo.getProtocolBinding() != null) {
044                bindingURI = JAXWSUtils.getBindingURI(this.portInfo.getProtocolBinding());
045            }
046            implInfo = new GeronimoJaxWsImplementorInfo(implementor.getClass(), bindingURI);
047    
048            serviceFactory = new JaxWsServiceFactoryBean(implInfo);        
049            serviceFactory.setBus(bus);
050                    
051            String wsdlLocation = null;
052            if (this.portInfo.getWsdlFile() != null) {
053                wsdlLocation = this.portInfo.getWsdlFile();
054            } else {
055                wsdlLocation = implInfo.getWsdlLocation();
056            }        
057            URL wsdlURL = getWsdlURL(configurationBaseUrl, wsdlLocation);
058    
059            // install as first to overwrite annotations (wsdl-file, wsdl-port, wsdl-service)
060            CXFServiceConfiguration configuration = 
061                new CXFServiceConfiguration(this.portInfo, wsdlURL);
062            serviceFactory.getConfigurations().add(0, configuration);
063    
064            service = serviceFactory.create();
065            
066            service.setInvoker(new JAXWSMethodInvoker(instance));       
067    
068            JNDIResolver jndiResolver = (JNDIResolver) bus.getExtension(JNDIResolver.class);
069            this.annotationProcessor = new JAXWSAnnotationProcessor(jndiResolver, new WebServiceContextImpl());
070        }
071        
072        protected void init() {        
073            // configure and inject handlers
074            try {
075                initHandlers();
076                injectHandlers();
077            } catch (Exception e) {
078                throw new WebServiceException("Error configuring handlers", e);
079            }
080    
081            // inject resources into service
082            try {
083                injectResources(this.implementor);
084            } catch (AnnotationException e) {
085                throw new WebServiceException("Service resource injection failed", e);
086            }
087        }
088    
089        public void stop() {
090            // call handler preDestroy
091            destroyHandlers();
092    
093            // call service preDestroy
094            this.annotationProcessor.invokePreDestroy(this.implementor);
095    
096            // shutdown server
097            super.stop();
098        }
099    }