001: package org.objectweb.celtix.bus.wsdl;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.net.URL;
006: import java.util.WeakHashMap;
007: import java.util.logging.Level;
008: import java.util.logging.Logger;
009: import javax.wsdl.Definition;
010: import javax.wsdl.WSDLException;
011: import javax.wsdl.extensions.ExtensionRegistry;
012: import javax.wsdl.factory.WSDLFactory;
013: import javax.wsdl.xml.WSDLReader;
014:
015: import org.w3c.dom.Element;
016:
017: import org.objectweb.celtix.Bus;
018: import org.objectweb.celtix.BusException;
019: import org.objectweb.celtix.bus.busimpl.ComponentCreatedEvent;
020: import org.objectweb.celtix.bus.busimpl.ComponentRemovedEvent;
021: import org.objectweb.celtix.common.logging.LogUtils;
022: import org.objectweb.celtix.wsdl.WSDLManager;
023:
024: /**
025: * WSDLManagerImpl
026: *
027: * @author dkulp
028: */
029: public class WSDLManagerImpl implements WSDLManager {
030:
031: private static final Logger LOG = LogUtils
032: .getL7dLogger(WSDLManagerImpl.class);
033:
034: final ExtensionRegistry registry;
035:
036: final WSDLFactory factory;
037:
038: final WeakHashMap<Object, Definition> definitionsMap;
039:
040: final Bus bus;
041:
042: public WSDLManagerImpl(Bus b) throws BusException {
043: bus = b;
044: try {
045: factory = WSDLFactory.newInstance();
046: registry = factory.newPopulatedExtensionRegistry();
047: } catch (WSDLException e) {
048: throw new BusException(e);
049: }
050: definitionsMap = new WeakHashMap<Object, Definition>();
051: // null check for the unit test
052: if (bus != null) {
053: bus.sendEvent(new ComponentCreatedEvent(this ));
054: }
055: }
056:
057: public WSDLFactory getWSDLFactory() {
058: return factory;
059: }
060:
061: /*
062: * (non-Javadoc)
063: *
064: * XXX - getExtensionRegistry()
065: *
066: * @see org.objectweb.celtix.wsdl.WSDLManager#getExtenstionRegistry()
067: */
068: public ExtensionRegistry getExtenstionRegistry() {
069: return registry;
070: }
071:
072: /*
073: * (non-Javadoc)
074: *
075: * @see org.objectweb.celtix.wsdl.WSDLManager#getDefinition(java.net.URL)
076: */
077: public Definition getDefinition(URL url) throws WSDLException {
078: synchronized (definitionsMap) {
079: if (definitionsMap.containsKey(url)) {
080: return definitionsMap.get(url);
081: }
082: }
083: Definition def = loadDefinition(url.toString());
084: synchronized (definitionsMap) {
085: definitionsMap.put(url, def);
086: }
087: return def;
088: }
089:
090: /*
091: * (non-Javadoc)
092: *
093: * @see org.objectweb.celtix.wsdl.WSDLManager#getDefinition(java.lang.String)
094: */
095: public Definition getDefinition(String url) throws WSDLException {
096: synchronized (definitionsMap) {
097: if (definitionsMap.containsKey(url)) {
098: return definitionsMap.get(url);
099: }
100: }
101: return loadDefinition(url);
102: }
103:
104: public Definition getDefinition(Element 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<?> 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 ex) {
130: throw new WSDLException(WSDLException.PARSER_ERROR, ex
131: .getMessage());
132: }
133:
134: synchronized (definitionsMap) {
135: definitionsMap.put(sei, def);
136: }
137: return def;
138: }
139:
140: public void addDefinition(Object key, Definition wsdl) {
141: synchronized (definitionsMap) {
142: definitionsMap.put(key, wsdl);
143: }
144: }
145:
146: private Definition loadDefinition(String url) throws WSDLException {
147: WSDLReader reader = factory.newWSDLReader();
148: reader.setFeature("javax.wsdl.verbose", false);
149: reader.setExtensionRegistry(registry);
150: Definition def = reader.readWSDL(url);
151: synchronized (definitionsMap) {
152: definitionsMap.put(url, def);
153: }
154: return def;
155: }
156:
157: private Definition createDefinition(Class<?> sei) throws Exception {
158: Definition definition = null;
159: if (LOG.isLoggable(Level.INFO)) {
160: LOG.info("createDefinition for class: " + sei.getName());
161: }
162: File tmp = null;
163: try {
164: tmp = File.createTempFile("tmp", ".wsdl");
165: tmp.delete();
166: tmp.mkdir();
167: } catch (IOException ex) {
168: LOG.log(Level.SEVERE, "WSDL_GENERATION_TMP_DIR_MSG", ex);
169: return null;
170: }
171:
172: /*
173: * JAXWSWsdlGenerator generator = new JAXWSWsdlGenerator(sei.getName(),
174: * sei.getClassLoader()); Configuration config = new ToolConfig(new
175: * String[] {"-wsdl", "-d", tmp.getPath()});
176: * generator.setConfiguration(config); generator.generate();
177: */
178:
179: try {
180: int result = 0;
181: org.objectweb.celtix.tools.JavaToWSDL.runTool(new String[] {
182: "-o", tmp.getPath() + "/tmp.wsdl", sei.getName() });
183: if (0 != result) {
184: LOG.log(Level.SEVERE, "WSDL_GENERATION_BAD_RESULT_MSG",
185: result);
186: return null;
187: }
188:
189: // schema and WSDL file should have been created in tmp directory
190:
191: File[] generated = tmp.listFiles();
192: File schema = null;
193: File wsdl = null;
194: for (File f : generated) {
195: if (f.isFile()) {
196: if (null == wsdl && f.getName().endsWith(".wsdl")) {
197: wsdl = f;
198: } else if (null == schema
199: && f.getName().endsWith(".xsd")) {
200: schema = f;
201: }
202: if (null != schema && null != wsdl) {
203: break;
204: }
205: }
206: }
207: if (null == wsdl || null == schema) {
208: LOG.severe("WSDL_SCHEMA_GENERATION_FAILURE_MSG");
209: return null;
210: } else if (LOG.isLoggable(Level.INFO)) {
211: LOG.info("Generated " + wsdl.getPath() + " and "
212: + schema.getPath());
213: }
214:
215: /*
216: * WSDLFactory wf = getWSDLFactory();
217: *
218: * try { WSDLReader reader = wf.newWSDLReader();
219: * reader.setFeature("javax.wsdl.verbose", false);
220: * reader.setExtensionRegistry(registry); definition =
221: * reader.readWSDL(wsdl.getPath()); } catch (WSDLException ex) {
222: * LOG.log(Level.SEVERE, "WSDL_UNREADABLE_MSG", ex); }
223: */
224:
225: definition = org.objectweb.celtix.tools.JavaToWSDL
226: .getDefinition();
227:
228: } finally {
229: class Directory {
230: private final File dir;
231:
232: Directory(File d) {
233: dir = d;
234: }
235:
236: void delete() {
237: File[] entries = dir.listFiles();
238: for (File f : entries) {
239: if (f.isDirectory()) {
240: Directory d = new Directory(f);
241: d.delete();
242: }
243: f.delete();
244: }
245: dir.delete();
246: }
247: }
248: Directory dir = new Directory(tmp);
249: dir.delete();
250: }
251:
252: return definition;
253: }
254:
255: public void shutdown() {
256: if (bus != null) {
257: bus.sendEvent(new ComponentRemovedEvent(this));
258: }
259: }
260:
261: }
|