1 19 20 package org.netbeans.modules.websvc.jaxrpc.client.wizard; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.HashMap ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.Set ; 34 import org.netbeans.api.java.classpath.ClassPath; 35 import org.netbeans.api.java.project.JavaProjectConstants; 36 import org.netbeans.api.java.queries.UnitTestForSourceQuery; 37 import org.netbeans.api.progress.ProgressHandle; 38 import org.netbeans.api.progress.ProgressHandleFactory; 39 import org.netbeans.api.project.Project; 40 import org.netbeans.api.project.ProjectUtils; 41 import org.netbeans.api.project.SourceGroup; 42 import org.netbeans.api.project.libraries.Library; 43 import org.netbeans.api.project.libraries.LibraryManager; 44 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 45 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform; 46 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 47 import org.netbeans.modules.websvc.api.client.ClientStubDescriptor; 48 import org.netbeans.modules.websvc.api.client.WebServicesClientSupport; 49 import org.netbeans.modules.websvc.api.jaxws.client.JAXWSClientSupport; 50 import org.netbeans.modules.websvc.core.ClientWizardProperties; 51 import org.netbeans.modules.websvc.core.webservices.ui.panels.WebProxySetter; 52 import org.netbeans.modules.websvc.core.WsdlRetriever; 53 import org.netbeans.modules.websvc.core.WsdlRetriever; 54 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender; 55 import org.openide.DialogDisplayer; 56 import org.openide.ErrorManager; 57 import org.openide.NotifyDescriptor; 58 import org.openide.WizardDescriptor; 59 import org.openide.filesystems.FileAlreadyLockedException; 60 import org.openide.filesystems.FileLock; 61 import org.openide.filesystems.FileObject; 62 import org.openide.filesystems.FileUtil; 63 import org.openide.filesystems.URLMapper; 64 import org.openide.util.NbBundle; 65 import org.openide.util.RequestProcessor; 66 import org.netbeans.modules.j2ee.common.Util; 67 import org.openide.util.Task; 68 69 73 public class WebServiceClientCreator { 74 75 private static WebServiceClientCreator instance; 76 private Project project; 77 private WizardDescriptor wiz; 78 79 private static final boolean DEBUG = false; 80 private static final int JSE_PROJECT_TYPE = 0; 81 private static final int WEB_PROJECT_TYPE = 1; 82 private static final int EJB_PROJECT_TYPE = 2; 83 private static final int CAR_PROJECT_TYPE = 3; 84 85 88 public WebServiceClientCreator(Project project, WizardDescriptor wiz) { 89 this.project = project; 90 this.wiz = wiz; 91 } 92 93 public Set create() throws IOException { 94 95 final boolean isJsr109Supported = isJsr109Supported(); 96 final boolean isJsr109OldSupported = isJsr109OldSupported(); 97 final boolean isJWSDPSupported = isJWSDPSupported(); 98 99 final ProgressHandle handle = ProgressHandleFactory.createHandle(NbBundle.getMessage(WebServiceClientCreator.class, "MSG_WizCreateClient")); 102 task = new Task(new Runnable () { 103 public void run() { 104 try { 105 String jaxVersion = (String ) wiz.getProperty(ClientWizardProperties.JAX_VERSION); 106 if (jaxVersion.equals(ClientWizardProperties.JAX_WS)) { 107 handle.start(); 108 generate15Client((isJsr109Supported || isJWSDPSupported), handle); 109 } else { 110 handle.start(100); 111 generate14Client(handle); 112 } 113 } catch (IOException exc) { 114 handle.finish(); 116 117 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, exc); 118 } 119 } 120 }); 121 RequestProcessor.getDefault().post(task); 122 123 return Collections.EMPTY_SET; 124 } 125 126 private void generate15Client(boolean isJsr109Platform, ProgressHandle handle) throws IOException { 127 128 JAXWSClientSupport jaxWsClientSupport=null; 130 if(project != null) { 131 jaxWsClientSupport = JAXWSClientSupport.getJaxWsClientSupport(project.getProjectDirectory()); 132 } 133 if(jaxWsClientSupport == null) { 134 String mes = NbBundle.getMessage(WebServiceClientCreator.class, "ERR_NoWebServiceClientSupport"); NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); 140 DialogDisplayer.getDefault().notify(desc); 141 } 142 143 String wsdlUrl = (String )wiz.getProperty(ClientWizardProperties.WSDL_DOWNLOAD_URL); 144 String filePath = (String )wiz.getProperty(ClientWizardProperties.WSDL_FILE_PATH); 145 if(wsdlUrl == null){ 147 wsdlUrl = FileUtil.toFileObject(new File (filePath)).getURL().toExternalForm(); 148 } 149 String packageName = (String )wiz.getProperty(ClientWizardProperties.WSDL_PACKAGE_NAME); 150 if (packageName!=null && packageName.length()==0) packageName=null; 151 jaxWsClientSupport.addServiceClient(getWsdlName(wsdlUrl),wsdlUrl,packageName, isJsr109Platform); 152 153 handle.finish(); 154 } 155 156 private String getWsdlName(String wsdlUrl) { 157 int ind = wsdlUrl.lastIndexOf("/"); String wsdlName = ind>=0?wsdlUrl.substring(ind+1):wsdlUrl; 159 if (wsdlName.toUpperCase().endsWith("?WSDL")) wsdlName = wsdlName.substring(0,wsdlName.length()-5); ind = wsdlName.lastIndexOf(".wsdl"); if (ind>0) wsdlName = wsdlName.substring(0,ind); 162 return convertAllSpecialChars(wsdlName); 164 } 165 166 private String convertAllSpecialChars(String resultStr){ 167 StringBuffer sb = new StringBuffer (resultStr); 168 for(int i = 0; i < sb.length(); i++){ 169 char c = sb.charAt(i); 170 if( Character.isLetterOrDigit(c) || 171 (c == '/') || 172 (c == '.') || 173 (c == '_') || 174 (c == ' ') || 175 (c == '-')){ 176 continue; 177 }else{ 178 sb.setCharAt(i, '_'); 179 } 180 } 181 return sb.toString(); 182 } 183 184 private void generate14Client(final ProgressHandle handle) throws IOException { 185 193 WebServicesClientSupport clientSupport = null; 194 195 if(project != null) { 197 clientSupport = WebServicesClientSupport.getWebServicesClientSupport(project.getProjectDirectory()); 198 } 199 200 if(clientSupport == null) { 201 String mes = NbBundle.getMessage(WebServiceClientCreator.class, "ERR_NoWebServiceClientSupport"); NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); 207 DialogDisplayer.getDefault().notify(desc); 208 return; 209 } 210 211 final byte [] sourceWsdlDownload = (byte []) wiz.getProperty(ClientWizardProperties.WSDL_DOWNLOAD_FILE); 212 final List downloadedSchemas = (List ) wiz.getProperty(ClientWizardProperties.WSDL_DOWNLOAD_SCHEMAS); 213 String wsdlFilePath = (String ) wiz.getProperty(ClientWizardProperties.WSDL_FILE_PATH); 214 String packageName = (String ) wiz.getProperty(ClientWizardProperties.WSDL_PACKAGE_NAME); 215 ClientStubDescriptor stubDescriptor = (ClientStubDescriptor) wiz.getProperty(ClientWizardProperties.CLIENT_STUB_TYPE); 216 217 String sourceUrl; 218 FileObject sourceWsdlFile = null; 219 220 if(sourceWsdlDownload == null) { 221 File normalizedWsdlFilePath = FileUtil.normalizeFile(new File (wsdlFilePath)); 223 sourceUrl = normalizedWsdlFilePath.toString(); 224 sourceWsdlFile = FileUtil.toFileObject(normalizedWsdlFilePath); 225 226 if(sourceWsdlFile == null) { 227 String mes = NbBundle.getMessage(WebServiceClientCreator.class, "ERR_WsdlFileNotFound", normalizedWsdlFilePath); NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); 229 DialogDisplayer.getDefault().notify(desc); 230 return; 231 } 232 } else { 233 File wsdlFile = new File (System.getProperty("java.io.tmpdir"), wsdlFilePath); 235 if(!wsdlFile.exists()) { 236 try { 237 wsdlFile.createNewFile(); 238 } catch(IOException ex) { 239 String mes = NbBundle.getMessage(WebServiceClientCreator.class, "ERR_UnableToCreateTempFile", wsdlFile.getPath()); NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); 241 DialogDisplayer.getDefault().notify(desc); 242 return; 243 } 244 } 245 246 sourceUrl = (String ) wiz.getProperty(ClientWizardProperties.WSDL_DOWNLOAD_URL); 247 sourceWsdlFile = FileUtil.toFileObject(FileUtil.normalizeFile(wsdlFile)); 248 249 if(sourceWsdlFile != null) { 250 FileLock wsdlLock = sourceWsdlFile.lock(); 251 252 try { 253 OutputStream out = sourceWsdlFile.getOutputStream(wsdlLock); 254 try { 255 out.write(sourceWsdlDownload); 256 out.flush(); 257 } finally { 258 if(out != null) { 259 out.close(); 260 } 261 } 262 } finally { 263 wsdlLock.releaseLock(); 264 } 265 } else { 266 String mes = NbBundle.getMessage(WebServiceClientCreator.class, "ERR_UnableToCreateTempFile", wsdlFile.getPath()); NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); 268 DialogDisplayer.getDefault().notify(desc); 269 return; 270 } 271 272 if (downloadedSchemas!=null) { 274 Iterator it = downloadedSchemas.iterator(); 275 while (it.hasNext()) { 276 WsdlRetriever.SchemaInfo schemaInfo = (WsdlRetriever.SchemaInfo)it.next(); 277 File schemalFile = new File (System.getProperty("java.io.tmpdir"), schemaInfo.getSchemaName()); 278 try { 279 schemalFile.createNewFile(); 280 } catch(IOException ex) { 281 String mes = NbBundle.getMessage(WebServiceClientCreator.class, "ERR_UnableToCreateTempFile", schemalFile.getPath()); NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); 283 DialogDisplayer.getDefault().notify(desc); 284 return; 285 } 286 FileObject schemaFo = FileUtil.toFileObject(FileUtil.normalizeFile(schemalFile)); 287 if(schemaFo != null) { 288 FileLock lock = schemaFo.lock(); 289 290 try { 291 OutputStream out = schemaFo.getOutputStream(lock); 292 try { 293 out.write(schemaInfo.getSchemaContent()); 294 out.flush(); 295 } finally { 296 if(out != null) { 297 out.close(); 298 } 299 } 300 } finally { 301 lock.releaseLock(); 302 } 303 } else { 304 String mes = NbBundle.getMessage(WebServiceClientCreator.class, "ERR_UnableToCreateTempFile", schemalFile.getPath()); NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); 306 DialogDisplayer.getDefault().notify(desc); 307 return; 308 } 309 } } } 313 SourceGroup[] sgs = getJavaSourceGroups(project); 315 ClassPath classPath = ClassPath.getClassPath(sgs[0].getRootFolder(),ClassPath.COMPILE); 316 317 FileObject wscompileFO = classPath.findResource("com/sun/xml/rpc/tools/ant/Wscompile.class"); 318 if (wscompileFO==null) { 319 ProjectClassPathExtender pce = (ProjectClassPathExtender)project.getLookup().lookup(ProjectClassPathExtender.class); 321 Library jaxrpclib = LibraryManager.getDefault().getLibrary("jaxrpc16"); if ((pce!=null) && (jaxrpclib != null)) { 323 pce.addLibrary(jaxrpclib); 324 } 325 } 326 327 clientSupport.setProxyJVMOptions(WebProxySetter.getInstance().getProxyHost(),WebProxySetter.getInstance().getProxyPort()); 329 330 final ClientBuilder builder = new ClientBuilder(project, clientSupport, sourceWsdlFile, packageName, sourceUrl, stubDescriptor); 333 final FileObject sourceWsdlFileTmp = sourceWsdlFile; 334 335 org.openide.util.RequestProcessor.getDefault().post(new Runnable () { 336 public void run() { 337 try { 338 builder.generate(handle); 339 340 if(sourceWsdlDownload != null) { 341 try { 343 sourceWsdlFileTmp.delete(); 344 } catch(FileAlreadyLockedException ex) { 345 String mes = NbBundle.getMessage(WebServiceClientCreator.class, "ERR_TempFileLocked", sourceWsdlFileTmp.getNameExt()); NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); 347 DialogDisplayer.getDefault().notify(desc); 348 } catch(IOException ex) { 349 String mes = NbBundle.getMessage(WebServiceClientCreator.class, "ERR_TempFileNotDeleted", sourceWsdlFileTmp.getNameExt()); NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); 351 DialogDisplayer.getDefault().notify(desc); 352 } 353 } 354 355 handle.progress(NbBundle.getMessage(WebServiceClientCreator.class, "MSG_WizDone"),99); 356 } finally { 357 handle.finish(); 358 } 359 } 360 }); 361 } 362 363 370 static SourceGroup[] getJavaSourceGroups(Project project) { 371 SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups( 372 JavaProjectConstants.SOURCES_TYPE_JAVA); 373 Set testGroups = getTestSourceGroups(project, sourceGroups); 374 List result = new ArrayList (); 375 for (int i = 0; i < sourceGroups.length; i++) { 376 if (!testGroups.contains(sourceGroups[i])) { 377 result.add(sourceGroups[i]); 378 } 379 } 380 return (SourceGroup[]) result.toArray(new SourceGroup[result.size()]); 381 } 382 383 private static Set getTestSourceGroups(Project project, SourceGroup[] sourceGroups) { 384 Map foldersToSourceGroupsMap = createFoldersToSourceGroupsMap(sourceGroups); 385 Set testGroups = new HashSet (); 386 for (int i = 0; i < sourceGroups.length; i++) { 387 testGroups.addAll(getTestTargets(sourceGroups[i], foldersToSourceGroupsMap)); 388 } 389 return testGroups; 390 } 391 392 private static List getTestTargets(SourceGroup sourceGroup, Map foldersToSourceGroupsMap) { 393 final URL [] rootURLs = UnitTestForSourceQuery.findUnitTests(sourceGroup.getRootFolder()); 394 if (rootURLs.length == 0) { 395 return new ArrayList (); 396 } 397 List result = new ArrayList (); 398 List sourceRoots = getFileObjects(rootURLs); 399 for (int i = 0; i < sourceRoots.size(); i++) { 400 FileObject sourceRoot = (FileObject) sourceRoots.get(i); 401 SourceGroup srcGroup = (SourceGroup) foldersToSourceGroupsMap.get(sourceRoot); 402 if (srcGroup != null) { 403 result.add(srcGroup); 404 } 405 } 406 return result; 407 } 408 409 private static Map createFoldersToSourceGroupsMap(final SourceGroup[] sourceGroups) { 410 Map result; 411 if (sourceGroups.length == 0) { 412 result = Collections.EMPTY_MAP; 413 } else { 414 result = new HashMap (2 * sourceGroups.length, .5f); 415 for (int i = 0; i < sourceGroups.length; i++) { 416 SourceGroup sourceGroup = sourceGroups[i]; 417 result.put(sourceGroup.getRootFolder(), sourceGroup); 418 } 419 } 420 return result; 421 } 422 423 private static List getFileObjects(URL [] urls) { 424 List result = new ArrayList (); 425 for (int i = 0; i < urls.length; i++) { 426 FileObject sourceRoot = URLMapper.findFileObject(urls[i]); 427 if (sourceRoot != null) { 428 result.add(sourceRoot); 429 } else { 430 int severity = ErrorManager.INFORMATIONAL; 431 if (ErrorManager.getDefault().isNotifiable(severity)) { 432 ErrorManager.getDefault().notify(severity, new IllegalStateException ( 433 "No FileObject found for the following URL: " + urls[i])); } 435 } 436 } 437 return result; 438 } 439 440 private J2eePlatform getJ2eePlatform(){ 441 J2eeModuleProvider provider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class); 442 if(provider != null){ 443 String serverInstanceID = provider.getServerInstanceID(); 444 if(serverInstanceID != null && serverInstanceID.length() > 0) { 445 return Deployment.getDefault().getJ2eePlatform(serverInstanceID); 446 } 447 } 448 return null; 449 } 450 451 private boolean isJWSDPSupported(){ 452 J2eePlatform j2eePlatform = getJ2eePlatform(); 453 if(j2eePlatform != null){ 454 return j2eePlatform.isToolSupported(J2eePlatform.TOOL_JWSDP); 455 } 456 return false; 457 } 458 459 private boolean isJsr109Supported(){ 460 J2eePlatform j2eePlatform = getJ2eePlatform(); 461 if(j2eePlatform != null){ 462 return j2eePlatform.isToolSupported(J2eePlatform.TOOL_JSR109); 463 } 464 return false; 465 } 466 467 private boolean isJsr109OldSupported(){ 468 J2eePlatform j2eePlatform = getJ2eePlatform(); 469 if(j2eePlatform != null){ 470 return j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSCOMPILE); 471 } 472 return false; 473 } 474 475 479 Task task; 480 } 481 | Popular Tags |