KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > geronimo > builder > CeltixBuilder


1 package org.objectweb.celtix.geronimo.builder;
2
3
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.net.URL JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Map JavaDoc;
11 import java.util.jar.JarFile JavaDoc;
12 import java.util.logging.Logger JavaDoc;
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 JavaDoc WEB_SERVICE_CONTAINER_ATTR = "webServiceContainer";
39     private static final String JavaDoc POJO_CLASS_ATTR = "pojoClassName";
40     private static final Logger JavaDoc 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 JavaDoc<String JavaDoc, PortInfo> parseWebServiceDescriptor(URL JavaDoc wsDDUrl, JarFile JavaDoc moduleFile, boolean isEJB,
68                                          Map JavaDoc correctedPortLocations)
69         throws DeploymentException {
70             
71         LOG.fine("parsing descriptor " + wsDDUrl);
72         
73         Map JavaDoc<String JavaDoc, PortInfo> map = new HashMap JavaDoc<String JavaDoc, PortInfo>();
74         
75         try {
76             InputStream JavaDoc 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 JavaDoc 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 JavaDoc wsdlFile = desc.getWsdlFile().getValue();
91                 final String JavaDoc serviceName = desc.getWebserviceDescriptionName().getValue();
92                 
93                 for (PortComponentType port : desc.getPortComponent()) {
94                     String JavaDoc servlet = port.getServiceImplBean().getServletLink().getValue();
95                     String JavaDoc sei = port.getServiceEndpointInterface().getValue();
96                     String JavaDoc 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 JavaDoc 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 JavaDoc moduleFile,
124                                            Object JavaDoc pi, String JavaDoc implClassName, ClassLoader JavaDoc classLoader)
125         throws DeploymentException {
126
127         assert pi instanceof PortInfo : "received incorrect portInfo object";
128
129         ClassLoader JavaDoc orig = Thread.currentThread().getContextClassLoader();
130         
131         try {
132             Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
133             PortInfo portInfo = (PortInfo)pi;
134             String JavaDoc seiClassName = portInfo.getServiceEndpointInterfaceName();
135             
136             LOG.info("configuring POJO webservice: " + pi + " sei: " + seiClassName);
137             
138             // verify that the class is loadable
139
loadSEI(seiClassName, classLoader);
140             targetGBean.setAttribute(POJO_CLASS_ATTR, implClassName);
141             // TODO: add support for handlers defined in the webservice.xml
142

143             /*List<Handler> handlers =*/ buildHandlerChain(portInfo);
144             
145             CeltixWebServiceContainer container = new CeltixWebServiceContainer(portInfo);
146             targetGBean.setAttribute(WEB_SERVICE_CONTAINER_ATTR, new StoredObject(container));
147             
148         } catch (IOException JavaDoc 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 JavaDoc moduleFile, Object JavaDoc portInfo,
157                              ClassLoader JavaDoc classLoader)
158         throws DeploymentException {
159
160         throw new DeploymentException("configureEJB NYI");
161     }
162
163     public void doStart() throws Exception JavaDoc {
164     }
165
166     public void doStop() throws Exception JavaDoc {
167     }
168
169     public void doFail() {
170         // TODO Auto-generated method stub
171

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 JavaDoc<?> loadSEI(String JavaDoc className, ClassLoader JavaDoc loader) throws DeploymentException {
182         try {
183             return loader.loadClass(className);
184         } catch (ClassNotFoundException JavaDoc ex) {
185             throw new DeploymentException("unable to load Service Endpoint Interface: " + className, ex);
186         }
187     }
188     
189     private List JavaDoc<Handler> buildHandlerChain(PortInfo portInfo) {
190         return new ArrayList JavaDoc<Handler>();
191     }
192     
193     protected Bus getBus() {
194         return bus;
195     }
196     
197 }
198
Popular Tags