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: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
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        private String contextRoot;
054    
055        public POJOWebServiceContainerFactoryGBean(org.apache.geronimo.jaxws.PortInfo portInfo,
056                                                   String endpointClassName,
057                                                   ClassLoader classLoader,
058                                                   Map componentContext,
059                                                   Kernel kernel,
060                                                   TransactionManager transactionManager,
061                                                   URL configurationBaseUrl,
062                                                   AnnotationHolder holder,
063                                                   String contextRoot)
064            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
065            
066            if (componentContext != null) {
067                
068                // The name should match WebServiceContextAnnotationHelper.RELATIVE_JNDI_NAME
069                componentContext.put("env/WebServiceContext", new WebServiceContextReference());
070                
071                GeronimoUserTransaction userTransaction = new GeronimoUserTransaction(transactionManager);
072                try {
073                    this.context = EnterpriseNamingContext.createEnterpriseNamingContext(componentContext,
074                            userTransaction,
075                            kernel,
076                            classLoader);
077                } catch (NamingException e) {
078                    LOG.warn("Failed to create naming context", e);
079                }
080            }
081            
082            this.portInfo = portInfo;
083            this.classLoader = classLoader;
084            this.endpointClassName = endpointClassName;
085            this.configurationBaseUrl = configurationBaseUrl;   
086            this.holder = holder;
087            this.contextRoot = contextRoot;
088        }
089    
090        public WebServiceContainer getWebServiceContainer() {
091            POJOWebServiceContainer container = new POJOWebServiceContainer(portInfo, endpointClassName, classLoader, 
092                                                                            context, configurationBaseUrl, holder, contextRoot);
093            try {
094                container.init();
095            } catch (Exception e) {
096                throw new RuntimeException("Failure initializing web service containter", e);
097            }
098            return container;
099        }
100    
101        private static class WebServiceContextReference extends SimpleReference {
102            public Object getContent() throws NamingException {
103                return new POJOWebServiceContext();
104            }        
105        }
106        
107        public static final GBeanInfo GBEAN_INFO;
108    
109        static {
110            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(POJOWebServiceContainerFactoryGBean.class, NameFactory.GERONIMO_SERVICE);
111            infoBuilder.addAttribute("portInfo", org.apache.geronimo.jaxws.PortInfo.class, true, true);
112            infoBuilder.addAttribute("endpointClassName", String.class, true, true);
113            infoBuilder.addAttribute("classLoader", ClassLoader.class, false);
114            infoBuilder.addAttribute("componentContext", Map.class, true, true);
115            infoBuilder.addAttribute("kernel", Kernel.class, false);
116            infoBuilder.addReference("TransactionManager", TransactionManager.class, NameFactory.JTA_RESOURCE);
117            infoBuilder.addAttribute("configurationBaseUrl", URL.class, true);
118            infoBuilder.addAttribute("holder", AnnotationHolder.class, true);
119            infoBuilder.addAttribute("contextRoot", String.class, true, true);
120    
121            infoBuilder.setConstructor(new String[]{
122                    "portInfo", 
123                    "endpointClassName", 
124                    "classLoader",
125                    "componentContext", 
126                    "kernel", 
127                    "TransactionManager", 
128                    "configurationBaseUrl", 
129                    "holder", 
130                    "contextRoot"
131            });
132            
133            GBEAN_INFO = infoBuilder.getBeanInfo();
134        }
135    
136        public static GBeanInfo getGBeanInfo() {
137            return GBEAN_INFO;
138        }
139    }