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.axis2.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.geronimo.gbean.GBeanInfo;
030    import org.apache.geronimo.gbean.GBeanInfoBuilder;
031    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
032    import org.apache.geronimo.jaxws.annotations.AnnotationHolder;
033    import org.apache.geronimo.kernel.Kernel;
034    import org.apache.geronimo.naming.enc.EnterpriseNamingContext;
035    import org.apache.geronimo.naming.reference.SimpleReference;
036    import org.apache.geronimo.transaction.GeronimoUserTransaction;
037    import org.apache.geronimo.webservices.WebServiceContainer;
038    import org.apache.geronimo.webservices.WebServiceContainerFactory;
039    
040    /**
041     * @version $Rev$ $Date$
042     */
043    public class POJOWebServiceContainerFactoryGBean implements WebServiceContainerFactory {
044    
045        private static final Log LOG = LogFactory.getLog(POJOWebServiceContainerFactoryGBean.class);
046        
047        private final ClassLoader classLoader;
048        private final org.apache.geronimo.jaxws.PortInfo portInfo;
049        private final String endpointClassName;
050        private URL configurationBaseUrl;
051        private Context context;
052        private AnnotationHolder holder;
053    
054        public POJOWebServiceContainerFactoryGBean(org.apache.geronimo.jaxws.PortInfo portInfo,
055                                                   String endpointClassName,
056                                                   ClassLoader classLoader,
057                                                   Map componentContext,
058                                                   Kernel kernel,
059                                                   TransactionManager transactionManager,
060                                                   URL configurationBaseUrl,
061                                                   AnnotationHolder holder)
062            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
063            
064            if (componentContext != null) {
065                
066                // The name should match WebServiceContextAnnotationHelper.RELATIVE_JNDI_NAME
067                componentContext.put("env/WebServiceContext", new WebServiceContextReference());
068                
069                GeronimoUserTransaction userTransaction = new GeronimoUserTransaction(transactionManager);
070                try {
071                    this.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.portInfo = portInfo;
081            this.classLoader = classLoader;
082            this.endpointClassName = endpointClassName;
083            this.configurationBaseUrl = configurationBaseUrl;   
084            this.holder = holder;
085        }
086    
087        public WebServiceContainer getWebServiceContainer() {
088            POJOWebServiceContainer container = new POJOWebServiceContainer(portInfo, endpointClassName, classLoader, context, configurationBaseUrl, holder);
089            try {
090                container.init();
091            } catch (Exception e) {
092                throw new RuntimeException("Failure initializing web service containter", e);
093            }
094            return container;
095        }
096    
097        private static class WebServiceContextReference extends SimpleReference {
098            public Object getContent() throws NamingException {
099                return new POJOWebServiceContext();
100            }        
101        }
102        
103        public static final GBeanInfo GBEAN_INFO;
104    
105        static {
106            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(POJOWebServiceContainerFactoryGBean.class, NameFactory.GERONIMO_SERVICE);
107            infoBuilder.addAttribute("portInfo", org.apache.geronimo.jaxws.PortInfo.class, true, true);
108            infoBuilder.addAttribute("endpointClassName", String.class, true, true);
109            infoBuilder.addAttribute("classLoader", ClassLoader.class, false);
110            infoBuilder.addAttribute("componentContext", Map.class, true, true);
111            infoBuilder.addAttribute("kernel", Kernel.class, false);
112            infoBuilder.addReference("TransactionManager", TransactionManager.class, NameFactory.TRANSACTION_MANAGER);
113            infoBuilder.addAttribute("configurationBaseUrl", URL.class, true);
114            infoBuilder.addAttribute("holder", AnnotationHolder.class, true);
115    
116            infoBuilder.setConstructor(new String[]{"portInfo", "endpointClassName", "classLoader",
117                    "componentContext", "kernel", "TransactionManager", "configurationBaseUrl", "holder"});
118            GBEAN_INFO = infoBuilder.getBeanInfo();
119        }
120    
121        public static GBeanInfo getGBeanInfo() {
122            return GBEAN_INFO;
123        }
124    }