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.geronimo.cxf.CXFCatalogUtils;
031    import org.apache.geronimo.cxf.CXFWebServiceContainer;
032    import org.apache.geronimo.gbean.GBeanInfo;
033    import org.apache.geronimo.gbean.GBeanInfoBuilder;
034    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
035    import org.apache.geronimo.jaxws.JNDIResolver;
036    import org.apache.geronimo.jaxws.PortInfo;
037    import org.apache.geronimo.jaxws.ServerJNDIResolver;
038    import org.apache.geronimo.kernel.Kernel;
039    import org.apache.geronimo.naming.enc.EnterpriseNamingContext;
040    import org.apache.geronimo.transaction.GeronimoUserTransaction;
041    import org.apache.geronimo.webservices.WebServiceContainer;
042    import org.apache.geronimo.webservices.WebServiceContainerFactory;
043    
044    /**
045     * @version $Rev: 508298 $ $Date: 2007-02-15 22:25:05 -0500 (Thu, 15 Feb 2007) $
046     */
047    public class POJOWebServiceContainerFactoryGBean implements WebServiceContainerFactory {
048    
049        private static final Log LOG = LogFactory.getLog(POJOWebServiceContainerFactoryGBean.class);
050    
051        private final Bus bus;
052        private final Object endpointInstance;
053        private final URL configurationBaseUrl;
054    
055        public POJOWebServiceContainerFactoryGBean(PortInfo portInfo,
056                                                   String endpointClassName,
057                                                   ClassLoader classLoader,
058                                                   Map componentContext,
059                                                   Kernel kernel,
060                                                   TransactionManager transactionManager,
061                                                   URL configurationBaseUrl)
062                throws ClassNotFoundException, 
063                       IllegalAccessException,
064                       InstantiationException {
065            
066            Context context = null;
067            
068            if (componentContext != null) {
069                GeronimoUserTransaction userTransaction = new GeronimoUserTransaction(transactionManager);
070                try {
071                    context = EnterpriseNamingContext.createEnterpriseNamingContext(componentContext,
072                                                                                    userTransaction,
073                                                                                    kernel,
074                                                                                    classLoader);
075                } catch (NamingException e) {
076                    LOG.warn("Failed to create naming context", e);
077                }
078            }
079    
080            this.bus = CXFWebServiceContainer.getBus();     
081            this.configurationBaseUrl = configurationBaseUrl;
082            
083            Class endpointClass = classLoader.loadClass(endpointClassName);
084            this.endpointInstance = endpointClass.newInstance();
085            
086            this.bus.setExtension(new ServerJNDIResolver(context), JNDIResolver.class);
087            this.bus.setExtension(portInfo, PortInfo.class);   
088            
089            CXFCatalogUtils.loadOASISCatalog(this.bus, 
090                                             this.configurationBaseUrl, 
091                                             "WEB-INF/jax-ws-catalog.xml");
092        }
093        
094        public WebServiceContainer getWebServiceContainer() {
095            return new POJOWebServiceContainer(bus, configurationBaseUrl, endpointInstance);
096        }
097    
098        public static final GBeanInfo GBEAN_INFO;
099    
100        static {
101            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(POJOWebServiceContainerFactoryGBean.class, NameFactory.GERONIMO_SERVICE);
102            infoBuilder.addAttribute("portInfo", PortInfo.class, true, true);
103            infoBuilder.addAttribute("endpointClassName", String.class, true, true);
104            infoBuilder.addAttribute("classLoader", ClassLoader.class, false);
105            infoBuilder.addAttribute("componentContext", Map.class, true, true);
106            infoBuilder.addAttribute("kernel", Kernel.class, false);
107            infoBuilder.addReference("TransactionManager", TransactionManager.class, NameFactory.TRANSACTION_MANAGER);
108            infoBuilder.addAttribute("configurationBaseUrl", URL.class, true);
109    
110            infoBuilder.setConstructor(new String[]{
111                    "portInfo", 
112                    "endpointClassName", 
113                    "classLoader",
114                    "componentContext", 
115                    "kernel", 
116                    "TransactionManager", 
117                    "configurationBaseUrl"
118            });
119            
120            GBEAN_INFO = infoBuilder.getBeanInfo();
121        }
122    
123        public static GBeanInfo getGBeanInfo() {
124            return GBEAN_INFO;
125        }
126    }