KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > jbi > se > CeltixServiceUnit


1 package org.objectweb.celtix.jbi.se;
2
3 import java.io.File JavaDoc;
4 import java.lang.reflect.Modifier JavaDoc;
5 import java.net.MalformedURLException JavaDoc;
6 import java.net.URL JavaDoc;
7 import java.net.URLClassLoader JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Collection JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.logging.Level JavaDoc;
12 import java.util.logging.Logger JavaDoc;
13
14 import javax.jbi.component.ComponentContext;
15 import javax.jws.WebService;
16 import javax.xml.namespace.QName JavaDoc;
17 import javax.xml.parsers.DocumentBuilder JavaDoc;
18 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
19
20 import org.w3c.dom.Document JavaDoc;
21 import org.w3c.dom.Element JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23 import org.w3c.dom.NodeList JavaDoc;
24
25 import org.objectweb.celtix.Bus;
26 import org.objectweb.celtix.bus.jaxws.EndpointImpl;
27 import org.objectweb.celtix.jbi.ServiceConsumer;
28
29 /**
30  * Wraps a Celtix service or client.
31  */

32 public class CeltixServiceUnit {
33     
34     private static final Logger JavaDoc LOG = Logger.getLogger(CeltixServiceUnit.class.getName());
35     
36     private final Bus bus;
37     
38     private Object JavaDoc serviceImplementation;
39     private ServiceConsumer serviceConsumer;
40     private EndpointImpl endpoint;
41     private final String JavaDoc rootPath;
42     private final ClassLoader JavaDoc parentLoader;
43     private boolean isProvider;
44     private QName JavaDoc serviceName;
45     private String JavaDoc endpointName;
46     
47     public CeltixServiceUnit(Bus b, String JavaDoc path, ComponentClassLoader parent) {
48         
49         URL JavaDoc url = null;
50         try {
51             url = new File JavaDoc(path + File.separator).toURL();
52             
53         } catch (MalformedURLException JavaDoc ex) {
54             LOG.log(Level.SEVERE, "failed to initialize service unit", ex);
55         }
56         bus = b;
57         rootPath = path;
58         parent.addResource(url);
59         parentLoader = parent;
60         parseJbiDescriptor();
61     }
62     
63     public boolean isServiceProvider() {
64         return isProvider;
65     }
66     
67     public void stop() {
68         if (endpoint != null) {
69             endpoint.stop();
70         } else {
71             serviceConsumer.stop();
72         }
73     }
74     
75     
76     public QName JavaDoc getServiceName() {
77         
78         QName JavaDoc ret = null;
79         
80         if (isServiceProvider()) {
81             if (serviceName == null) {
82                 WebService ws = (WebService)serviceImplementation.getClass().getAnnotation(WebService.class);
83                 serviceName = new QName JavaDoc(ws.targetNamespace(), ws.serviceName());
84             }
85             ret = serviceName;
86         }
87         return ret;
88     }
89     
90     public String JavaDoc getEndpointName() {
91         return endpointName;
92     }
93     
94     public void prepare(ComponentContext ctx) {
95         
96         try {
97             WebServiceClassFinder finder = new WebServiceClassFinder(rootPath, parentLoader);
98             Collection JavaDoc<Class JavaDoc<?>> classes = finder.findWebServiceClasses();
99             if (classes.size() > 0) {
100                 LOG.info("publishing endpoint");
101                 isProvider = true;
102                 Class JavaDoc<?> clz = classes.iterator().next();
103                 serviceImplementation = clz.newInstance();
104                 endpoint = new EndpointImpl(bus, serviceImplementation, null);
105                 // dummy endpoint to publish on
106
endpoint.publish("http://foo/bar/baz");
107             } else {
108                 LOG.info("starting consumer");
109                 classes = finder.findServiceConsumerClasses();
110                 Class JavaDoc<?> clz = classes.iterator().next();
111                 LOG.fine("starting consumer: " + clz);
112                 serviceConsumer = (ServiceConsumer)clz.newInstance();
113                 serviceConsumer.setComponentContext(ctx);
114                 new Thread JavaDoc(serviceConsumer).start();
115             }
116         } catch (Exception JavaDoc ex) {
117             if (ex.getCause() != null) {
118                 ex = (Exception JavaDoc)ex.getCause();
119             }
120             LOG.log(Level.SEVERE, "failed to publish endpoint", ex);
121             // TODO throw decent exception here
122
//throw new RuntimeException(ex);
123
}
124     }
125     
126     
127     public ClassLoader JavaDoc getClassLoader() {
128         return parentLoader;
129     }
130     
131     
132     Document JavaDoc getWsdlAsDocument() {
133         
134         Document JavaDoc doc = null;
135         try {
136             WebService ws = serviceImplementation.getClass().getAnnotation(WebService.class);
137             if (ws != null) {
138                 DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
139                 factory.setNamespaceAware(true);
140                 DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
141                 doc = builder.parse(ws.wsdlLocation());
142             } else {
143                 LOG.severe("could not get WebService annotation from " + serviceImplementation);
144             }
145         } catch (Exception JavaDoc ex) {
146             ex.printStackTrace();
147         }
148         return doc;
149     }
150     
151     
152     
153     static class WebServiceClassFinder {
154         private final String JavaDoc rootPath;
155         private final ClassLoader JavaDoc parent;
156         
157         public WebServiceClassFinder(String JavaDoc argRootPath, ClassLoader JavaDoc loader) {
158             if (argRootPath.endsWith(File.separator)) {
159                 argRootPath = argRootPath.substring(0, argRootPath.length() - 2);
160             }
161             rootPath = argRootPath;
162             parent = loader;
163         }
164         
165         public Collection JavaDoc<Class JavaDoc<?>> findServiceConsumerClasses() throws MalformedURLException JavaDoc {
166             return find(new Matcher() {
167                 public boolean accept(Class JavaDoc<?> clz) {
168                     return ServiceConsumer.class.isAssignableFrom(clz)
169                         && (clz.getModifiers() & Modifier.ABSTRACT) == 0;
170                 }
171             });
172         }
173         
174         public Collection JavaDoc<Class JavaDoc<?>> findWebServiceClasses() throws MalformedURLException JavaDoc {
175             
176             return find(new Matcher() {
177                 public boolean accept(Class JavaDoc<?> clz) {
178                     return clz.getAnnotation(WebService.class) != null
179                         && (clz.getModifiers() & Modifier.ABSTRACT) == 0;
180                 }
181             });
182         }
183         
184         private Collection JavaDoc<Class JavaDoc<?>> find(Matcher matcher) throws MalformedURLException JavaDoc {
185             List JavaDoc<Class JavaDoc<?>> classes = new ArrayList JavaDoc<Class JavaDoc<?>>();
186             
187             File JavaDoc root = new File JavaDoc(rootPath);
188             URL JavaDoc[] urls = {root.toURL()};
189             URLClassLoader JavaDoc loader = new URLClassLoader JavaDoc(urls, parent);
190             
191             find(root, loader, classes, matcher);
192             return classes;
193         }
194         
195         private void find(File JavaDoc dir, ClassLoader JavaDoc loader, Collection JavaDoc<Class JavaDoc<?>> classes,
196                           Matcher matcher) {
197             
198             File JavaDoc[] files = dir.listFiles();
199             for (File JavaDoc f : files) {
200                 if (f.toString().endsWith(".class")) {
201                     Class JavaDoc<?> clz = loadClass(loader, f);
202                     if (matcher.accept(clz)) {
203                         classes.add(clz);
204                     }
205                 } else if (f.isDirectory()) {
206                     find(f, loader, classes, matcher);
207                 }
208             }
209         }
210         
211         
212         private Class JavaDoc<?> loadClass(ClassLoader JavaDoc loader, File JavaDoc classFile) {
213             
214             String JavaDoc fileName = classFile.toString();
215             String JavaDoc className = fileName.substring(rootPath.length() + 1);
216             className = className.substring(0, className.length() - ".class".length())
217                 .replace(File.separatorChar, '.');
218             
219             try {
220                 return loader.loadClass(className);
221             } catch (ClassNotFoundException JavaDoc ex) {
222                 LOG.severe("failed to load class: " + className);
223             }
224             return null;
225         }
226         
227         interface Matcher {
228             boolean accept(Class JavaDoc<?> clz);
229         }
230     }
231     
232     private void parseJbiDescriptor() {
233         
234         // right now, all we are interested in is an endpoint name
235
// from the jbi dd.
236
File JavaDoc metaInf = new File JavaDoc(rootPath, "META-INF");
237         File JavaDoc jbiXml = new File JavaDoc(metaInf, "jbi.xml");
238         try {
239             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
240             factory.setNamespaceAware(true);
241             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
242             Document JavaDoc doc = builder.parse(jbiXml.toURL().toString());
243             
244             Element JavaDoc providesEl = (Element JavaDoc)findNode(doc.getDocumentElement(), "provides");
245             endpointName = providesEl.getAttribute("endpoint-name");
246         } catch (Exception JavaDoc ex) {
247             LOG.log(Level.SEVERE, "error parsing " + jbiXml, ex);
248         }
249         
250     }
251     
252     
253     private Node JavaDoc findNode(Node JavaDoc root, String JavaDoc name) {
254         
255         if (name.equals(root.getNodeName())) {
256             return root;
257         }
258         
259         NodeList JavaDoc children = root.getChildNodes();
260         for (int i = 0; i < children.getLength(); i++) {
261             Node JavaDoc child = children.item(i);
262             Node JavaDoc found = findNode(child, name);
263             if (found != null) {
264                 return found;
265             }
266         }
267         return null;
268     }
269     
270     
271 }
272
Popular Tags