1 package org.objectweb.celtix.geronimo.builder; 2 3 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 import java.net.URL ; 7 import java.util.ArrayList ; 8 import java.util.HashMap ; 9 import java.util.List ; 10 import java.util.Map ; 11 import java.util.jar.JarFile ; 12 import java.util.logging.Logger ; 13 14 import javax.xml.bind.JAXBContext; 15 import javax.xml.bind.JAXBElement; 16 import javax.xml.bind.JAXBException; 17 import javax.xml.bind.Unmarshaller; 18 import javax.xml.ws.handler.Handler; 19 20 import com.sun.java.xml.ns.j2ee.PortComponentType; 21 import com.sun.java.xml.ns.j2ee.WebserviceDescriptionType; 22 import com.sun.java.xml.ns.j2ee.WebservicesType; 23 24 import org.apache.geronimo.common.DeploymentException; 25 import org.apache.geronimo.gbean.GBeanData; 26 import org.apache.geronimo.gbean.GBeanInfo; 27 import org.apache.geronimo.gbean.GBeanInfoBuilder; 28 import org.apache.geronimo.j2ee.deployment.WebServiceBuilder; 29 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 30 import org.apache.geronimo.kernel.StoredObject; 31 import org.objectweb.celtix.Bus; 32 import org.objectweb.celtix.geronimo.container.CeltixWebServiceContainer; 33 34 35 public class CeltixBuilder implements WebServiceBuilder { 36 37 public static final GBeanInfo GBEAN_INFO; 38 static final String WEB_SERVICE_CONTAINER_ATTR = "webServiceContainer"; 39 private static final String POJO_CLASS_ATTR = "pojoClassName"; 40 private static final Logger LOG = Logger.getLogger(CeltixBuilder.class.getName()); 41 42 private final Bus bus; 43 private JAXBContext ctx; 44 45 46 static { 47 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(CeltixBuilder.class, 48 NameFactory.MODULE_BUILDER); 49 infoBuilder.addInterface(WebServiceBuilder.class); 50 GBEAN_INFO = infoBuilder.getBeanInfo(); 51 } 52 53 54 public CeltixBuilder() { 55 this(Bus.getCurrent()); 56 } 57 58 CeltixBuilder(Bus aBus) { 59 bus = aBus; 60 } 61 62 public static GBeanInfo getGBeanInfo() { 63 return GBEAN_INFO; 64 } 65 66 67 public Map <String , PortInfo> parseWebServiceDescriptor(URL wsDDUrl, JarFile moduleFile, boolean isEJB, 68 Map correctedPortLocations) 69 throws DeploymentException { 70 71 LOG.fine("parsing descriptor " + wsDDUrl); 72 73 Map <String , PortInfo> map = new HashMap <String , PortInfo>(); 74 75 try { 76 InputStream in = wsDDUrl.openStream(); 77 if (in == null) { 78 throw new DeploymentException("unable to read descriptor " + wsDDUrl); 79 } 80 81 Unmarshaller unmarshaller = getJAXBContext().createUnmarshaller(); 82 Object obj = unmarshaller.unmarshal(in); 83 84 WebservicesType wst = null; 85 if (obj instanceof JAXBElement) { 86 wst = (WebservicesType)((JAXBElement)obj).getValue(); 87 } 88 89 for (WebserviceDescriptionType desc : wst.getWebserviceDescription()) { 90 final String wsdlFile = desc.getWsdlFile().getValue(); 91 final String serviceName = desc.getWebserviceDescriptionName().getValue(); 92 93 for (PortComponentType port : desc.getPortComponent()) { 94 String servlet = port.getServiceImplBean().getServletLink().getValue(); 95 String sei = port.getServiceEndpointInterface().getValue(); 96 String portName = port.getPortComponentName().getValue(); 97 98 PortInfo portInfo = new PortInfo(); 99 100 portInfo.setServiceName(serviceName); 101 portInfo.setServletLink(servlet); 102 portInfo.setServiceEndpointInterfaceName(sei); 103 portInfo.setPortName(portName); 104 portInfo.setWsdlFile(wsdlFile); 105 portInfo.setHandlers(port.getHandler()); 106 107 map.put(servlet, portInfo); 108 } 109 } 110 111 return map; 112 113 } catch (IOException ex) { 114 ex.printStackTrace(); 115 throw new DeploymentException("unable to read " + wsDDUrl, ex); 116 } catch (JAXBException ex) { 117 ex.printStackTrace(); 118 throw new DeploymentException("unable to parse webservices.xml", ex); 119 } 120 } 121 122 123 public synchronized void configurePOJO(GBeanData targetGBean, JarFile moduleFile, 124 Object pi, String implClassName, ClassLoader classLoader) 125 throws DeploymentException { 126 127 assert pi instanceof PortInfo : "received incorrect portInfo object"; 128 129 ClassLoader orig = Thread.currentThread().getContextClassLoader(); 130 131 try { 132 Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); 133 PortInfo portInfo = (PortInfo)pi; 134 String seiClassName = portInfo.getServiceEndpointInterfaceName(); 135 136 LOG.info("configuring POJO webservice: " + pi + " sei: " + seiClassName); 137 138 loadSEI(seiClassName, classLoader); 140 targetGBean.setAttribute(POJO_CLASS_ATTR, implClassName); 141 143 buildHandlerChain(portInfo); 144 145 CeltixWebServiceContainer container = new CeltixWebServiceContainer(portInfo); 146 targetGBean.setAttribute(WEB_SERVICE_CONTAINER_ATTR, new StoredObject(container)); 147 148 } catch (IOException ex) { 149 throw new DeploymentException("unable to store CeltixWebServiceContainer", ex); 150 } finally { 151 Thread.currentThread().setContextClassLoader(orig); 152 } 153 } 154 155 156 public void configureEJB(GBeanData targetGBean, JarFile moduleFile, Object portInfo, 157 ClassLoader classLoader) 158 throws DeploymentException { 159 160 throw new DeploymentException("configureEJB NYI"); 161 } 162 163 public void doStart() throws Exception { 164 } 165 166 public void doStop() throws Exception { 167 } 168 169 public void doFail() { 170 172 } 173 174 private JAXBContext getJAXBContext() throws JAXBException { 175 if (ctx == null) { 176 ctx = JAXBContext.newInstance("com.sun.java.xml.ns.j2ee", getClass().getClassLoader()); 177 } 178 return ctx; 179 } 180 181 Class <?> loadSEI(String className, ClassLoader loader) throws DeploymentException { 182 try { 183 return loader.loadClass(className); 184 } catch (ClassNotFoundException ex) { 185 throw new DeploymentException("unable to load Service Endpoint Interface: " + className, ex); 186 } 187 } 188 189 private List <Handler> buildHandlerChain(PortInfo portInfo) { 190 return new ArrayList <Handler>(); 191 } 192 193 protected Bus getBus() { 194 return bus; 195 } 196 197 } 198 | Popular Tags |