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.builder;
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.apache.geronimo.jaxws.builder.EndpointInfoBuilder;
023    import org.apache.geronimo.jaxws.builder.JAXWSServiceRefBuilder;
024    import org.apache.geronimo.jaxws.client.EndpointInfo;
025    import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
026    import org.apache.geronimo.kernel.GBeanNotFoundException;
027    import org.apache.geronimo.kernel.repository.Environment;
028    import org.apache.geronimo.xbeans.javaee.PortComponentRefType;
029    import org.apache.geronimo.xbeans.javaee.ServiceRefHandlerChainsType;
030    import org.apache.geronimo.xbeans.javaee.ServiceRefType;
031    import org.apache.geronimo.xbeans.geronimo.naming.GerServiceRefType;
032    import org.apache.geronimo.j2ee.deployment.EARContext;
033    import org.apache.geronimo.j2ee.deployment.Module;
034    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
035    import org.apache.geronimo.axis2.client.Axis2ConfigGBean;
036    import org.apache.geronimo.axis2.client.Axis2ServiceReference;
037    import org.apache.geronimo.common.DeploymentException;
038    import org.apache.geronimo.gbean.AbstractName;
039    import org.apache.geronimo.gbean.GBeanData;
040    import org.apache.geronimo.gbean.GBeanInfo;
041    import org.apache.geronimo.gbean.GBeanInfoBuilder;
042    import org.apache.geronimo.naming.deployment.ServiceRefBuilder;
043    import org.apache.xmlbeans.XmlOptions;
044    
045    import javax.xml.namespace.QName;
046    
047    import java.io.IOException;
048    import java.io.StringWriter;
049    import java.net.URI;
050    import java.util.Map;
051    
052    public class Axis2ServiceRefBuilder extends JAXWSServiceRefBuilder {
053    
054        private static final Log log = LogFactory.getLog(Axis2ServiceRefBuilder.class);
055        
056        public Axis2ServiceRefBuilder(Environment defaultEnvironment,
057                                     String[] eeNamespaces) {
058            super(defaultEnvironment, eeNamespaces);
059        }
060    
061        public Object createService(ServiceRefType serviceRef, GerServiceRefType gerServiceRef,
062                                    Module module, ClassLoader cl, Class serviceInterfaceClass,
063                                    QName serviceQName, URI wsdlURI, Class serviceReferenceType,
064                                    Map<Class, PortComponentRefType> portComponentRefMap) throws DeploymentException {
065            registerConfigGBean(module);
066            EndpointInfoBuilder builder = new EndpointInfoBuilder(serviceInterfaceClass,
067                    gerServiceRef, portComponentRefMap, module.getModuleFile(),                
068                    wsdlURI, serviceQName);
069            builder.build();
070    
071            wsdlURI = builder.getWsdlURI();
072            serviceQName = builder.getServiceQName();
073            Map<Object, EndpointInfo> seiInfoMap = builder.getEndpointInfo();
074    
075            String handlerChainsXML = null;
076            try {
077                handlerChainsXML = getHandlerChainAsString(serviceRef.getHandlerChains());
078            } catch (IOException e) {
079                // this should not happen
080                log.warn("Failed to serialize handler chains", e);
081            }
082    
083            String serviceReferenceName = (serviceReferenceType == null) ? null : serviceReferenceType.getName();
084            return new Axis2ServiceReference(serviceInterfaceClass.getName(), serviceReferenceName,  wsdlURI,
085                    serviceQName, module.getModuleName(), handlerChainsXML, seiInfoMap);
086        }
087    
088        private static String getHandlerChainAsString(ServiceRefHandlerChainsType handlerChains)
089                throws IOException {
090            String xml = null;
091            if (handlerChains != null) {
092                StringWriter w = new StringWriter();
093                XmlOptions options = new XmlOptions();
094                options.setSaveSyntheticDocumentElement(new QName("http://java.sun.com/xml/ns/javaee",
095                                                                  "handler-chains"));
096                handlerChains.save(w, options);
097                xml = w.toString();
098            }
099            return xml;
100        }
101        
102        private void registerConfigGBean(Module module) throws DeploymentException {
103            EARContext context = module.getEarContext();
104            AbstractName containerFactoryName = context.getNaming().createChildName(
105                    module.getModuleName(), Axis2ConfigGBean.GBEAN_INFO.getName(),
106                    NameFactory.GERONIMO_SERVICE);
107    
108            try {
109                context.getGBeanInstance(containerFactoryName);
110            } catch (GBeanNotFoundException e1) {
111                GBeanData configGBeanData = new GBeanData(containerFactoryName, Axis2ConfigGBean.GBEAN_INFO);
112                configGBeanData.setAttribute("moduleName", module.getModuleName());
113                
114                try {
115                    context.addGBean(configGBeanData);
116                } catch (GBeanAlreadyExistsException e) {
117                    throw new DeploymentException("Could not add config gbean", e);
118                }
119            }
120        }
121        
122        public static final GBeanInfo GBEAN_INFO;
123    
124        static {
125            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
126                    Axis2ServiceRefBuilder.class, NameFactory.MODULE_BUILDER);
127            infoBuilder.addInterface(ServiceRefBuilder.class);
128            infoBuilder.addAttribute("defaultEnvironment", Environment.class, true,
129                    true);
130            infoBuilder.addAttribute("eeNamespaces", String[].class, true, true);
131    
132            infoBuilder.setConstructor(new String[] { "defaultEnvironment",
133                                                      "eeNamespaces" });
134    
135            GBEAN_INFO = infoBuilder.getBeanInfo();
136        }
137    
138        public static GBeanInfo getGBeanInfo() {
139            return Axis2ServiceRefBuilder.GBEAN_INFO;
140        }
141    }