001: package org.objectweb.celtix.geronimo.builder;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.net.URL;
006: import java.util.ArrayList;
007: import java.util.HashMap;
008: import java.util.List;
009: import java.util.Map;
010: import java.util.jar.JarFile;
011: import java.util.logging.Logger;
012:
013: import javax.xml.bind.JAXBContext;
014: import javax.xml.bind.JAXBElement;
015: import javax.xml.bind.JAXBException;
016: import javax.xml.bind.Unmarshaller;
017: import javax.xml.ws.handler.Handler;
018:
019: import com.sun.java.xml.ns.j2ee.PortComponentType;
020: import com.sun.java.xml.ns.j2ee.WebserviceDescriptionType;
021: import com.sun.java.xml.ns.j2ee.WebservicesType;
022:
023: import org.apache.geronimo.common.DeploymentException;
024: import org.apache.geronimo.gbean.GBeanData;
025: import org.apache.geronimo.gbean.GBeanInfo;
026: import org.apache.geronimo.gbean.GBeanInfoBuilder;
027: import org.apache.geronimo.j2ee.deployment.WebServiceBuilder;
028: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
029: import org.apache.geronimo.kernel.StoredObject;
030: import org.objectweb.celtix.Bus;
031: import org.objectweb.celtix.geronimo.container.CeltixWebServiceContainer;
032:
033: public class CeltixBuilder implements WebServiceBuilder {
034:
035: public static final GBeanInfo GBEAN_INFO;
036: static final String WEB_SERVICE_CONTAINER_ATTR = "webServiceContainer";
037: private static final String POJO_CLASS_ATTR = "pojoClassName";
038: private static final Logger LOG = Logger
039: .getLogger(CeltixBuilder.class.getName());
040:
041: private final Bus bus;
042: private JAXBContext ctx;
043:
044: static {
045: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
046: CeltixBuilder.class, NameFactory.MODULE_BUILDER);
047: infoBuilder.addInterface(WebServiceBuilder.class);
048: GBEAN_INFO = infoBuilder.getBeanInfo();
049: }
050:
051: public CeltixBuilder() {
052: this (Bus.getCurrent());
053: }
054:
055: CeltixBuilder(Bus aBus) {
056: bus = aBus;
057: }
058:
059: public static GBeanInfo getGBeanInfo() {
060: return GBEAN_INFO;
061: }
062:
063: public Map<String, PortInfo> parseWebServiceDescriptor(URL wsDDUrl,
064: JarFile moduleFile, boolean isEJB,
065: Map correctedPortLocations) throws DeploymentException {
066:
067: LOG.fine("parsing descriptor " + wsDDUrl);
068:
069: Map<String, PortInfo> map = new HashMap<String, PortInfo>();
070:
071: try {
072: InputStream in = wsDDUrl.openStream();
073: if (in == null) {
074: throw new DeploymentException(
075: "unable to read descriptor " + wsDDUrl);
076: }
077:
078: Unmarshaller unmarshaller = getJAXBContext()
079: .createUnmarshaller();
080: Object obj = unmarshaller.unmarshal(in);
081:
082: WebservicesType wst = null;
083: if (obj instanceof JAXBElement) {
084: wst = (WebservicesType) ((JAXBElement) obj).getValue();
085: }
086:
087: for (WebserviceDescriptionType desc : wst
088: .getWebserviceDescription()) {
089: final String wsdlFile = desc.getWsdlFile().getValue();
090: final String serviceName = desc
091: .getWebserviceDescriptionName().getValue();
092:
093: for (PortComponentType port : desc.getPortComponent()) {
094: String servlet = port.getServiceImplBean()
095: .getServletLink().getValue();
096: String sei = port.getServiceEndpointInterface()
097: .getValue();
098: String portName = port.getPortComponentName()
099: .getValue();
100:
101: PortInfo portInfo = new PortInfo();
102:
103: portInfo.setServiceName(serviceName);
104: portInfo.setServletLink(servlet);
105: portInfo.setServiceEndpointInterfaceName(sei);
106: portInfo.setPortName(portName);
107: portInfo.setWsdlFile(wsdlFile);
108: portInfo.setHandlers(port.getHandler());
109:
110: map.put(servlet, portInfo);
111: }
112: }
113:
114: return map;
115:
116: } catch (IOException ex) {
117: ex.printStackTrace();
118: throw new DeploymentException("unable to read " + wsDDUrl,
119: ex);
120: } catch (JAXBException ex) {
121: ex.printStackTrace();
122: throw new DeploymentException(
123: "unable to parse webservices.xml", ex);
124: }
125: }
126:
127: public synchronized void configurePOJO(GBeanData targetGBean,
128: JarFile moduleFile, Object pi, String implClassName,
129: ClassLoader classLoader) throws DeploymentException {
130:
131: assert pi instanceof PortInfo : "received incorrect portInfo object";
132:
133: ClassLoader orig = Thread.currentThread()
134: .getContextClassLoader();
135:
136: try {
137: Thread.currentThread().setContextClassLoader(
138: getClass().getClassLoader());
139: PortInfo portInfo = (PortInfo) pi;
140: String seiClassName = portInfo
141: .getServiceEndpointInterfaceName();
142:
143: LOG.info("configuring POJO webservice: " + pi + " sei: "
144: + seiClassName);
145:
146: // verify that the class is loadable
147: loadSEI(seiClassName, classLoader);
148: targetGBean.setAttribute(POJO_CLASS_ATTR, implClassName);
149: // TODO: add support for handlers defined in the webservice.xml
150:
151: /*List<Handler> handlers =*/buildHandlerChain(portInfo);
152:
153: CeltixWebServiceContainer container = new CeltixWebServiceContainer(
154: portInfo);
155: targetGBean.setAttribute(WEB_SERVICE_CONTAINER_ATTR,
156: new StoredObject(container));
157:
158: } catch (IOException ex) {
159: throw new DeploymentException(
160: "unable to store CeltixWebServiceContainer", ex);
161: } finally {
162: Thread.currentThread().setContextClassLoader(orig);
163: }
164: }
165:
166: public void configureEJB(GBeanData targetGBean, JarFile moduleFile,
167: Object portInfo, ClassLoader classLoader)
168: throws DeploymentException {
169:
170: throw new DeploymentException("configureEJB NYI");
171: }
172:
173: public void doStart() throws Exception {
174: }
175:
176: public void doStop() throws Exception {
177: }
178:
179: public void doFail() {
180: // TODO Auto-generated method stub
181:
182: }
183:
184: private JAXBContext getJAXBContext() throws JAXBException {
185: if (ctx == null) {
186: ctx = JAXBContext.newInstance("com.sun.java.xml.ns.j2ee",
187: getClass().getClassLoader());
188: }
189: return ctx;
190: }
191:
192: Class<?> loadSEI(String className, ClassLoader loader)
193: throws DeploymentException {
194: try {
195: return loader.loadClass(className);
196: } catch (ClassNotFoundException ex) {
197: throw new DeploymentException(
198: "unable to load Service Endpoint Interface: "
199: + className, ex);
200: }
201: }
202:
203: private List<Handler> buildHandlerChain(PortInfo portInfo) {
204: return new ArrayList<Handler>();
205: }
206:
207: protected Bus getBus() {
208: return bus;
209: }
210:
211: }
|