1 package org.objectweb.celtix.bus.wsdl; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.net.URL ; 6 import java.util.WeakHashMap ; 7 import java.util.logging.Level ; 8 import java.util.logging.Logger ; 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 ; 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 29 public class WSDLManagerImpl implements WSDLManager { 30 31 private static final Logger LOG = LogUtils 32 .getL7dLogger(WSDLManagerImpl.class); 33 34 final ExtensionRegistry registry; 35 36 final WSDLFactory factory; 37 38 final WeakHashMap <Object , 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 <Object , Definition>(); 51 if (bus != null) { 53 bus.sendEvent(new ComponentCreatedEvent(this)); 54 } 55 } 56 57 public WSDLFactory getWSDLFactory() { 58 return factory; 59 } 60 61 68 public ExtensionRegistry getExtenstionRegistry() { 69 return registry; 70 } 71 72 77 public Definition getDefinition(URL 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 95 public Definition getDefinition(String 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 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.getMessage()); 131 } 132 133 synchronized (definitionsMap) { 134 definitionsMap.put(sei, def); 135 } 136 return def; 137 } 138 139 public void addDefinition(Object key, Definition wsdl) { 140 synchronized (definitionsMap) { 141 definitionsMap.put(key, wsdl); 142 } 143 } 144 145 private Definition loadDefinition(String 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 <?> sei) throws Exception { 157 Definition definition = null; 158 if (LOG.isLoggable(Level.INFO)) { 159 LOG.info("createDefinition for class: " + sei.getName()); 160 } 161 File tmp = null; 162 try { 163 tmp = File.createTempFile("tmp", ".wsdl"); 164 tmp.delete(); 165 tmp.mkdir(); 166 } catch (IOException ex) { 167 LOG.log(Level.SEVERE, "WSDL_GENERATION_TMP_DIR_MSG", ex); 168 return null; 169 } 170 171 177 178 try { 179 int result = 0; 180 org.objectweb.celtix.tools.JavaToWSDL.runTool(new String [] {"-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 189 File [] generated = tmp.listFiles(); 190 File schema = null; 191 File wsdl = null; 192 for (File 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 221 222 definition = org.objectweb.celtix.tools.JavaToWSDL.getDefinition(); 223 224 } finally { 225 class Directory { 226 private final File dir; 227 228 Directory(File d) { 229 dir = d; 230 } 231 232 void delete() { 233 File [] entries = dir.listFiles(); 234 for (File 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 |