1 19 20 package org.netbeans.modules.websvc.registry.jaxrpc; 21 22 import org.netbeans.modules.websvc.registry.util.Util; 23 import org.netbeans.modules.websvc.registry.util.WebProxySetter; 24 import org.netbeans.modules.websvc.registry.model.WebServiceData; 25 import org.netbeans.modules.websvc.registry.WebServiceException; 26 import com.sun.xml.rpc.spi.JaxRpcObjectFactory; 27 28 import com.sun.xml.rpc.processor.model.Operation; 29 import com.sun.xml.rpc.processor.model.Port; 30 import com.sun.xml.rpc.processor.model.java.JavaException; 31 import com.sun.xml.rpc.processor.model.java.JavaMethod; 32 import com.sun.xml.rpc.spi.model.JavaInterface; 33 import com.sun.xml.rpc.spi.model.Model; 34 import com.sun.xml.rpc.spi.model.Service; 35 import com.sun.xml.rpc.spi.tools.CompileTool; 36 import com.sun.xml.rpc.spi.tools.CompileToolDelegate; 37 import com.sun.xml.rpc.spi.tools.GeneratedFileInfo; 38 import com.sun.xml.rpc.tools.wscompile.ActionConstants; 39 import java.io.BufferedOutputStream ; 40 import java.io.File ; 41 import java.io.FileOutputStream ; 42 import java.io.FileWriter ; 43 import java.io.OutputStreamWriter ; 44 import java.io.OutputStream ; 45 import java.io.PrintStream ; 46 import java.io.IOException ; 47 48 import java.net.URI ; 49 import java.net.URL ; 50 import java.util.ArrayList ; 51 import java.util.HashSet ; 52 import java.util.Iterator ; 53 import java.util.List ; 54 import java.util.Set ; 55 import java.util.StringTokenizer ; 56 import org.openide.ErrorManager; 57 import org.openide.util.NbBundle; 58 import org.openide.awt.StatusDisplayer; 59 60 61 62 65 public class Wsdl2Java { 66 67 private String program ="wscompile"; 68 private String outputDir; 69 private String nonClassOutputDir; 70 private String sourceOutputDir; 71 private URL wsdlUrl; 72 private String packageName; 73 private JaxRpcObjectFactory jaxprObjectFactory ; 74 private CompileTool compTool; 75 private File webserviceClient; 76 private File webserviceClientBeanInfo; 77 78 79 public final static String DEFAULT_TARGET_PACKAGE = "webservice"; 81 public Wsdl2Java() { 82 83 System.setProperty("http.proxyHost", WebProxySetter.getInstance().getProxyHost()); 84 System.setProperty("http.proxyPort", WebProxySetter.getInstance().getProxyPort()); 85 86 jaxprObjectFactory = JaxRpcObjectFactory.newInstance(); 87 } 88 89 public void setOutputDirectory(String path){ 90 outputDir = path; 91 } 92 93 public String getOutputDirectory(){ 94 if(outputDir == null) { 95 outputDir = System.getProperty("user.home"); 96 } 97 return outputDir; 98 } 99 100 public void setNonClassOutputDirectory(String path){ 101 nonClassOutputDir = path; 102 } 103 104 public String getNonClassOutputDirectory(){ 105 if(nonClassOutputDir == null) { 106 nonClassOutputDir = getOutputDirectory(); 107 } 108 return nonClassOutputDir; 109 } 110 111 public void setSourceOutputDirectory(String path){ 112 sourceOutputDir = path; 113 } 114 115 public String getSourceOutputDirectory(){ 116 if(sourceOutputDir == null) { 117 sourceOutputDir = getOutputDirectory(); 118 } 119 return sourceOutputDir; 120 } 121 122 public File getWebserviceClient() { 123 return webserviceClient; 124 } 125 public File getWebserviceClientBeanInfo() { 126 return webserviceClientBeanInfo; 127 } 128 129 public void setWsdlUrl(URL url){ 130 wsdlUrl = url; 131 } 132 133 public URL getWsdlUrl(){ 134 return wsdlUrl; 135 } 136 137 public void setPackageName(String pkgName){ 138 packageName = pkgName; 139 } 140 141 public String getPackageName(){ 142 if(packageName == null) { 143 packageName = this.DEFAULT_TARGET_PACKAGE; 144 } 145 return packageName; 146 } 147 148 public boolean execute(WebServiceData inWSData, File errorFile) { 149 PrintStream ps = null; 150 try{ 151 FileOutputStream fdout = new FileOutputStream (errorFile); 152 BufferedOutputStream bos = new BufferedOutputStream (fdout, 2048); 153 ps = new PrintStream (bos, false); 154 }catch(Exception exc){ 155 ps = System.out; 156 } 157 return execute(inWSData,ps); 158 } 159 160 public boolean execute(WebServiceData inWSData,OutputStream outputStream) { 161 jaxprObjectFactory = JaxRpcObjectFactory.newInstance(); 162 compTool = jaxprObjectFactory.createCompileTool(outputStream, program); 163 WSCompileArguments wsCompileArgs = new WSCompileArguments(); 164 165 wsCompileArgs.setClasspath(Util.getRuntimeClassPath()); 166 File outDir = new File (getOutputDirectory()); 167 if(!outDir.exists()) { 168 outDir.mkdirs(); 169 } 170 wsCompileArgs.setOutputDirectory(outDir.getAbsolutePath()); 171 wsCompileArgs.setGen("client"); 172 wsCompileArgs.setVerbose(false); 174 wsCompileArgs.setKeep(false); 175 180 wsCompileArgs.addFeature("searchschema"); 182 File nonClassDir = new File (getNonClassOutputDirectory()); 183 if(!nonClassDir.exists()) nonClassDir.mkdirs(); 184 wsCompileArgs.setNonclassOutputDirectory(nonClassDir.getAbsolutePath()); 185 186 File srcDir = new File (getSourceOutputDirectory()); 187 if(!srcDir.exists()) srcDir.mkdirs(); 188 wsCompileArgs.setSourceOutputDirectory(srcDir.getAbsolutePath()); 189 190 Configuration config = new Configuration(); 191 WsdlType wsdl = new WsdlType(); 192 config.setWsdl(wsdl); 193 try{ 194 wsdl.setLocation( getWsdlUrl()); 195 }catch(Exception exc){ 196 exc.printStackTrace(); 197 return false; 198 } 199 wsdl.setPackageName(getPackageName()); 200 wsCompileArgs.setConfiguration(config); 201 202 boolean ret = false; 203 try{ 204 ret = compTool.run(wsCompileArgs.toArgs()); 206 if(ret) { 207 createWrapperClients(inWSData); 208 } else { 209 ErrorManager.getDefault().log(this.getClass().getName() + NbBundle.getMessage(this.getClass(), "ERROR_WSCOMPILE_INTERNAL")); 210 StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(this.getClass(), "ERROR_WSCOMPILE_EXTERNAL")); 212 } 213 }catch(Exception exc){ 214 ErrorManager.getDefault().notify(exc); 215 ErrorManager.getDefault().log(this.getClass().getName() + NbBundle.getMessage(this.getClass(), "ERROR_WSCOMPILE_INTERNAL")); 216 StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(this.getClass(), "ERROR_WSCOMPILE_EXTERNAL")); 218 } 219 return ret; 220 } 221 222 225 public void createWrapperClients(WebServiceData inWSData){ 226 File sourceDir = new File (getSourceOutputDirectory()); 227 String pkgName = getPackageName(); 228 StringTokenizer strTokenizer = new StringTokenizer (getPackageName(),"."); 229 while(strTokenizer.hasMoreTokens()) { 230 sourceDir = new File (sourceDir,strTokenizer.nextToken()); 231 } 232 if(!sourceDir.exists()) { 233 ErrorManager.getDefault().log(this.getClass().getName() + NbBundle.getMessage(this.getClass(), "ERROR_CREATING_SOURCEDIR")); 234 StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(this.getClass(), "ERROR_CREATING_SOURCEDIR")); 236 } 237 Model model = compTool.getProcessor().getModel(); 238 if(model == null) { 239 ErrorManager.getDefault().log(this.getClass().getName() + NbBundle.getMessage(this.getClass(), "ERROR_WSCOMPILE_INTERNAL")); 240 StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(this.getClass(), "ERROR_WSCOMPILE_EXTERNAL")); 242 return; 243 } 244 for (Iterator services = model.getServices(); services.hasNext(); ) { 245 Service service = (Service) services.next(); 246 String serviceFullName = service.getJavaIntf().getName(); 247 250 String serviceName = serviceFullName.substring(serviceFullName.lastIndexOf(".") + 1); 251 Iterator ports = service.getPorts(); 252 256 String displayName = inWSData.getDisplayName(); 257 String clientNameRoot = displayName.substring(displayName.lastIndexOf('.') + 1, displayName.length()); 258 259 webserviceClient = new File (sourceDir, clientNameRoot + "Client.java"); 260 webserviceClientBeanInfo = new File (sourceDir, clientNameRoot + "ClientBeanInfo.java"); 261 WrapperClientWriter beanWriter = null; 262 WrapperClientBeanInfoWriter beanInfoWriter = null; 263 try { 264 beanWriter = new WrapperClientWriter(new FileWriter (webserviceClient)); 265 beanInfoWriter = new WrapperClientBeanInfoWriter(new FileWriter (webserviceClientBeanInfo)); 266 } catch(IOException ioe) { 267 ErrorManager.getDefault().notify(ioe); 268 StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(this.getClass(), "ERROR_WSCOMPILE_EXTERNAL")); 270 return; 271 } 272 273 beanWriter.setPackage(getPackageName()); 274 beanWriter.setName(clientNameRoot + "Client"); 275 beanWriter.setContainedClassInfo(serviceName); 276 beanWriter.addImport(inWSData.getPackageName() + ".*"); 277 beanInfoWriter.setPackage(getPackageName()); 278 beanInfoWriter.setName(clientNameRoot + "Client"); 279 while (ports.hasNext()) { 280 Port port = (Port)ports.next(); 281 beanWriter.addPort(port); 282 } 283 beanWriter.writeClass(); 284 beanWriter.flush(); 285 beanWriter.close(); 286 beanInfoWriter.writeBeanInfo(); 287 beanInfoWriter.flush(); 288 beanInfoWriter.close(); 289 290 } 291 } 292 293 public Model getModel(){ 294 return compTool.getProcessor().getModel(); 295 } 296 297 298 public File [] getGeneratedFiles(){ 299 return getGeneratedFiles(false); 300 } 301 302 public File [] getGeneratedJavaFiles(){ 303 return getGeneratedFiles(true); 304 } 305 306 public File [] getGeneratedFiles(boolean onlyJava){ 307 Set generatedFiles = new HashSet (); 308 for (Iterator iter = compTool.getEnvironment().getGeneratedFiles(); iter.hasNext();) { 313 Object obj = iter.next(); 314 File file; 315 if (obj instanceof File ) { 316 file = (File ) obj; 317 }else if (obj instanceof GeneratedFileInfo) { 318 file = ((GeneratedFileInfo)obj).getFile(); 319 }else { 320 continue; 322 } 323 324 if (file.exists()){ 325 if(onlyJava){ 326 if(file.getName().endsWith(".java")) generatedFiles.add(file); 327 }else{ 328 generatedFiles.add(file); 329 } 330 } 331 } 332 return (File []) generatedFiles.toArray(new File [generatedFiles.size()]); 333 } 334 335 public static void main(String [] args){ 336 System.setProperty("http.proxyHost", "webcache.sfbay.sun.com"); 337 System.setProperty("http.proxyPort", "8080"); 338 339 Wsdl2Java wsdl2java = new Wsdl2Java(); 340 wsdl2java.setOutputDirectory("D:\\wsdl2java\\classes"); 341 File wsdlFile = new File ("D:\\wsdl2java\\JavaTravellerService.wsdl"); 344 346 try{ 347 wsdl2java.setWsdlUrl(wsdlFile.toURI().toURL()); 348 }catch (Exception exc){ 352 exc.printStackTrace(); 353 System.exit(-1); 354 } 355 356 WebServiceData wsData = new WebServiceData(); 358 wsData.setDisplayName("JavaTraveller_service"); 359 if(wsdl2java.execute(wsData,System.out)) { 360 System.out.println("Client Proxy Successfully created"); 361 }else{ 362 System.out.println("Client Proxy creation failed"); 363 } 364 365 369 } 370 371 } 372 | Popular Tags |