KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > wsdl > WSDLManagerImpl


1 package org.objectweb.celtix.bus.wsdl;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.net.URL JavaDoc;
6 import java.util.WeakHashMap JavaDoc;
7 import java.util.logging.Level JavaDoc;
8 import java.util.logging.Logger JavaDoc;
9 import javax.wsdl.Definition;
10 import javax.wsdl.WSDLException;
11 import javax.wsdl.extensions.ExtensionRegistry;
12 import javax.wsdl.factory.WSDLFactory;
13 import javax.wsdl.xml.WSDLReader;
14
15 import org.w3c.dom.Element JavaDoc;
16
17 import org.objectweb.celtix.Bus;
18 import org.objectweb.celtix.BusException;
19 import org.objectweb.celtix.bus.busimpl.ComponentCreatedEvent;
20 import org.objectweb.celtix.bus.busimpl.ComponentRemovedEvent;
21 import org.objectweb.celtix.common.logging.LogUtils;
22 import org.objectweb.celtix.wsdl.WSDLManager;
23
24 /**
25  * WSDLManagerImpl
26  *
27  * @author dkulp
28  */

29 public class WSDLManagerImpl implements WSDLManager {
30
31     private static final Logger JavaDoc LOG = LogUtils
32             .getL7dLogger(WSDLManagerImpl.class);
33
34     final ExtensionRegistry registry;
35
36     final WSDLFactory factory;
37
38     final WeakHashMap JavaDoc<Object JavaDoc, Definition> definitionsMap;
39     
40     final Bus bus;
41
42     public WSDLManagerImpl(Bus b) throws BusException {
43         bus = b;
44         try {
45             factory = WSDLFactory.newInstance();
46             registry = factory.newPopulatedExtensionRegistry();
47         } catch (WSDLException e) {
48             throw new BusException(e);
49         }
50         definitionsMap = new WeakHashMap JavaDoc<Object JavaDoc, Definition>();
51         // null check for the unit test
52
if (bus != null) {
53             bus.sendEvent(new ComponentCreatedEvent(this));
54         }
55     }
56
57     public WSDLFactory getWSDLFactory() {
58         return factory;
59     }
60
61     /*
62      * (non-Javadoc)
63      *
64      * XXX - getExtensionRegistry()
65      *
66      * @see org.objectweb.celtix.wsdl.WSDLManager#getExtenstionRegistry()
67      */

68     public ExtensionRegistry getExtenstionRegistry() {
69         return registry;
70     }
71
72     /*
73      * (non-Javadoc)
74      *
75      * @see org.objectweb.celtix.wsdl.WSDLManager#getDefinition(java.net.URL)
76      */

77     public Definition getDefinition(URL JavaDoc url) throws WSDLException {
78         synchronized (definitionsMap) {
79             if (definitionsMap.containsKey(url)) {
80                 return definitionsMap.get(url);
81             }
82         }
83         Definition def = loadDefinition(url.toString());
84         synchronized (definitionsMap) {
85             definitionsMap.put(url, def);
86         }
87         return def;
88     }
89
90     /*
91      * (non-Javadoc)
92      *
93      * @see org.objectweb.celtix.wsdl.WSDLManager#getDefinition(java.lang.String)
94      */

95     public Definition getDefinition(String JavaDoc url) throws WSDLException {
96         synchronized (definitionsMap) {
97             if (definitionsMap.containsKey(url)) {
98                 return definitionsMap.get(url);
99             }
100         }
101         return loadDefinition(url);
102     }
103
104     public Definition getDefinition(Element JavaDoc el) throws WSDLException {
105         synchronized (definitionsMap) {
106             if (definitionsMap.containsKey(el)) {
107                 return definitionsMap.get(el);
108             }
109         }
110         WSDLReader reader = factory.newWSDLReader();
111         reader.setFeature("javax.wsdl.verbose", false);
112         reader.setExtensionRegistry(registry);
113         Definition def = reader.readWSDL(null, el);
114         synchronized (definitionsMap) {
115             definitionsMap.put(el, def);
116         }
117         return def;
118     }
119
120     public Definition getDefinition(Class JavaDoc<?> sei) throws WSDLException {
121         synchronized (definitionsMap) {
122             if (definitionsMap.containsKey(sei)) {
123                 return definitionsMap.get(sei);
124             }
125         }
126         Definition def = null;
127         try {
128             def = createDefinition(sei);
129         } catch (Exception JavaDoc ex) {
130             throw new WSDLException(WSDLException.PARSER_ERROR, ex.getMessage());
131         }
132         
133         synchronized (definitionsMap) {
134             definitionsMap.put(sei, def);
135         }
136         return def;
137     }
138     
139     public void addDefinition(Object JavaDoc key, Definition wsdl) {
140         synchronized (definitionsMap) {
141             definitionsMap.put(key, wsdl);
142         }
143     }
144     
145     private Definition loadDefinition(String JavaDoc url) throws WSDLException {
146         WSDLReader reader = factory.newWSDLReader();
147         reader.setFeature("javax.wsdl.verbose", false);
148         reader.setExtensionRegistry(registry);
149         Definition def = reader.readWSDL(url);
150         synchronized (definitionsMap) {
151             definitionsMap.put(url, def);
152         }
153         return def;
154     }
155
156     private Definition createDefinition(Class JavaDoc<?> sei) throws Exception JavaDoc {
157         Definition definition = null;
158         if (LOG.isLoggable(Level.INFO)) {
159             LOG.info("createDefinition for class: " + sei.getName());
160         }
161         File JavaDoc tmp = null;
162         try {
163             tmp = File.createTempFile("tmp", ".wsdl");
164             tmp.delete();
165             tmp.mkdir();
166         } catch (IOException JavaDoc ex) {
167             LOG.log(Level.SEVERE, "WSDL_GENERATION_TMP_DIR_MSG", ex);
168             return null;
169         }
170
171         /*
172          * JAXWSWsdlGenerator generator = new JAXWSWsdlGenerator(sei.getName(),
173          * sei.getClassLoader()); Configuration config = new ToolConfig(new
174          * String[] {"-wsdl", "-d", tmp.getPath()});
175          * generator.setConfiguration(config); generator.generate();
176          */

177
178         try {
179             int result = 0;
180             org.objectweb.celtix.tools.JavaToWSDL.runTool(new String JavaDoc[] {"-o",
181                     tmp.getPath() + "/tmp.wsdl", sei.getName() });
182             if (0 != result) {
183                 LOG.log(Level.SEVERE, "WSDL_GENERATION_BAD_RESULT_MSG", result);
184                 return null;
185             }
186
187             // schema and WSDL file should have been created in tmp directory
188

189             File JavaDoc[] generated = tmp.listFiles();
190             File JavaDoc schema = null;
191             File JavaDoc wsdl = null;
192             for (File JavaDoc f : generated) {
193                 if (f.isFile()) {
194                     if (null == wsdl && f.getName().endsWith(".wsdl")) {
195                         wsdl = f;
196                     } else if (null == schema && f.getName().endsWith(".xsd")) {
197                         schema = f;
198                     }
199                     if (null != schema && null != wsdl) {
200                         break;
201                     }
202                 }
203             }
204             if (null == wsdl || null == schema) {
205                 LOG.severe("WSDL_SCHEMA_GENERATION_FAILURE_MSG");
206                 return null;
207             } else if (LOG.isLoggable(Level.INFO)) {
208                 LOG.info("Generated " + wsdl.getPath() + " and "
209                         + schema.getPath());
210             }
211
212             /*
213              * WSDLFactory wf = getWSDLFactory();
214              *
215              * try { WSDLReader reader = wf.newWSDLReader();
216              * reader.setFeature("javax.wsdl.verbose", false);
217              * reader.setExtensionRegistry(registry); definition =
218              * reader.readWSDL(wsdl.getPath()); } catch (WSDLException ex) {
219              * LOG.log(Level.SEVERE, "WSDL_UNREADABLE_MSG", ex); }
220              */

221
222             definition = org.objectweb.celtix.tools.JavaToWSDL.getDefinition();
223
224         } finally {
225             class Directory {
226                 private final File JavaDoc dir;
227
228                 Directory(File JavaDoc d) {
229                     dir = d;
230                 }
231
232                 void delete() {
233                     File JavaDoc[] entries = dir.listFiles();
234                     for (File JavaDoc f : entries) {
235                         if (f.isDirectory()) {
236                             Directory d = new Directory(f);
237                             d.delete();
238                         }
239                         f.delete();
240                     }
241                     dir.delete();
242                 }
243             }
244             Directory dir = new Directory(tmp);
245             dir.delete();
246         }
247
248         return definition;
249     }
250
251     public void shutdown() {
252         if (bus != null) {
253             bus.sendEvent(new ComponentRemovedEvent(this));
254         }
255     }
256
257
258 }
259
Popular Tags