Home » geronimo-2.2-source-release » org.apache.geronimo.cxf.builder » [javadoc | source]

    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.geronimo.cxf.builder;
   18   
   19   import java.io.FileNotFoundException;
   20   import java.io.IOException;
   21   import java.io.InputStream;
   22   import java.net.URL;
   23   import java.util.Collection;
   24   import java.util.Collections;
   25   import java.util.HashMap;
   26   import java.util.Map;
   27   import java.util.jar.JarFile;
   28   
   29   import javax.xml.bind.JAXBContext;
   30   import javax.xml.bind.JAXBElement;
   31   import javax.xml.bind.JAXBException;
   32   import javax.xml.bind.Unmarshaller;
   33   import javax.xml.transform.stream.StreamSource;
   34   
   35   import org.slf4j.Logger;
   36   import org.slf4j.LoggerFactory;
   37   import org.apache.cxf.jaxws.javaee.HandlerChainsType;
   38   import org.apache.cxf.jaxws.javaee.PortComponentType;
   39   import org.apache.cxf.jaxws.javaee.ServiceImplBeanType;
   40   import org.apache.cxf.jaxws.javaee.WebserviceDescriptionType;
   41   import org.apache.cxf.jaxws.javaee.WebservicesType;
   42   import org.apache.cxf.jaxws.support.JaxWsImplementorInfo;
   43   import org.apache.geronimo.common.DeploymentException;
   44   import org.apache.geronimo.cxf.pojo.POJOWebServiceContainerFactoryGBean;
   45   import org.apache.geronimo.gbean.GBeanData;
   46   import org.apache.geronimo.gbean.GBeanInfo;
   47   import org.apache.geronimo.gbean.GBeanInfoBuilder;
   48   import org.apache.geronimo.j2ee.deployment.Module;
   49   import org.apache.geronimo.j2ee.deployment.WebServiceBuilder;
   50   import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
   51   import org.apache.geronimo.jaxws.JAXWSUtils;
   52   import org.apache.geronimo.jaxws.PortInfo;
   53   import org.apache.geronimo.jaxws.builder.JAXWSServiceBuilder;
   54   import org.apache.geronimo.jaxws.builder.WARWebServiceFinder;
   55   import org.apache.geronimo.jaxws.wsdl.WsdlGenerator;
   56   import org.apache.geronimo.jaxws.wsdl.WsdlGeneratorOptions;
   57   import org.apache.geronimo.kernel.repository.Environment;
   58   
   59   public class CXFBuilder extends JAXWSServiceBuilder {
   60       private static final Logger LOG = LoggerFactory.getLogger(CXFBuilder.class);
   61       
   62       /**
   63        * This property if enabled will cause the Sun wsgen tool to be used to 
   64        * generate the WSDL for servies without WSDL. By default CXF tooling
   65        * will be used the generate the WSDL.
   66        */
   67       private static final String USE_WSGEN_PROPERTY = 
   68           "org.apache.geronimo.cxf.use.wsgen";
   69   
   70       protected Collection<WsdlGenerator> wsdlGenerators;
   71       
   72       public CXFBuilder() {
   73           super(null);
   74       }
   75   
   76       public CXFBuilder(Environment defaultEnvironment, Collection<WsdlGenerator> wsdlGenerators) {
   77           super(defaultEnvironment);
   78           this.wsdlGenerators = wsdlGenerators;
   79           this.webServiceFinder = new WARWebServiceFinder();
   80       }
   81   
   82       protected GBeanInfo getContainerFactoryGBeanInfo() {
   83           return POJOWebServiceContainerFactoryGBean.GBEAN_INFO;
   84       }
   85   
   86       protected Map<String, PortInfo> parseWebServiceDescriptor(InputStream in,
   87                                                                 URL wsDDUrl,
   88                                                                 JarFile moduleFile,
   89                                                                 boolean isEJB,
   90                                                                 Map correctedPortLocations)
   91               throws DeploymentException {
   92   
   93           LOG.debug("Parsing descriptor " + wsDDUrl);
   94   
   95           Map<String, PortInfo> map = null;
   96   
   97           try {
   98               JAXBContext ctx = JAXBContext.newInstance(WebservicesType.class);
   99               Unmarshaller unmarshaller = ctx.createUnmarshaller();
  100               Object obj = unmarshaller.unmarshal(new StreamSource(in), WebservicesType.class);
  101   
  102               if (obj instanceof JAXBElement) {
  103                   obj = ((JAXBElement) obj).getValue();
  104               }
  105   
  106               if (!(obj instanceof WebservicesType)) {
  107                   return map;
  108               }
  109               WebservicesType wst = (WebservicesType) obj;
  110   
  111               for (WebserviceDescriptionType desc : wst.getWebserviceDescription()) {
  112                   String wsdlFile = null;
  113                   if (desc.getWsdlFile() != null) {
  114                       wsdlFile = getString(desc.getWsdlFile().getValue());
  115                   }
  116   
  117                   String serviceName = desc.getWebserviceDescriptionName().getValue();
  118   
  119                   for (PortComponentType port : desc.getPortComponent()) {
  120   
  121                       PortInfo portInfo = new PortInfo();
  122   
  123                       String serviceLink = null;
  124                       ServiceImplBeanType beanType = port.getServiceImplBean();
  125                       if (beanType.getEjbLink() != null) {
  126                           serviceLink = beanType.getEjbLink().getValue();
  127                       } else if (beanType.getServletLink().getValue() != null) {
  128                           serviceLink = beanType.getServletLink().getValue();
  129                       }
  130                       portInfo.setServiceLink(serviceLink);
  131   
  132                       if (port.getServiceEndpointInterface() != null) {
  133                           String sei = port.getServiceEndpointInterface().getValue();
  134                           portInfo.setServiceEndpointInterfaceName(sei);
  135                       }
  136   
  137                       String portName = port.getPortComponentName().getValue();
  138                       portInfo.setPortName(portName);
  139   
  140                       portInfo.setProtocolBinding(port.getProtocolBinding());
  141                       portInfo.setServiceName(serviceName);
  142                       portInfo.setWsdlFile(wsdlFile);
  143   
  144                       if (port.getEnableMtom() != null) {
  145                           portInfo.setEnableMTOM(port.getEnableMtom().isValue());
  146                       }
  147   
  148                       portInfo.setHandlers(HandlerChainsType.class, port.getHandlerChains());
  149   
  150                       if (port.getWsdlPort() != null) {
  151                           portInfo.setWsdlPort(port.getWsdlPort().getValue());
  152                       }
  153   
  154                       if (port.getWsdlService() != null) {
  155                           portInfo.setWsdlService(port.getWsdlService().getValue());
  156                       }
  157   
  158                       String location = (String) correctedPortLocations.get(serviceLink);
  159                       portInfo.setLocation(location);
  160   
  161                       if (map == null) {
  162                           map = new HashMap<String, PortInfo>();
  163                       }
  164                       map.put(serviceLink, portInfo);
  165                   }
  166               }
  167   
  168               return map;
  169           } catch (FileNotFoundException e) {
  170               return Collections.emptyMap();
  171           } catch (IOException ex) {
  172               throw new DeploymentException("Unable to read " + wsDDUrl, ex);
  173           } catch (JAXBException ex) {
  174               throw new DeploymentException("Unable to parse " + wsDDUrl, ex);
  175           } catch (Exception ex) {
  176               throw new DeploymentException("Unknown deployment error", ex);
  177           } finally {
  178               try {
  179                   in.close();
  180               } catch (IOException e) {
  181                   // ignore
  182               }
  183           }
  184       }
  185           
  186       private static String getString(String in) {
  187           if (in != null) {
  188               in = in.trim();
  189               if (in.length() == 0) {
  190                   return null;
  191               }
  192           }
  193           return in;
  194       }
  195           
  196       @Override
  197       protected void initialize(GBeanData targetGBean, Class serviceClass, PortInfo portInfo, Module module) 
  198           throws DeploymentException {  
  199           if (Boolean.getBoolean(USE_WSGEN_PROPERTY)) {
  200               generateWSDL(serviceClass, portInfo, module);
  201           }
  202       }
  203       
  204       protected WsdlGenerator getWsdlGenerator() throws DeploymentException {
  205           if (this.wsdlGenerators == null || this.wsdlGenerators.isEmpty()) {
  206               throw new DeploymentException("Wsdl generator not found");
  207           } else {
  208               return this.wsdlGenerators.iterator().next();
  209           }
  210       }
  211       
  212       private void generateWSDL(Class serviceClass, PortInfo portInfo, Module module) 
  213           throws DeploymentException {
  214           String serviceName = (portInfo.getServiceName() == null ? serviceClass.getName() : portInfo.getServiceName());
  215           if (isWsdlSet(portInfo, serviceClass)) {
  216               LOG.debug("Service " + serviceName + " has WSDL.");
  217               return;
  218           }        
  219           
  220           if (isHTTPBinding(portInfo, serviceClass)) {
  221               LOG.debug("Service " + serviceName + " has HTTPBinding.");
  222               return;
  223           }
  224           
  225           if (JAXWSUtils.isWebServiceProvider(serviceClass)) {            
  226               throw new DeploymentException("WSDL must be specified for @WebServiceProvider service " + serviceName);
  227           }
  228           
  229           LOG.debug("Service " + serviceName + " does not have WSDL. Generating WSDL...");
  230           
  231           WsdlGenerator wsdlGenerator = getWsdlGenerator();
  232           
  233           WsdlGeneratorOptions options = new WsdlGeneratorOptions();
  234           options.setSAAJ(WsdlGeneratorOptions.SAAJ.SUN);
  235           
  236           JaxWsImplementorInfo serviceInfo = new JaxWsImplementorInfo(serviceClass);
  237           
  238           // set wsdl service
  239           if (portInfo.getWsdlService() == null) {
  240               options.setWsdlService(serviceInfo.getServiceName());
  241           } else {
  242               options.setWsdlService(portInfo.getWsdlService());
  243           }
  244           
  245           // set wsdl port
  246           if (portInfo.getWsdlPort() != null) {
  247               options.setWsdlPort(portInfo.getWsdlPort());
  248           }
  249                           
  250           String wsdlFile = wsdlGenerator.generateWsdl(module, serviceClass.getName(), module.getEarContext(), options);
  251           portInfo.setWsdlFile(wsdlFile);
  252           
  253           LOG.debug("Generated " + wsdlFile + " for service " + serviceName);
  254       }     
  255       
  256       public static final GBeanInfo GBEAN_INFO;
  257   
  258       static {
  259           GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(CXFBuilder.class, NameFactory.MODULE_BUILDER);
  260           infoBuilder.addInterface(WebServiceBuilder.class);
  261           infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true);
  262           infoBuilder.addReference("WsdlGenerator", WsdlGenerator.class, GBeanInfoBuilder.DEFAULT_J2EE_TYPE);
  263           infoBuilder.setConstructor(new String[]{"defaultEnvironment", "WsdlGenerator"});
  264   
  265           GBEAN_INFO = infoBuilder.getBeanInfo();
  266       }
  267   
  268       public static GBeanInfo getGBeanInfo() {
  269           return GBEAN_INFO;
  270       }
  271   
  272   }

Home » geronimo-2.2-source-release » org.apache.geronimo.cxf.builder » [javadoc | source]