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.naming.Context;
024    import javax.xml.ws.WebServiceException;
025    
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    import org.apache.cxf.Bus;
029    import org.apache.cxf.jaxws.JAXWSMethodInvoker;
030    import org.apache.cxf.jaxws.context.WebServiceContextImpl;
031    import org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean;
032    import org.apache.geronimo.cxf.CXFEndpoint;
033    import org.apache.geronimo.cxf.CXFServiceConfiguration;
034    import org.apache.geronimo.cxf.GeronimoJaxWsImplementorInfo;
035    import org.apache.geronimo.jaxws.JAXWSAnnotationProcessor;
036    import org.apache.geronimo.jaxws.JNDIResolver;
037    import org.apache.geronimo.jaxws.annotations.AnnotationHolder;
038    
039    public class POJOEndpoint extends CXFEndpoint {
040      
041        private static final Log LOG = LogFactory.getLog(POJOEndpoint.class);
042        
043        private AnnotationHolder holder;
044    
045        public POJOEndpoint(Bus bus, 
046                            URL configurationBaseUrl, 
047                            Class instance) {
048            super(bus, instance);
049            
050            implInfo = new GeronimoJaxWsImplementorInfo(instance, this.portInfo, instance.getClassLoader());
051    
052            serviceFactory = new JaxWsServiceFactoryBean(implInfo);        
053            serviceFactory.setBus(bus);
054                    
055            String wsdlLocation = null;
056            if (this.portInfo.getWsdlFile() != null) {
057                wsdlLocation = this.portInfo.getWsdlFile();
058            } else {
059                wsdlLocation = implInfo.getWsdlLocation();
060            }        
061            URL wsdlURL = getWsdlURL(configurationBaseUrl, wsdlLocation);
062    
063            // install as first to overwrite annotations (wsdl-file, wsdl-port, wsdl-service)
064            CXFServiceConfiguration configuration = 
065                new CXFServiceConfiguration(this.portInfo, wsdlURL);
066            serviceFactory.getConfigurations().add(0, configuration);
067    
068            service = serviceFactory.create();
069            
070            this.holder = bus.getExtension(AnnotationHolder.class);
071            
072            Context context = bus.getExtension(Context.class);
073            
074            // instantiate and inject resources into service
075            try {
076                this.implementor = this.holder.newInstance(instance.getName(), 
077                                                           instance.getClassLoader(), 
078                                                           context);
079            } catch (Exception e) {
080                throw new WebServiceException("Service resource injection failed", e);
081            }
082            
083            service.setInvoker(new JAXWSMethodInvoker(this.implementor));       
084    
085            JNDIResolver jndiResolver = (JNDIResolver) bus.getExtension(JNDIResolver.class);
086            this.annotationProcessor = new JAXWSAnnotationProcessor(jndiResolver, new WebServiceContextImpl());
087        }
088        
089        protected void init() {        
090            // configure and inject handlers
091            try {
092                initHandlers();
093                injectHandlers();
094            } catch (Exception e) {
095                throw new WebServiceException("Error configuring handlers", e);
096            }
097        }
098    
099        public void stop() {
100            // call handler preDestroy
101            destroyHandlers();
102    
103            // call service preDestroy
104            if (this.implementor != null) {
105                try {
106                    this.holder.destroyInstance(this.implementor);
107                } catch (Exception e) {
108                    LOG.warn("Error calling @PreDestroy method", e); 
109                }
110            }
111    
112            // shutdown server
113            super.stop();
114        }
115    }