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.builder;
019    
020    import java.io.IOException;
021    import java.io.StringWriter;
022    import java.net.URI;
023    import java.util.Map;
024    
025    import javax.xml.namespace.QName;
026    
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    import org.apache.geronimo.common.DeploymentException;
030    import org.apache.geronimo.cxf.client.CXFServiceReference;
031    import org.apache.geronimo.gbean.GBeanInfo;
032    import org.apache.geronimo.gbean.GBeanInfoBuilder;
033    import org.apache.geronimo.j2ee.deployment.Module;
034    import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
035    import org.apache.geronimo.jaxws.builder.EndpointInfoBuilder;
036    import org.apache.geronimo.jaxws.builder.JAXWSServiceRefBuilder;
037    import org.apache.geronimo.jaxws.client.EndpointInfo;
038    import org.apache.geronimo.kernel.repository.Environment;
039    import org.apache.geronimo.naming.deployment.ServiceRefBuilder;
040    import org.apache.geronimo.xbeans.geronimo.naming.GerServiceRefType;
041    import org.apache.geronimo.xbeans.javaee.PortComponentRefType;
042    import org.apache.geronimo.xbeans.javaee.ServiceRefHandlerChainsType;
043    import org.apache.geronimo.xbeans.javaee.ServiceRefType;
044    import org.apache.xmlbeans.XmlOptions;
045    
046    public class CXFServiceRefBuilder extends JAXWSServiceRefBuilder {
047    
048        private static final Log LOG = LogFactory.getLog(CXFServiceRefBuilder.class);
049            
050        public CXFServiceRefBuilder(Environment defaultEnvironment,
051                                    String[] eeNamespaces) {
052            super(defaultEnvironment, eeNamespaces);
053        }
054           
055        public Object createService(ServiceRefType serviceRef, GerServiceRefType gerServiceRef, 
056                                    Module module, ClassLoader cl, Class serviceInterface, 
057                                    QName serviceQName, URI wsdlURI, Class serviceReference, 
058                                    Map<Class, PortComponentRefType> portComponentRefMap) throws DeploymentException {
059            EndpointInfoBuilder builder = new EndpointInfoBuilder(serviceInterface,
060                    gerServiceRef, portComponentRefMap, module.getModuleFile(),
061                    wsdlURI, serviceQName);
062            builder.build();
063    
064            wsdlURI = builder.getWsdlURI();
065            serviceQName = builder.getServiceQName();
066            Map<Object, EndpointInfo> seiInfoMap = builder.getEndpointInfo();
067    
068            String handlerChainsXML = null;
069            try {
070                handlerChainsXML = getHandlerChainAsString(serviceRef.getHandlerChains());
071            } catch (IOException e) {
072                // this should not happen
073                LOG.warn("Failed to serialize handler chains", e);
074            }
075    
076            String serviceReferenceName = (serviceReference == null) ? null : serviceReference.getName();
077            
078            return new CXFServiceReference(serviceInterface.getName(), serviceReferenceName,  wsdlURI,
079                    serviceQName, module.getModuleName(), handlerChainsXML, seiInfoMap);
080        }
081        
082        private static String getHandlerChainAsString(ServiceRefHandlerChainsType handlerChains)
083                throws IOException {
084            String xml = null;
085            if (handlerChains != null) {
086                StringWriter w = new StringWriter();
087                XmlOptions options = new XmlOptions();
088                options.setSaveSyntheticDocumentElement(new QName("http://java.sun.com/xml/ns/javaee",
089                                                                  "handler-chains"));
090                handlerChains.save(w, options);
091                xml = w.toString();
092            }
093            return xml;
094        }
095        
096        public static final GBeanInfo GBEAN_INFO;
097    
098        static {
099            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
100                    CXFServiceRefBuilder.class, NameFactory.MODULE_BUILDER);
101            infoBuilder.addInterface(ServiceRefBuilder.class);
102            infoBuilder.addAttribute("defaultEnvironment", Environment.class, true,
103                    true);
104            infoBuilder.addAttribute("eeNamespaces", String[].class, true, true);
105    
106            infoBuilder.setConstructor(new String[] { "defaultEnvironment",
107                                                      "eeNamespaces"});
108    
109            GBEAN_INFO = infoBuilder.getBeanInfo();
110        }
111    
112        public static GBeanInfo getGBeanInfo() {
113            return GBEAN_INFO;
114        }
115    }