1 19 package org.netbeans.modules.websvc.core.jaxws.nodes; 20 21 import java.io.File ; 22 import java.net.URI ; 23 import java.net.URISyntaxException ; 24 import java.net.URL ; 25 import java.net.UnknownHostException ; 26 import java.util.*; 27 import java.util.Set ; 28 import javax.lang.model.element.AnnotationMirror; 29 import javax.lang.model.element.Element; 30 import javax.lang.model.element.ExecutableElement; 31 import javax.lang.model.element.Modifier; 32 import javax.lang.model.element.TypeElement; 33 import javax.lang.model.util.ElementFilter; 34 import javax.swing.SwingUtilities ; 35 import javax.xml.parsers.ParserConfigurationException ; 36 import javax.xml.transform.stream.StreamSource ; 37 import org.apache.tools.ant.module.api.support.ActionUtils; 38 import org.netbeans.api.java.source.CancellableTask; 39 import org.netbeans.api.java.source.CompilationController; 40 import org.netbeans.api.java.source.JavaSource; 41 import org.openide.nodes.AbstractNode; 42 import static org.netbeans.api.java.source.JavaSource.Phase; 43 import org.netbeans.api.project.FileOwnerQuery; 44 import org.netbeans.api.project.Project; 45 import org.netbeans.modules.j2ee.common.source.SourceUtils; 46 import org.netbeans.modules.websvc.api.jaxws.project.GeneratedFilesHelper; 47 import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel; 48 import org.netbeans.modules.websvc.core.JaxWsUtils; 49 import org.netbeans.modules.websvc.jaxws.api.JAXWSSupport; 50 import org.netbeans.modules.websvc.api.jaxws.project.config.Service; 51 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModel; 52 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModelListener; 53 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModeler; 54 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModelerFactory; 55 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlOperation; 56 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlPort; 57 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlService; 58 import org.netbeans.modules.websvc.api.jaxws.project.WSUtils; 59 import org.netbeans.modules.websvc.jaxws.api.WsdlWrapperGenerator; 60 import org.netbeans.modules.websvc.jaxws.api.WsdlWrapperHandler; 61 import org.openide.DialogDisplayer; 62 import org.openide.ErrorManager; 63 import org.openide.execution.ExecutorTask; 64 import org.openide.filesystems.FileLock; 65 import org.openide.filesystems.FileObject; 66 import org.openide.filesystems.FileStateInvalidException; 67 import org.openide.filesystems.FileUtil; 68 import org.openide.nodes.Children; 69 import org.openide.nodes.Node; 70 import org.openide.util.NbBundle; 71 import java.io.IOException ; 72 import org.netbeans.modules.websvc.api.jaxws.project.config.Binding; 73 import org.xml.sax.SAXException ; 74 75 79 80 public class JaxWsChildren extends Children.Keys{ 81 private static final java.awt.Image OPERATION_BADGE = 82 org.openide.util.Utilities.loadImage( "org/netbeans/modules/websvc/core/webservices/ui/resources/wsoperation.png" ); 84 private FileObject implClass; 85 private Service service; 86 private FileObject srcRoot; 87 88 private WsdlModel wsdlModel; 89 private WsdlModeler wsdlModeler; 90 private boolean modelGenerationFinished; 91 92 93 public JaxWsChildren(Service service, FileObject srcRoot, FileObject implClass) { 94 super(); 95 this.service = service; 96 this.srcRoot=srcRoot; 97 this.implClass = implClass; 98 } 99 100 107 private List<ExecutableElement> getPublicMethods(CompilationController controller, TypeElement classElement) throws IOException { 108 List<? extends Element> members = classElement.getEnclosedElements(); 109 List<ExecutableElement> methods = ElementFilter.methodsIn(members); 110 List<ExecutableElement> publicMethods = new ArrayList<ExecutableElement>(); 111 for (ExecutableElement method:methods) { 112 Set <Modifier> modifiers = method.getModifiers(); 113 if (modifiers.contains(Modifier.PUBLIC)) { 114 publicMethods.add(method); 115 } 116 } 117 return publicMethods; 118 } 119 120 protected void addNotify() { 121 super.addNotify(); 122 if (isFromWsdl()) { 123 try { 124 FileObject localWsdlFolder = getJAXWSSupport().getLocalWsdlFolderForService(service.getName(),false); 125 assert localWsdlFolder!=null:"Cannot find folder for local wsdl file"; FileObject wsdlFo = 127 localWsdlFolder.getFileObject(service.getLocalWsdlFile()); 128 if (wsdlFo==null) return; 129 wsdlModeler = WsdlModelerFactory.getDefault().getWsdlModeler(wsdlFo.getURL()); 130 String packageName = service.getPackageName(); 131 if (packageName!=null && service.isPackageNameForceReplace()) { 132 wsdlModeler.setPackageName(packageName); 134 } else { 135 wsdlModeler.setPackageName(null); 136 } 137 JAXWSSupport support = getJAXWSSupport(); 138 wsdlModeler.setCatalog(support.getCatalog()); 139 setBindings(support,wsdlModeler,service); 140 modelGenerationFinished=false; 141 ((JaxWsNode)getNode()).changeIcon(); 142 wsdlModeler.generateWsdlModel(new WsdlModelListener() { 143 public void modelCreated(WsdlModel model) { 144 wsdlModel=model; 145 modelGenerationFinished=true; 146 ((JaxWsNode)getNode()).changeIcon(); 147 if (model==null) { 148 DialogDisplayer.getDefault().notify( 149 new JaxWsUtils.WsImportServiceFailedMessage(wsdlModeler.getCreationException())); 150 } 151 updateKeys(); 152 } 153 }); 154 } catch (FileStateInvalidException ex) { 155 ErrorManager.getDefault().log(ex.getLocalizedMessage()); 156 updateKeys(); 157 } 158 } else { 159 assert(implClass != null); 160 updateKeys(); 164 } 165 } 166 167 193 203 private void updateKeys() { 204 if (isFromWsdl()) { 205 List keys = new ArrayList(); 206 if (wsdlModel!=null) { 207 WsdlService wsdlService = wsdlModel.getServiceByName(service.getServiceName()); 208 if (wsdlService!=null) { 209 WsdlPort wsdlPort = wsdlService.getPortByName(service.getPortName()); 210 if (wsdlPort!=null) 211 keys = wsdlPort.getOperations(); 212 } 213 } 214 setKeys(keys); 215 } else { 216 SwingUtilities.invokeLater(new Runnable () { 217 public void run() { 218 final List[] keys = new List[]{new ArrayList()}; 219 if (implClass != null) { 220 JavaSource javaSource = JavaSource.forFileObject(implClass); 221 if (javaSource!=null) { 222 CancellableTask<CompilationController> task = new CancellableTask<CompilationController>() { 223 public void run(CompilationController controller) throws IOException { 224 controller.toPhase(Phase.ELEMENTS_RESOLVED); 225 SourceUtils srcUtils = SourceUtils.newInstance(controller); 226 if (srcUtils!=null) { 227 List<ExecutableElement> publicMethods = getPublicMethods(controller, srcUtils.getTypeElement()); 231 List<ExecutableElement> wsOperations = new ArrayList<ExecutableElement>(); 232 boolean foundWebMethodAnnotation=false; 233 for(ExecutableElement method:publicMethods) { 234 List<? extends AnnotationMirror> annotations = method.getAnnotationMirrors(); 235 boolean hasWebMethodAnnotation=false; 236 for (AnnotationMirror an:annotations) { 237 TypeElement webMethodEl = controller.getElements().getTypeElement("javax.jws.WebMethod"); if (webMethodEl!=null && controller.getTypes().isSameType(webMethodEl.asType(), an.getAnnotationType())) { 239 hasWebMethodAnnotation=true; 240 break; 241 } 242 } 243 if (hasWebMethodAnnotation) { 244 if (!foundWebMethodAnnotation) { 245 foundWebMethodAnnotation=true; 246 if (wsOperations.size()>0) wsOperations.clear(); 249 } 250 wsOperations.add(method); 251 } else if (!foundWebMethodAnnotation) { 252 wsOperations.add(method); 254 } 255 } keys[0] = wsOperations; 257 } 258 } 259 260 public void cancel() {} 261 }; 262 try { 263 javaSource.runUserActionTask(task, true); 264 } catch (IOException ex) { 265 ErrorManager.getDefault().notify(ex); 266 } 267 } 268 269 } 274 setKeys(keys[0]); 275 } 276 }); 277 } 278 } 279 280 protected Node[] createNodes(Object key) { 281 if(key instanceof WsdlOperation) { 282 return new Node[] {new OperationNode((WsdlOperation)key)}; 283 } else if(key instanceof ExecutableElement) { 284 final ExecutableElement method = (ExecutableElement)key; 289 Node n = new AbstractNode(Children.LEAF) { 290 291 @java.lang.Override 292 public java.awt.Image getIcon(int type) { 293 return OPERATION_BADGE; 294 } 295 296 @Override 297 public String getDisplayName() { 298 return method.getSimpleName().toString(); 299 } 300 }; 301 302 return new Node[]{n}; 303 } 304 return new Node[0]; 305 } 306 307 private boolean isFromWsdl() { 308 return service.getWsdlUrl()!=null; 309 } 310 311 private JAXWSSupport getJAXWSSupport() { 312 return JAXWSSupport.getJAXWSSupport(srcRoot); 313 } 314 315 private void setBindings(JAXWSSupport support, WsdlModeler wsdlModeler, Service service) { 316 Binding[] extbindings = service.getBindings(); 317 if (extbindings==null || extbindings.length==0) { 318 wsdlModeler.setJAXBBindings(null); 319 return; 320 } 321 String [] bindingFiles = new String [extbindings.length]; 322 for(int i = 0; i < extbindings.length; i++){ 323 bindingFiles[i] = extbindings[i].getFileName(); 324 } 325 332 FileObject bindingsFolder = support.getBindingsFolderForService(getNode().getName(),true); 333 List<URL > list = new ArrayList<URL >(); 334 for (int i=0;i<bindingFiles.length;i++) { 335 FileObject fo = bindingsFolder.getFileObject(bindingFiles[i]); 336 try { 337 list.add(fo.getURL()); 338 } catch (FileStateInvalidException ex) { 339 } 341 } 342 URL [] bindings = new URL [list.size()]; 343 list.<URL >toArray(bindings); 344 wsdlModeler.setJAXBBindings(bindings); 345 } 346 347 void refreshKeys(boolean downloadWsdl, final boolean refreshImplClass) { 348 if (!isFromWsdl()) return; 349 super.addNotify(); 350 List keys=null; 351 try { 352 JAXWSSupport support = getJAXWSSupport(); 354 355 if (downloadWsdl) { 356 String serviceName = getNode().getName(); 357 FileObject xmlResorcesFo = support.getLocalWsdlFolderForService(serviceName,true); 358 FileObject localWsdl = null; 359 try { 360 localWsdl = WSUtils.retrieveResource( 361 xmlResorcesFo, 362 new URI (service.getWsdlUrl())); 363 FileObject wsdlFolder = getWsdlFolderForService(support, serviceName); 365 WSUtils.copyFiles(xmlResorcesFo, wsdlFolder); 366 } catch (URISyntaxException ex) { 367 ErrorManager.getDefault().notify(ex); 368 } catch (UnknownHostException ex) { 369 ErrorManager.getDefault().annotate(ex, 370 NbBundle.getMessage(JaxWsClientChildren.class,"MSG_ConnectionProblem")); 371 return; 372 } catch (IOException ex) { 373 ErrorManager.getDefault().annotate(ex, 374 NbBundle.getMessage(JaxWsClientChildren.class,"MSG_ConnectionProblem")); 375 return; 376 } 377 378 if (localWsdl!=null) { 380 WsdlWrapperHandler handler = null; 381 try { 382 handler = WsdlWrapperGenerator.parse(localWsdl.getURL().toExternalForm()); 383 } catch (ParserConfigurationException ex) { 384 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex); 385 } catch (SAXException ex) { 386 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex); 387 } catch (IOException ex) { 388 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex); 389 } 390 if (!handler.isServiceElement()) { 391 StreamSource source = new StreamSource (localWsdl.getURL().toExternalForm()); 392 try { 393 File wrapperWsdlFile = new File (FileUtil.toFile(localWsdl.getParent()), WsdlWrapperGenerator.getWrapperName(localWsdl.getURL())); 395 if(!wrapperWsdlFile.exists()) { 396 try { 397 wrapperWsdlFile.createNewFile(); 398 } catch(IOException ex) { 399 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION,ex); 400 } 401 } 402 if (wrapperWsdlFile.exists()) { 403 WsdlWrapperGenerator.generateWrapperWSDLContent(wrapperWsdlFile, source, handler.getTargetNsPrefix(),localWsdl.getNameExt()); 404 } 405 } catch (IOException ex) { 406 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex); 407 } 408 } 409 } 410 } 411 FileObject wsdlFo = 412 getJAXWSSupport().getLocalWsdlFolderForService(service.getName(),false).getFileObject(service.getLocalWsdlFile()); 413 wsdlModeler = WsdlModelerFactory.getDefault().getWsdlModeler(wsdlFo.getURL()); 414 String packageName = service.getPackageName(); 415 if (packageName!=null && service.isPackageNameForceReplace()) { 416 wsdlModeler.setPackageName(packageName); 418 } else { 419 wsdlModeler.setPackageName(null); 420 } 421 wsdlModeler.setCatalog(support.getCatalog()); 422 setBindings(support, wsdlModeler, service); 423 424 regenerateJavaArtifacts(); 426 428 modelGenerationFinished=false; 429 ((JaxWsNode)getNode()).changeIcon(); 430 wsdlModeler.generateWsdlModel(new WsdlModelListener() { 431 public void modelCreated(WsdlModel model) { 432 wsdlModel=model; 433 modelGenerationFinished=true; 434 ((JaxWsNode)getNode()).changeIcon(); 435 if (model==null) { 436 DialogDisplayer.getDefault().notify( 437 new JaxWsUtils.WsImportServiceFailedMessage(wsdlModeler.getCreationException())); 438 } 439 updateKeys(); 440 if (model!=null) { 441 try { 442 String serviceName = service.getServiceName(); 444 String portName = service.getPortName(); 445 WsdlService wsdlService = model.getServiceByName(serviceName); 446 boolean jaxWsModelChanged=false; 447 if (wsdlService==null) { 448 wsdlService = (WsdlService)model.getServices().get(0); 449 service.setServiceName(wsdlService.getName()); 450 jaxWsModelChanged=true; 451 } 452 WsdlPort wsdlPort = wsdlService.getPortByName(portName); 453 if (wsdlPort==null) { 454 wsdlPort = (WsdlPort)wsdlService.getPorts().get(0); 455 service.setPortName(wsdlPort.getName()); 456 jaxWsModelChanged=true; 457 } 458 459 String oldPkgName = service.getPackageName(); 461 if (wsdlService!=null && oldPkgName!=null && !service.isPackageNameForceReplace()) { 462 String javaName = wsdlService.getJavaName(); 463 int dotPosition = javaName.lastIndexOf("."); 464 if (dotPosition>=0) { 465 String newPkgName = javaName.substring(0,dotPosition); 466 if (!oldPkgName.equals(newPkgName)) { 467 service.setPackageName(newPkgName); 468 jaxWsModelChanged=true; 469 } 470 } 471 } 472 473 if (jaxWsModelChanged) { 475 Project project = FileOwnerQuery.getOwner(srcRoot); 476 if (project!=null) { 477 JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class); 478 if (jaxWsModel!=null) jaxWsModel.write(); 479 } 480 481 } 482 if (refreshImplClass) { 483 String implClass = service.getImplementationClass(); 485 FileObject oldImplClass = srcRoot.getFileObject(implClass.replace('.','/')+".java"); FileObject oldCopy = srcRoot.getFileObject(implClass.replace('.','/')+".java.old"); int index = implClass.lastIndexOf("."); 488 FileObject folder = index>0?srcRoot.getFileObject(implClass.substring(0,index).replace('.','/')):srcRoot; 489 if (folder!=null) { 490 String name = (index>=0?implClass.substring(index+1):implClass); 491 if (oldImplClass!=null) { 492 if (oldCopy!=null) oldCopy.delete(); 493 FileUtil.copyFile(oldImplClass, folder, name+".java", "old"); oldImplClass.delete(); 495 } 496 JaxWsUtils.generateJaxWsImplementationClass(FileOwnerQuery.getOwner(srcRoot), 497 folder, name, model, service); 498 JaxWsNode parent = (JaxWsNode)getNode(); 499 FileObject newImplClass = srcRoot.getFileObject(implClass.replace('.','/')+".java"); if (newImplClass!=null) { 501 JaxWsChildren.this.implClass=newImplClass; 502 } 503 parent.refreshImplClass(); 504 505 } 506 } 507 } catch (Exception ex) { 508 ErrorManager.getDefault().notify(ErrorManager.ERROR,ex); 509 } 510 } 511 } 512 }); 513 } catch (FileStateInvalidException ex) { 514 ErrorManager.getDefault().log(ex.getLocalizedMessage()); 515 } 516 } 517 518 private void regenerateJavaArtifacts() { 519 Project project = FileOwnerQuery.getOwner(srcRoot); 520 if (project!=null) { 521 FileObject buildImplFo = project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_IMPL_XML_PATH); 522 try { 523 String name = service.getName(); 524 ExecutorTask wsimportTask = 525 ActionUtils.runTarget(buildImplFo, 526 new String []{"wsimport-service-clean-"+name,"wsimport-service-"+name},null); wsimportTask.waitFinished(); 528 } catch (IOException ex) { 529 ErrorManager.getDefault().log(ex.getLocalizedMessage()); 530 } catch (IllegalArgumentException ex) { 531 ErrorManager.getDefault().log(ex.getLocalizedMessage()); 532 } 533 } 534 } 535 536 612 638 private FileObject getWsdlFolderForService(JAXWSSupport support, String name) throws IOException { 639 FileObject globalWsdlFolder = support.getWsdlFolder(true); 640 FileObject oldWsdlFolder = globalWsdlFolder.getFileObject(name); 641 if (oldWsdlFolder!=null) { 642 FileLock lock = oldWsdlFolder.lock(); 643 try { 644 oldWsdlFolder.delete(lock); 645 } finally { 646 lock.releaseLock(); 647 } 648 } 649 return globalWsdlFolder.createFolder(name); 650 } 651 652 WsdlModeler getWsdlModeler() { 653 return wsdlModeler; 654 } 655 656 boolean isModelGenerationFinished() { 657 return modelGenerationFinished; 658 } 659 660 } 661 | Popular Tags |