1 19 20 package org.netbeans.modules.websvc.api.jaxws.wsdlmodel; 21 22 import com.sun.tools.ws.processor.model.Model; 23 import com.sun.tools.ws.processor.modeler.wsdl.WSDLModeler; 24 import com.sun.tools.ws.wscompile.AbortException; 25 import com.sun.tools.ws.wscompile.BadCommandLineException; 26 import com.sun.tools.ws.wscompile.ErrorReceiver; 27 import com.sun.tools.ws.wscompile.WsimportOptions; 28 import com.sun.xml.ws.util.JAXWSUtils; 29 import java.io.File ; 30 import java.net.URL ; 31 import java.util.ArrayList ; 32 import java.util.HashSet ; 33 import java.util.List ; 34 import java.util.Properties ; 35 import java.util.Set ; 36 import java.util.logging.Level ; 37 import java.util.logging.Logger ; 38 import org.apache.xml.resolver.CatalogManager; 39 import org.apache.xml.resolver.tools.CatalogResolver; 40 import org.openide.util.RequestProcessor; 41 import org.xml.sax.EntityResolver ; 42 import org.xml.sax.SAXParseException ; 43 44 48 public class WsdlModeler { 49 50 private WsdlModel wsdlModel; 51 private WSDLModeler ideWSDLModeler; 52 private URL wsdlUrl; 53 private URL [] bindings; 54 private URL catalog; 55 private EntityResolver entityResolver; 56 private Set <String > bindingFiles; 57 private String packageName; 58 private List <WsdlModelListener> modelListeners; 59 RequestProcessor.Task task; 60 int listenersSize; 61 62 protected Properties properties; 63 64 private Throwable creationException; 65 66 WsdlModeler(URL wsdlUrl) { 67 this.wsdlUrl=wsdlUrl; 68 modelListeners = new ArrayList <WsdlModelListener>(); 69 task = RequestProcessor.getDefault().create(new Runnable () { 70 public void run() { 71 generateWsdlModel(); 72 synchronized (this) { 73 listenersSize = modelListeners.size(); 74 fireModelCreated(wsdlModel,listenersSize); 75 removeListeners(); 76 } 77 } 78 },true); 79 } 80 81 public void setPackageName(String packageName) { 82 this.packageName=packageName; 83 } 84 85 public String getPackageName() { 86 return packageName; 87 } 88 89 public void setJAXBBindings(URL [] bindings) { 90 this.bindings=bindings; 91 } 92 93 public URL [] getJAXBBindings() { 94 return bindings; 95 } 96 97 public void setCatalog(URL catalog) { 98 this.catalog=catalog; 99 } 100 101 public URL getCatalog() { 102 return catalog; 103 } 104 105 void setWsdlUrl(URL url){ 106 wsdlUrl = url; 107 } 108 109 public URL getWsdlUrl(){ 110 return wsdlUrl; 111 } 112 113 public Throwable getCreationException() { 114 return creationException; 115 } 116 117 public WsdlModel getWsdlModel() { 118 return wsdlModel; 119 } 120 121 public WsdlModel getAndWaitForWsdlModel() { 122 if (getWsdlModel()==null) generateWsdlModel(); 123 return wsdlModel; 124 } 125 126 public void generateWsdlModel(WsdlModelListener listener) { 127 generateWsdlModel(listener,false); 128 } 129 130 public void generateWsdlModel(WsdlModelListener listener, boolean forceReload) { 131 if (forceReload) { 132 try {task.waitFinished(10000);} catch (InterruptedException ex) {} 133 addWsdlModelListener(listener); 134 task.schedule(0); 135 } else { 136 addWsdlModelListener(listener); 137 if (task.isFinished()) { 138 task.schedule(0); 139 } 140 } 141 } 142 143 private synchronized void generateWsdlModel() { 144 WsimportOptions options = new WsimportOptions(); 145 properties = new Properties (); 146 bindingFiles = new HashSet <String >(); 147 if (bindings!=null) { 148 for (int i=0;i<bindings.length;i++) { 149 try { 150 options.addBindings(JAXWSUtils.absolutize(bindings[i].toExternalForm())); 151 } catch (BadCommandLineException ex) { 152 Logger.getLogger(this.getClass().getName()).log(Level.FINE, "WsdlModeler.generateWsdlModel", ex); } 154 } 155 } 156 try { 157 options.addWSDL(new File (wsdlUrl.toURI())); 158 options.compatibilityMode = WsimportOptions.EXTENSION; 159 160 if (packageName!=null) { 161 options.defaultPackage = packageName; 162 } 163 if(catalog != null) { 164 CatalogManager manager = new CatalogManager(null); 165 manager.setCatalogFiles(catalog.toExternalForm()); 166 entityResolver = new CatalogResolver(manager); 167 options.entityResolver = entityResolver; 168 } 169 170 options.parseBindings(new IdeErrorReceiver()); 171 172 ideWSDLModeler = 173 new WSDLModeler(options, new IdeErrorReceiver()); 174 Model tmpModel = ideWSDLModeler.buildModel(); 175 176 if (tmpModel!=null) { 177 wsdlModel=new WsdlModel(tmpModel); 178 creationException=null; 179 } 180 } catch (Exception ex){ 181 wsdlModel=null; 182 creationException=ex; 183 Logger.getLogger(this.getClass().getName()).log(Level.FINE, "WsdlModeler.generateWsdlModel", ex); } 185 186 } 187 188 private synchronized void addWsdlModelListener(WsdlModelListener listener) { 189 if (listener!=null) 190 modelListeners.add(listener); 191 } 192 193 private void removeListeners() { 194 modelListeners.clear(); 195 } 196 197 private void fireModelCreated(WsdlModel model, int listenersSize) { 198 for (int i=0;i<listenersSize;i++) { 199 modelListeners.get(i).modelCreated(model); 200 } 201 } 202 203 private class IdeErrorReceiver extends ErrorReceiver{ 204 public void warning(SAXParseException ex) throws AbortException { 205 Logger.getLogger(this.getClass().getName()).log(Level.FINE, 206 "WsdlModeler.generateWsdlModel", ex); } 208 209 public void info(SAXParseException ex) { 210 Logger.getLogger(this.getClass().getName()).log(Level.FINE, 211 "WsdlModeler.generateWsdlModel", ex); } 213 214 public void fatalError(SAXParseException ex) throws AbortException { 215 Logger.getLogger(this.getClass().getName()).log(Level.FINE, 216 "WsdlModeler.generateWsdlModel", ex); } 218 219 public void error(SAXParseException ex) throws AbortException { 220 Logger.getLogger(this.getClass().getName()).log(Level.FINE, 221 "WsdlModeler.generateWsdlModel", ex); } 223 224 } 225 } 226 | Popular Tags |