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.cxf.pojo;
019    
020    import java.net.URL;
021    import java.util.Map;
022    
023    import javax.naming.Context;
024    import javax.naming.NamingException;
025    import javax.transaction.TransactionManager;
026    
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    import org.apache.cxf.Bus;
030    import org.apache.cxf.jaxws.context.WebServiceContextImpl;
031    import org.apache.geronimo.cxf.CXFCatalogUtils;
032    import org.apache.geronimo.cxf.CXFWebServiceContainer;
033    import org.apache.geronimo.gbean.GBeanInfo;
034    import org.apache.geronimo.gbean.GBeanInfoBuilder;
035    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
036    import org.apache.geronimo.jaxws.JNDIResolver;
037    import org.apache.geronimo.jaxws.PortInfo;
038    import org.apache.geronimo.jaxws.ServerJNDIResolver;
039    import org.apache.geronimo.jaxws.annotations.AnnotationHolder;
040    import org.apache.geronimo.kernel.Kernel;
041    import org.apache.geronimo.naming.enc.EnterpriseNamingContext;
042    import org.apache.geronimo.naming.reference.SimpleReference;
043    import org.apache.geronimo.transaction.GeronimoUserTransaction;
044    import org.apache.geronimo.webservices.WebServiceContainer;
045    import org.apache.geronimo.webservices.WebServiceContainerFactory;
046    
047    /**
048     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
049     */
050    public class POJOWebServiceContainerFactoryGBean implements WebServiceContainerFactory {
051    
052        private static final Log LOG = LogFactory.getLog(POJOWebServiceContainerFactoryGBean.class);
053    
054        private final Bus bus;
055        private final Class servletClass;
056        private final URL configurationBaseUrl;
057    
058        public POJOWebServiceContainerFactoryGBean(PortInfo portInfo,
059                                                   String endpointClassName,
060                                                   ClassLoader classLoader,
061                                                   Map componentContext,
062                                                   Kernel kernel,
063                                                   TransactionManager transactionManager,
064                                                   URL configurationBaseUrl,
065                                                   AnnotationHolder holder,
066                                                   String contextRoot)
067                throws ClassNotFoundException, 
068                       IllegalAccessException,
069                       InstantiationException {
070            
071            Context context = null;
072            
073            if (componentContext != null) {
074                
075                // The name should match WebServiceContextAnnotationHelper.RELATIVE_JNDI_NAME
076                componentContext.put("env/WebServiceContext", new WebServiceContextReference());
077                
078                GeronimoUserTransaction userTransaction = new GeronimoUserTransaction(transactionManager);
079                try {
080                    context = EnterpriseNamingContext.createEnterpriseNamingContext(componentContext,
081                                                                                    userTransaction,
082                                                                                    kernel,
083                                                                                    classLoader);
084                } catch (NamingException e) {
085                    LOG.warn("Failed to create naming context", e);
086                }
087            }
088    
089            this.bus = CXFWebServiceContainer.getBus();     
090            this.configurationBaseUrl = configurationBaseUrl;
091            
092            this.servletClass = classLoader.loadClass(endpointClassName);
093            
094            this.bus.setExtension(new ServerJNDIResolver(context), JNDIResolver.class);
095            this.bus.setExtension(portInfo, PortInfo.class); 
096            this.bus.setExtension(context, Context.class);
097            this.bus.setExtension(holder, AnnotationHolder.class);
098            
099            CXFCatalogUtils.loadOASISCatalog(this.bus, 
100                                             this.configurationBaseUrl, 
101                                             "WEB-INF/jax-ws-catalog.xml");
102        }
103        
104        public WebServiceContainer getWebServiceContainer() {
105            return new POJOWebServiceContainer(bus, configurationBaseUrl, servletClass);
106        }
107    
108        private static class WebServiceContextReference extends SimpleReference {
109            public Object getContent() throws NamingException {
110                return new WebServiceContextImpl();
111            }
112        }
113        
114        public static final GBeanInfo GBEAN_INFO;
115    
116        static {
117            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(POJOWebServiceContainerFactoryGBean.class, NameFactory.GERONIMO_SERVICE);
118            infoBuilder.addAttribute("portInfo", PortInfo.class, true, true);
119            infoBuilder.addAttribute("endpointClassName", String.class, true, true);
120            infoBuilder.addAttribute("classLoader", ClassLoader.class, false);
121            infoBuilder.addAttribute("componentContext", Map.class, true, true);
122            infoBuilder.addAttribute("kernel", Kernel.class, false);
123            infoBuilder.addReference("TransactionManager", TransactionManager.class, NameFactory.JTA_RESOURCE);
124            infoBuilder.addAttribute("configurationBaseUrl", URL.class, true);
125            infoBuilder.addAttribute("holder", AnnotationHolder.class, true);
126            infoBuilder.addAttribute("contextRoot", String.class, true, true);
127    
128            infoBuilder.setConstructor(new String[]{
129                    "portInfo", 
130                    "endpointClassName", 
131                    "classLoader",
132                    "componentContext", 
133                    "kernel", 
134                    "TransactionManager", 
135                    "configurationBaseUrl",
136                    "holder",
137                    "contextRoot"
138            });
139            
140            GBEAN_INFO = infoBuilder.getBeanInfo();
141        }
142    
143        public static GBeanInfo getGBeanInfo() {
144            return GBEAN_INFO;
145        }
146    }