1 19 20 package org.netbeans.modules.websvc.core; 21 22 import com.sun.source.tree.AnnotationTree; 23 import com.sun.source.tree.ClassTree; 24 import com.sun.source.tree.ExpressionTree; 25 import com.sun.source.tree.MethodTree; 26 import com.sun.source.tree.ModifiersTree; 27 import com.sun.source.tree.TypeParameterTree; 28 import com.sun.source.tree.VariableTree; 29 import java.io.IOException ; 30 import java.net.URI ; 31 import java.net.URL ; 32 import java.util.ArrayList ; 33 import java.util.Collections ; 34 import java.util.List ; 35 import java.util.Map ; 36 import javax.lang.model.element.Modifier; 37 import javax.lang.model.element.TypeElement; 38 import org.netbeans.api.java.classpath.ClassPath; 39 import org.netbeans.api.java.project.JavaProjectConstants; 40 import org.netbeans.api.java.source.CancellableTask; 41 import org.netbeans.api.java.source.JavaSource; 42 import org.netbeans.api.java.source.JavaSource.Phase; 43 import org.netbeans.api.java.source.TreeMaker; 44 import org.netbeans.api.java.source.WorkingCopy; 45 import org.netbeans.api.project.Project; 46 import org.netbeans.api.project.ant.AntArtifact; 47 import org.netbeans.modules.j2ee.api.ejbjar.EjbJar; 48 import org.netbeans.modules.j2ee.common.Util; 49 import org.netbeans.modules.j2ee.common.source.GenerationUtils; 50 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 51 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform; 52 import org.netbeans.modules.web.api.webmodule.WebModule; 53 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModel; 54 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlOperation; 55 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlParameter; 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.core.jaxws.bindings.model.BindingsModel; 59 import org.netbeans.modules.websvc.core.jaxws.bindings.model.BindingsModelFactory; 60 import org.netbeans.modules.websvc.core.jaxws.bindings.model.GlobalBindings; 61 import org.netbeans.modules.websvc.jaxws.api.JAXWSSupport; 62 import org.netbeans.modules.xml.xam.ModelSource; 63 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender; 64 import org.netbeans.spi.project.ant.AntArtifactProvider; 65 import org.openide.ErrorManager; 66 import org.openide.NotifyDescriptor; 67 import org.openide.WizardDescriptor; 68 import org.openide.cookies.EditorCookie; 69 import org.openide.filesystems.FileObject; 70 import org.openide.loaders.DataObject; 71 import org.openide.util.NbBundle; 72 import org.openide.util.RequestProcessor; 73 import java.util.Iterator ; 74 75 79 public class JaxWsUtils { 80 81 private static int projectType; 82 83 protected static final int JSE_PROJECT_TYPE = 0; 84 protected static final int WEB_PROJECT_TYPE = 1; 85 protected static final int EJB_PROJECT_TYPE = 2; 86 87 private static boolean jwsdpSupported = false; 88 private static boolean jsr109Supported = false; 89 90 91 public JaxWsUtils() { 92 } 93 94 96 public static void generateJaxWsImplementationClass(Project project, FileObject targetFolder, String targetName, WsdlModel wsdlModel, org.netbeans.modules.websvc.api.jaxws.project.config.Service service) throws Exception { 97 WsdlService wsdlService = wsdlModel.getServiceByName(service.getServiceName()); 98 WsdlPort wsdlPort = null; 99 if (wsdlService != null) 100 wsdlPort = wsdlService.getPortByName(service.getPortName()); 101 if (wsdlService!=null && wsdlPort!=null) { 102 String serviceID = service.getName(); 103 if(wsdlPort.isProvider()){ 104 generateProviderImplClass(project, targetFolder, targetName, wsdlService, wsdlPort, serviceID); 105 }else{ 106 generateJaxWsImplClass(project, targetFolder, targetName, null, wsdlService, wsdlPort, false, serviceID); 107 } 108 } 109 } 110 111 113 public static void generateJaxWsImplementationClass(Project project, FileObject targetFolder, String targetName, URL wsdlURL, WsdlService service, WsdlPort port) throws Exception { 114 generateJaxWsImplClass(project, targetFolder, targetName, wsdlURL, service, port, true, null); 115 } 116 117 119 public static void generateJaxWsArtifacts(Project project, FileObject targetFolder, String targetName, URL wsdlURL, String service, String port) throws Exception { 120 initProjectInfo(project); 121 JAXWSSupport jaxWsSupport = JAXWSSupport.getJAXWSSupport(project.getProjectDirectory()); 122 String artifactsPckg = "service."+targetName.toLowerCase(); ClassPath classPath = ClassPath.getClassPath(targetFolder, ClassPath.SOURCE); 124 String serviceImplPath = classPath.getResourceName(targetFolder, '.', false); 125 jaxWsSupport.addService(targetName, serviceImplPath+"."+targetName, wsdlURL.toExternalForm(), service, port, artifactsPckg, jsr109Supported && Util.isJavaEE5orHigher(project)); 126 } 127 128 129 public static void generateProviderImplClass(Project project, FileObject targetFolder, 130 String targetName, WsdlService service, WsdlPort port, String serviceID) throws Exception { 131 } 182 183 private static void generateJaxWsImplClass(Project project, FileObject targetFolder, String targetName, URL wsdlURL, final WsdlService service, final WsdlPort port, boolean addService, String serviceID) throws Exception { 184 initProjectInfo(project); 185 186 JAXWSSupport jaxWsSupport = JAXWSSupport.getJAXWSSupport(project.getProjectDirectory()); 190 191 FileObject implClassFo = GenerationUtils.createClass(targetFolder, targetName, null); 192 193 ClassPath classPath = ClassPath.getClassPath(implClassFo, ClassPath.SOURCE); 194 String serviceImplPath = classPath.getResourceName(implClassFo, '.', false); 195 String portJavaName = port.getJavaName(); 196 String artifactsPckg = portJavaName.substring(0, portJavaName.lastIndexOf(".")); 197 if (addService) { 198 serviceID = jaxWsSupport.addService(targetName, serviceImplPath, wsdlURL.toString(), service.getName(), port.getName(), artifactsPckg, jsr109Supported && Util.isJavaEE5orHigher(project)); 199 } 200 201 final String wsdlLocation = jaxWsSupport.getWsdlLocation(serviceID); 202 JavaSource targetSource = JavaSource.forFileObject(implClassFo); 203 CancellableTask<WorkingCopy> task = new CancellableTask<WorkingCopy>() { 204 205 public void run(WorkingCopy workingCopy) throws java.io.IOException { 206 workingCopy.toPhase(Phase.RESOLVED); 207 GenerationUtils genUtils = GenerationUtils.newInstance(workingCopy); 208 if (genUtils!=null) { 209 TreeMaker make = workingCopy.getTreeMaker(); 210 ClassTree javaClass = genUtils.getClassTree(); 211 212 ClassTree modifiedClass = genUtils.addImplementsClause(javaClass, port.getJavaName()); 214 215 TypeElement WSAn = workingCopy.getElements().getTypeElement("javax.jws.WebService"); List <ExpressionTree> attrs = new ArrayList <ExpressionTree>(); 218 attrs.add( 219 make.Assignment(make.Identifier("serviceName"), make.Literal(service.getName()))); attrs.add( 221 make.Assignment(make.Identifier("portName"), make.Literal(port.getName()))); attrs.add( 223 make.Assignment(make.Identifier("endpointInterface"), make.Literal(port.getJavaName()))); attrs.add( 225 make.Assignment(make.Identifier("targetNamespace"), make.Literal(port.getNamespaceURI()))); attrs.add( 227 make.Assignment(make.Identifier("wsdlLocation"), make.Literal(wsdlLocation))); 229 AnnotationTree WSAnnotation = make.Annotation( 230 make.QualIdent(WSAn), 231 attrs 232 ); 233 modifiedClass = genUtils.addAnnotation(modifiedClass, WSAnnotation); 234 235 if (projectType == EJB_PROJECT_TYPE) { TypeElement StatelessAn = workingCopy.getElements().getTypeElement("javax.ejb.Stateless"); AnnotationTree StatelessAnnotation = make.Annotation( 239 make.QualIdent(StatelessAn), 240 Collections.<ExpressionTree>emptyList() 241 ); 242 modifiedClass = genUtils.addAnnotation(modifiedClass, StatelessAnnotation); 243 } 244 245 List <WsdlOperation> operations = port.getOperations(); 246 for(WsdlOperation operation: operations) { 247 248 String returnType = operation.getReturnTypeName(); 250 251 List <WsdlParameter> parameters = operation.getParameters(); 253 List <VariableTree> params = new ArrayList <VariableTree>(); 254 for (WsdlParameter parameter:parameters) { 255 params.add(make.Variable( 258 make.Modifiers( 259 Collections.<Modifier>emptySet(), 260 Collections.<AnnotationTree>emptyList() 261 ), 262 parameter.getName(), make.Identifier(parameter.getTypeName()), null )); 266 } 267 268 Iterator <String > exceptions = operation.getExceptions(); 270 List <ExpressionTree> exc = new ArrayList <ExpressionTree>(); 271 while (exceptions.hasNext()) { 272 String exception = exceptions.next(); 273 TypeElement excEl = workingCopy.getElements().getTypeElement(exception); 274 exc.add(make.QualIdent(excEl)); 275 } 276 277 ModifiersTree methodModifiers = make.Modifiers( 279 Collections.<Modifier>singleton(Modifier.PUBLIC), 280 Collections.<AnnotationTree>emptyList() 281 ); 282 MethodTree method = make.Method( 283 methodModifiers, operation.getJavaName(), make.Identifier(returnType), Collections.<TypeParameterTree>emptyList(), params, 288 exc, "{ //TODO implement this method\nthrow new UnsupportedOperationException(\"Not implemented yet.\") }", null ); 292 293 modifiedClass = make.addClassMember(modifiedClass, method); 294 } 295 workingCopy.rewrite(javaClass, modifiedClass); 296 } 297 } 298 299 public void cancel() { 300 } 301 }; 302 targetSource.runModificationTask(task).commit(); 303 DataObject dobj = DataObject.find(implClassFo); 305 openFileInEditor(dobj); 306 307 } 404 405 private static void openFileInEditor(DataObject dobj){ 406 final EditorCookie ec = (EditorCookie)dobj.getCookie(EditorCookie.class); 407 RequestProcessor.getDefault().post(new Runnable (){ 408 public void run(){ 409 ec.open(); 410 } 411 }, 1000); 412 } 413 414 437 private static void initProjectInfo(Project project) { 438 JAXWSSupport wss = JAXWSSupport.getJAXWSSupport(project.getProjectDirectory()); 439 if (wss != null) { 440 Map properties = wss.getAntProjectHelper().getStandardPropertyEvaluator().getProperties(); 441 String serverInstance = (String )properties.get("j2ee.server.instance"); if (serverInstance != null) { 443 J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(serverInstance); 444 if (j2eePlatform != null) { 445 jwsdpSupported = j2eePlatform.isToolSupported(J2eePlatform.TOOL_JWSDP); jsr109Supported = j2eePlatform.isToolSupported(J2eePlatform.TOOL_JSR109); 447 } 448 } 449 } 450 451 WebModule wm = WebModule.getWebModule(project.getProjectDirectory()); 452 EjbJar em = EjbJar.getEjbJar(project.getProjectDirectory()); 453 if (em != null) 454 projectType = EJB_PROJECT_TYPE; 455 else if (wm != null) 456 projectType = WEB_PROJECT_TYPE; 457 else 458 projectType = JSE_PROJECT_TYPE; 459 } 460 461 public static boolean isProjectReferenceable(Project clientProject, Project targetProject) { 462 if (clientProject==targetProject) { 463 return true; 464 } else { 465 ProjectClassPathExtender pce = (ProjectClassPathExtender)targetProject.getLookup().lookup(ProjectClassPathExtender.class); 466 AntArtifactProvider antArtifactProvider = (AntArtifactProvider)clientProject.getLookup().lookup(AntArtifactProvider.class); 467 if (antArtifactProvider!=null) { 468 AntArtifact jarArtifact = getJarArtifact(antArtifactProvider); 469 if (jarArtifact!=null) return true; 470 } 471 return false; 472 } 473 } 474 475 public static boolean addProjectReference(Project clientProject, Project targetProject) { 476 try { 477 assert clientProject!=null && targetProject!=null; 478 if (clientProject!=targetProject) { 479 ProjectClassPathExtender pce = (ProjectClassPathExtender)targetProject.getLookup().lookup(ProjectClassPathExtender.class); 480 AntArtifactProvider antArtifactProvider = (AntArtifactProvider)clientProject.getLookup().lookup(AntArtifactProvider.class); 481 if (antArtifactProvider!=null) { 482 AntArtifact jarArtifact = getJarArtifact(antArtifactProvider); 483 if (jarArtifact!=null) { 484 URI [] artifactsUri = jarArtifact.getArtifactLocations(); 485 for (int i=0;i<artifactsUri.length;i++) { 486 pce.addAntArtifact(jarArtifact,artifactsUri[i]); 487 } 488 return true; 489 } 490 } 491 } else { 492 return true; 493 } 494 } catch (IOException ex) { 495 ErrorManager.getDefault().log(ex.getLocalizedMessage()); 496 } 497 return false; 498 } 499 500 private static AntArtifact getJarArtifact(AntArtifactProvider antArtifactProvider) { 501 AntArtifact[] artifacts = antArtifactProvider.getBuildArtifacts(); 502 for (int i=0;i<artifacts.length;i++) { 503 if (JavaProjectConstants.ARTIFACT_TYPE_JAR.equals(artifacts[i].getType())) return artifacts[i]; 504 } 505 return null; 506 } 507 508 public static class WsImportServiceFailedMessage extends NotifyDescriptor.Message { 509 public WsImportServiceFailedMessage(Throwable ex) { 510 super(NbBundle.getMessage(JaxWsUtils.class,"TXT_CannotGenerateService",ex.getLocalizedMessage()), 511 NotifyDescriptor.ERROR_MESSAGE); 512 } 513 } 514 515 public static class WsImportClientFailedMessage extends NotifyDescriptor.Message { 516 public WsImportClientFailedMessage(Throwable ex) { 517 super(NbBundle.getMessage(JaxWsUtils.class,"TXT_CannotGenerateClient",ex.getLocalizedMessage()), 518 NotifyDescriptor.ERROR_MESSAGE); 519 } 520 } 521 522 528 public static boolean addRelativeWsdlLocation(FileObject bindingFile, String relativePath) { 529 GlobalBindings gb = null; 530 531 ModelSource ms = org.netbeans.modules.xml.retriever.catalog.Utilities.getModelSource(bindingFile, true); 532 if(ms != null){ 533 BindingsModel bindingsModel = BindingsModelFactory.getDefault().getModel(ms); 534 if(bindingsModel != null){ 535 gb = bindingsModel.getGlobalBindings(); 536 if(gb != null){ 537 bindingsModel.startTransaction(); 538 gb.setWsdlLocation(relativePath); 539 bindingsModel.endTransaction(); 540 return true; 541 } 542 } 543 } 544 return false; 545 } 546 548 public static boolean isJavaPackage(String pkg) { 549 boolean result = false; 550 551 if(pkg != null && pkg.length() > 0) { 552 int state = 0; 553 for(int i = 0, pkglength = pkg.length(); i < pkglength && state < 2; i++) { 554 switch(state) { 555 case 0: 556 if(Character.isJavaIdentifierStart(pkg.charAt(i))) { 557 state = 1; 558 } else { 559 state = 2; 560 } 561 break; 562 case 1: 563 if(pkg.charAt(i) == '.') { 564 state = 0; 565 } else if(!Character.isJavaIdentifierPart(pkg.charAt(i))) { 566 state = 2; 567 } 568 break; 569 } 570 } 571 572 if(state == 1) { 573 result = true; 574 } 575 } 576 577 return result; 578 } 579 580 582 public static boolean isJavaIdentifier(String id) { 583 boolean result = true; 584 585 if(id == null || id.length() == 0 || !Character.isJavaIdentifierStart(id.charAt(0))) { 586 result = false; 587 } else { 588 for(int i = 1, idlength = id.length(); i < idlength; i++) { 589 if(!Character.isJavaIdentifierPart(id.charAt(i))) { 590 result = false; 591 break; 592 } 593 } 594 } 595 596 return result; 597 } 598 599 604 public static String [] createSteps(String [] before, WizardDescriptor.Panel[] panels) { 605 int diff = 0; 608 if (before == null) { 609 before = new String [0]; 610 } else if (before.length > 0) { 611 diff = ("...".equals(before[before.length - 1])) ? 1 : 0; } 613 String [] res = new String [ (before.length - diff) + panels.length]; 614 for (int i = 0; i < res.length; i++) { 615 if (i < (before.length - diff)) { 616 res[i] = before[i]; 617 } else { 618 res[i] = panels[i - before.length + diff].getComponent().getName(); 619 } 620 } 621 return res; 622 } 623 } 624 | Popular Tags |