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    package org.apache.geronimo.axis.builder;
018    
019    import java.net.URL;
020    import java.util.Collection;
021    import java.util.HashMap;
022    import java.util.List;
023    import java.util.Map;
024    import java.util.jar.JarFile;
025    
026    import org.apache.geronimo.axis.server.EjbWebServiceGBean;
027    import org.apache.geronimo.common.DeploymentException;
028    import org.apache.geronimo.deployment.ModuleIDBuilder;
029    import org.apache.geronimo.deployment.service.EnvironmentBuilder;
030    import org.apache.geronimo.gbean.AbstractName;
031    import org.apache.geronimo.gbean.AbstractNameQuery;
032    import org.apache.geronimo.gbean.GBeanData;
033    import org.apache.geronimo.gbean.GBeanInfo;
034    import org.apache.geronimo.gbean.GBeanInfoBuilder;
035    import org.apache.geronimo.j2ee.deployment.EARContext;
036    import org.apache.geronimo.j2ee.deployment.Module;
037    import org.apache.geronimo.j2ee.deployment.ModuleBuilderExtension;
038    import org.apache.geronimo.j2ee.deployment.WebServiceBuilder;
039    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
040    import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
041    import org.apache.geronimo.kernel.Naming;
042    import org.apache.geronimo.kernel.config.ConfigurationModuleType;
043    import org.apache.geronimo.kernel.config.ConfigurationStore;
044    import org.apache.geronimo.kernel.repository.Environment;
045    import org.apache.geronimo.openejb.deployment.EjbModule;
046    import org.apache.openejb.assembler.classic.EnterpriseBeanInfo;
047    import org.apache.openejb.jee.oejb2.GeronimoEjbJarType;
048    import org.apache.openejb.jee.oejb2.WebServiceBindingType;
049    import org.apache.openejb.jee.oejb2.WebServiceBindingType.WebServiceSecurityType;
050    
051    /**
052     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
053     */
054    public class AxisModuleBuilderExtension implements ModuleBuilderExtension {
055    
056        private WebServiceBuilder axisBuilder;
057        private Environment defaultEnvironment;
058        private AbstractNameQuery listener;   
059    
060        public AxisModuleBuilderExtension() {
061            this(null, null, null);
062        }
063    
064        public AxisModuleBuilderExtension(WebServiceBuilder wsBuilder,
065                                          Environment defaultEnvironment,
066                                          AbstractNameQuery listener) {
067            this.axisBuilder = wsBuilder;
068            this.defaultEnvironment = defaultEnvironment;
069            this.listener = listener;
070        }
071    
072        public void createModule(Module module, Object plan, JarFile moduleFile, String targetPath, URL specDDUrl, Environment environment, Object moduleContextInfo, AbstractName earName, Naming naming, ModuleIDBuilder idBuilder) throws DeploymentException {
073            if (module.getType() != ConfigurationModuleType.EJB) {
074                return;
075            }
076    
077            EjbModule ejbModule = (EjbModule) module;
078            
079            //overridden web service locations
080            Map correctedPortLocations = new HashMap();         
081            GeronimoEjbJarType geronimoEjbJarType = 
082                (GeronimoEjbJarType) ejbModule.getEjbModule().getAltDDs().get("geronimo-openejb.xml");
083            if (geronimoEjbJarType != null) {
084               for (WebServiceBindingType bt : geronimoEjbJarType.getWebServiceBinding()) {
085                   String location = bt.getWebServiceAddress();
086                   if (location != null) {
087                       location = location.trim();
088                       if (!location.startsWith("/")) {
089                           location = "/" + location;
090                       }
091                       correctedPortLocations.put(bt.getEjbName(), location);
092                   }
093               }
094            }
095    
096            axisBuilder.findWebServices(module, true, correctedPortLocations, environment, ejbModule.getSharedContext());
097            
098            if (this.defaultEnvironment != null) {
099                EnvironmentBuilder.mergeEnvironments(environment, this.defaultEnvironment);
100            } 
101        }
102    
103        public void installModule(JarFile earFile, EARContext earContext, Module module, Collection configurationStores, ConfigurationStore targetConfigurationStore, Collection repository) throws DeploymentException {
104        }
105    
106        public void initContext(EARContext earContext, Module module, ClassLoader cl) throws DeploymentException {
107        }
108    
109        public void addGBeans(EARContext earContext, Module module, ClassLoader cl, Collection repository) throws DeploymentException {
110            if (module.getType() != ConfigurationModuleType.EJB) {
111                return;
112            }
113    
114            EjbModule ejbModule = (EjbModule) module;
115            
116            Map<String, WebServiceBindingType> wsBindingMap = 
117                createWebServiceBindingMap(ejbModule);
118    
119            for (EnterpriseBeanInfo bean : ejbModule.getEjbJarInfo().enterpriseBeans) {
120                if (bean.type != EnterpriseBeanInfo.STATELESS) {
121                    continue;
122                }
123                String ejbName = bean.ejbName;
124    
125                AbstractName sessionName = earContext.getNaming().createChildName(module.getModuleName(), ejbName, NameFactory.STATELESS_SESSION_BEAN);
126    
127                assert sessionName != null: "StatelesSessionBean object name is null";
128    
129                AbstractName ejbWebServiceName = earContext.getNaming().createChildName(sessionName, ejbName, NameFactory.WEB_SERVICE_LINK);
130    
131                GBeanData ejbWebServiceGBean = new GBeanData(ejbWebServiceName, EjbWebServiceGBean.GBEAN_INFO);
132    
133                ejbWebServiceGBean.setAttribute("ejbName", ejbName);
134                ejbWebServiceGBean.setAttribute("ejbClass", bean.ejbClass);
135                
136                WebServiceBindingType wsBinding = wsBindingMap.get(ejbName);
137                if (wsBinding != null) {
138                    List<String> ddVirtualHosts = wsBinding.getWebServiceVirtualHost();
139                    if (ddVirtualHosts != null) {                    
140                        String[] virtualHosts = new String[ddVirtualHosts.size()];
141                        for (int i=0; i<ddVirtualHosts.size(); i++) {                    
142                            virtualHosts[i] = ddVirtualHosts.get(i).trim();
143                        }
144                        ejbWebServiceGBean.setAttribute("virtualHosts", virtualHosts);
145                    }
146                    
147                    WebServiceSecurityType wsSecurity = wsBinding.getWebServiceSecurity();
148                    if (wsSecurity != null) {
149                        ejbWebServiceGBean.setAttribute("securityRealmName", wsSecurity.getSecurityRealmName().trim());
150                        ejbWebServiceGBean.setAttribute("transportGuarantee", wsSecurity.getTransportGuarantee().toString());
151                        ejbWebServiceGBean.setAttribute("authMethod", wsSecurity.getAuthMethod().value());
152                        if (wsSecurity.getRealmName() != null) {
153                            ejbWebServiceGBean.setAttribute("realmName", wsSecurity.getRealmName().trim());                    
154                        }
155                    }
156                }
157                
158                if (axisBuilder.configureEJB(ejbWebServiceGBean, ejbName, ejbModule,
159                                             ejbModule.getSharedContext(), cl)) {
160                    
161                    try {
162                        earContext.addGBean(ejbWebServiceGBean);
163                    } catch (GBeanAlreadyExistsException e) {
164                        throw new DeploymentException(
165                                "Could not add axis ejb web service gbean to context", e);
166                    }
167                    
168                    if (this.listener != null) {
169                        ejbWebServiceGBean.setReferencePattern("WebServiceContainer", this.listener);
170                    }
171                    
172                    ejbWebServiceGBean.setReferencePattern("EjbDeployment", sessionName);
173                }
174    
175                ejbWebServiceGBean.clearAttribute("ejbName");
176                ejbWebServiceGBean.clearAttribute("ejbClass");
177                
178            }
179        }
180    
181        private Map<String, WebServiceBindingType> createWebServiceBindingMap(EjbModule ejbModule) {
182            Map<String, WebServiceBindingType> wsBindingMap = 
183                new HashMap<String, WebServiceBindingType>();
184            GeronimoEjbJarType geronimoEjbJarType = 
185                (GeronimoEjbJarType) ejbModule.getEjbModule().getAltDDs().get("geronimo-openejb.xml");
186            if (geronimoEjbJarType != null) {
187                for (WebServiceBindingType bt : geronimoEjbJarType.getWebServiceBinding()) {
188                    wsBindingMap.put(bt.getEjbName(), bt);
189                }
190            }
191            return wsBindingMap;
192        }
193        
194        public static final GBeanInfo GBEAN_INFO;
195    
196        static {
197            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(AxisModuleBuilderExtension.class, NameFactory.MODULE_BUILDER);
198            infoBuilder.addInterface(ModuleBuilderExtension.class);
199            infoBuilder.addReference("WebServiceBuilder", WebServiceBuilder.class, NameFactory.MODULE_BUILDER);
200            infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true);
201            infoBuilder.addAttribute("listener", AbstractNameQuery.class, true);
202    
203            infoBuilder.setConstructor(new String[]{"WebServiceBuilder","defaultEnvironment","listener"});
204    
205            GBEAN_INFO = infoBuilder.getBeanInfo();
206        }
207    
208        public static GBeanInfo getGBeanInfo() {
209            return GBEAN_INFO;
210        }
211    
212    }