1 17 package org.apache.geronimo.cxf.builder; 18 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.FileNotFoundException ; 23 import java.net.URL ; 24 import java.net.MalformedURLException ; 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Collections ; 30 import java.util.jar.JarFile ; 31 import java.util.logging.Logger ; 32 33 import javax.xml.bind.JAXBContext; 34 import javax.xml.bind.JAXBElement; 35 import javax.xml.bind.JAXBException; 36 import javax.xml.bind.Unmarshaller; 37 import javax.xml.ws.handler.Handler; 38 39 import org.apache.cxf.jaxws.javaee.PortComponentType; 40 import org.apache.cxf.jaxws.javaee.WebserviceDescriptionType; 41 import org.apache.cxf.jaxws.javaee.WebservicesType; 42 43 import org.apache.geronimo.common.DeploymentException; 44 import org.apache.geronimo.gbean.GBeanData; 45 import org.apache.geronimo.gbean.GBeanInfo; 46 import org.apache.geronimo.gbean.GBeanInfoBuilder; 47 import org.apache.geronimo.gbean.AbstractName; 48 import org.apache.geronimo.j2ee.deployment.WebServiceBuilder; 49 import org.apache.geronimo.j2ee.deployment.WebModule; 50 import org.apache.geronimo.j2ee.deployment.Module; 51 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 52 import org.apache.geronimo.cxf.PortInfo; 53 import org.apache.geronimo.cxf.CXFWebServiceContainerFactoryGBean; 54 import org.apache.geronimo.kernel.repository.Environment; 55 import org.apache.geronimo.kernel.GBeanAlreadyExistsException; 56 import org.apache.geronimo.deployment.DeploymentContext; 57 import org.apache.geronimo.deployment.service.EnvironmentBuilder; 58 import org.apache.geronimo.deployment.util.DeploymentUtil; 59 60 61 public class CXFBuilder implements WebServiceBuilder { 62 63 private static final Logger LOG = Logger.getLogger(CXFBuilder.class.getName()); 64 65 private final Environment defaultEnvironment; 66 private static final String KEY = CXFBuilder.class.getName(); 67 private JAXBContext ctx; 68 69 70 public CXFBuilder(Environment defaultEnvironment) { 71 this.defaultEnvironment = defaultEnvironment; 72 } 73 74 public void findWebServices(JarFile moduleFile, boolean isEJB, Map servletLocations, Environment environment, Map sharedContext) throws DeploymentException { 75 final String path = isEJB ? "META-INF/webservices.xml" : "WEB-INF/webservices.xml"; 76 try { 77 URL wsDDUrl = DeploymentUtil.createJarURL(moduleFile, path); 78 Map portMap = parseWebServiceDescriptor(wsDDUrl, moduleFile, isEJB, servletLocations); 79 if (portMap != null) { 80 EnvironmentBuilder.mergeEnvironments(environment, defaultEnvironment); 81 sharedContext.put(KEY, portMap); 82 } 83 } catch (MalformedURLException e) { 84 } 86 } 87 88 private Map <String , PortInfo> parseWebServiceDescriptor(URL wsDDUrl, JarFile moduleFile, boolean isEJB, Map correctedPortLocations) throws DeploymentException { 89 90 LOG.fine("parsing descriptor " + wsDDUrl); 91 92 Map <String , PortInfo> map = new HashMap <String , PortInfo>(); 93 94 try { 95 InputStream in = wsDDUrl.openStream(); 96 if (in == null) { 97 throw new DeploymentException("unable to read descriptor " + wsDDUrl); 98 } 99 100 Unmarshaller unmarshaller = getJAXBContext().createUnmarshaller(); 101 Object obj = unmarshaller.unmarshal(in); 102 103 WebservicesType wst = null; 104 if (obj instanceof JAXBElement) { 105 wst = (WebservicesType) ((JAXBElement) obj).getValue(); 106 } 107 108 for (WebserviceDescriptionType desc : wst.getWebserviceDescription()) { 109 final String wsdlFile = desc.getWsdlFile().getValue(); 110 final String serviceName = desc.getWebserviceDescriptionName().getValue(); 111 112 for (PortComponentType port : desc.getPortComponent()) { 113 String servlet = port.getServiceImplBean().getServletLink().getValue(); 114 String sei = port.getServiceEndpointInterface().getValue(); 115 String portName = port.getPortComponentName().getValue(); 116 117 PortInfo portInfo = new PortInfo(); 118 119 portInfo.setServiceName(serviceName); 120 portInfo.setServletLink(servlet); 121 portInfo.setServiceEndpointInterfaceName(sei); 122 portInfo.setPortName(portName); 123 portInfo.setWsdlFile(wsdlFile); 124 portInfo.setHandlers(port.getHandler()); 125 126 map.put(servlet, portInfo); 127 } 128 } 129 130 return map; 131 } catch (FileNotFoundException e) { 132 return Collections.EMPTY_MAP; 133 } catch (IOException ex) { 134 ex.printStackTrace(); 135 throw new DeploymentException("unable to read " + wsDDUrl, ex); 136 } catch (JAXBException ex) { 137 throw new DeploymentException("unable to parse webservices.xml", ex); 138 } 139 } 140 141 142 public boolean configurePOJO(GBeanData targetGBean, String servletName, Module module, String seiClassName, DeploymentContext context) throws DeploymentException { 143 144 146 Map sharedContext = ((WebModule) module).getSharedContext(); 147 Map portInfoMap = (Map ) sharedContext.get(KEY); 148 PortInfo portInfo = (PortInfo) portInfoMap.get(servletName); 149 if (portInfo == null) { 150 return false; 152 } 153 154 155 LOG.info("configuring POJO webservice: " + servletName + " sei: " + seiClassName); 156 157 ClassLoader classLoader = context.getClassLoader(); 159 loadSEI(seiClassName, classLoader); 160 161 buildHandlerChain(portInfo); 162 AbstractName containerFactoryName = context.getNaming().createChildName(targetGBean.getAbstractName(), "cxfWebServiceContainerFactory", NameFactory.GERONIMO_SERVICE); 163 GBeanData containerFactoryData = new GBeanData(containerFactoryName, CXFWebServiceContainerFactoryGBean.GBEAN_INFO); 164 containerFactoryData.setAttribute("portInfo", portInfo); 165 containerFactoryData.setAttribute("endpointClassName", seiClassName); 166 try { 167 context.addGBean(containerFactoryData); 168 } catch (GBeanAlreadyExistsException e) { 169 throw new DeploymentException("Could not add web service container factory gbean", e); 170 } 171 172 targetGBean.setReferencePattern("WebServiceContainerFactory", containerFactoryName); 173 targetGBean.setAttribute("pojoClassName", seiClassName); 174 return true; 175 } 176 177 178 public boolean configureEJB(GBeanData targetGBean, String ejbName, JarFile moduleFile, Map sharedContext, ClassLoader classLoader) throws DeploymentException { 179 throw new DeploymentException("configureEJB NYI"); 180 } 181 182 private JAXBContext getJAXBContext() throws JAXBException { 183 if (ctx == null) { 184 ctx = JAXBContext.newInstance("com.sun.java.xml.ns.j2ee", getClass().getClassLoader()); 185 } 186 return ctx; 187 } 188 189 Class <?> loadSEI(String className, ClassLoader loader) throws DeploymentException { 190 try { 191 return loader.loadClass(className); 192 } catch (ClassNotFoundException ex) { 193 throw new DeploymentException("unable to load Service Endpoint Interface: " + className, ex); 194 } 195 } 196 197 private List <Handler> buildHandlerChain(PortInfo portInfo) { 198 return new ArrayList <Handler>(); 199 } 200 201 public static final GBeanInfo GBEAN_INFO; 202 203 static { 204 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(CXFBuilder.class, NameFactory.MODULE_BUILDER); 205 infoBuilder.addInterface(WebServiceBuilder.class); 206 infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true); 207 208 infoBuilder.setConstructor(new String []{"defaultEnvironment"}); 209 210 GBEAN_INFO = infoBuilder.getBeanInfo(); 211 } 212 213 public static GBeanInfo getGBeanInfo() { 214 return GBEAN_INFO; 215 } 216 217 } 218 | Popular Tags |