1 19 20 package org.netbeans.modules.websvc.wsitconf.wizard; 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.File ; 30 import java.net.URL ; 31 import java.util.ArrayList ; 32 import java.util.Collections ; 33 import java.util.List ; 34 import java.util.Map ; 35 import javax.lang.model.element.Modifier; 36 import javax.lang.model.element.TypeElement; 37 import org.netbeans.api.java.classpath.ClassPath; 38 import org.netbeans.api.java.source.CancellableTask; 39 import org.netbeans.api.java.source.JavaSource; 40 import org.netbeans.api.java.source.JavaSource.Phase; 41 import org.netbeans.api.java.source.TreeMaker; 42 import org.netbeans.api.java.source.WorkingCopy; 43 import org.netbeans.api.progress.ProgressHandle; 44 import org.netbeans.api.progress.ProgressHandleFactory; 45 import org.netbeans.api.project.Project; 46 import org.netbeans.modules.j2ee.api.ejbjar.EjbJar; 47 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 48 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 49 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform; 50 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 51 import org.netbeans.modules.web.api.webmodule.WebModule; 52 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModel; 53 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModelListener; 54 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModeler; 55 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlPort; 56 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlService; 57 import org.netbeans.modules.websvc.jaxws.api.JAXWSSupport; 58 import org.netbeans.modules.websvc.wsitconf.util.GenerationUtils; 59 import org.netbeans.spi.project.ui.templates.support.Templates; 60 import org.openide.DialogDisplayer; 61 import org.openide.ErrorManager; 62 import org.openide.NotifyDescriptor; 63 import org.openide.WizardDescriptor; 64 import org.openide.cookies.EditorCookie; 65 import org.openide.filesystems.FileObject; 66 import org.openide.filesystems.FileUtil; 67 import org.openide.loaders.DataObject; 68 import org.openide.util.NbBundle; 69 import org.openide.util.RequestProcessor; 70 71 public class STSWizardCreator { 72 73 protected static final int JSE_PROJECT_TYPE = 0; 74 protected static final int WEB_PROJECT_TYPE = 1; 75 protected static final int EJB_PROJECT_TYPE = 2; 76 77 private int projectType; 78 79 private Project project; 80 private WizardDescriptor wiz; 81 82 public boolean jwsdpSupported, wsitSupported, jsr109Supported, jsr109oldSupported; 83 84 public STSWizardCreator(Project project, WizardDescriptor wiz) { 85 this.project = project; 86 this.wiz = wiz; 87 } 88 89 public STSWizardCreator(Project project) { 90 this.project = project; 91 } 92 93 public void createSTS() { 94 final ProgressHandle handle = ProgressHandleFactory.createHandle( 95 NbBundle.getMessage(STSWizardCreator.class, "TXT_WebServiceGeneration")); 97 initProjectInfo(project); 98 99 Runnable r = new Runnable () { 100 public void run() { 101 try { 102 handle.start(100); 103 generateWsFromWsdl15(handle); 104 } catch (Exception e) { 105 handle.finish(); 107 String message = e.getLocalizedMessage(); 108 if(message != null) { 109 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 110 NotifyDescriptor nd = new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE); 111 DialogDisplayer.getDefault().notify(nd); 112 } else { 113 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 114 } 115 } 116 } 117 }; 118 RequestProcessor.getDefault().post(r); 119 } 120 121 private void initProjectInfo(Project project) { 122 JAXWSSupport wss = JAXWSSupport.getJAXWSSupport(project.getProjectDirectory()); 123 if (wss != null) { 124 Map properties = wss.getAntProjectHelper().getStandardPropertyEvaluator().getProperties(); 125 String serverInstance = (String )properties.get("j2ee.server.instance"); if (serverInstance != null) { 127 J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(serverInstance); 128 if (j2eePlatform != null) { 129 jwsdpSupported = j2eePlatform.isToolSupported(J2eePlatform.TOOL_JWSDP); 130 wsitSupported = j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSIT); 131 jsr109Supported = j2eePlatform.isToolSupported(J2eePlatform.TOOL_JSR109); 132 jsr109oldSupported = j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSCOMPILE); 133 } 134 } 135 } 136 137 WebModule wm = WebModule.getWebModule(project.getProjectDirectory()); 138 EjbJar em = EjbJar.getEjbJar(project.getProjectDirectory()); 139 if (em != null) 140 projectType = EJB_PROJECT_TYPE; 141 else if (wm != null) 142 projectType = WEB_PROJECT_TYPE; 143 else 144 projectType = JSE_PROJECT_TYPE; 145 } 146 147 private void generateWsFromWsdl15(final ProgressHandle handle) throws Exception { 148 String wsdlFilePath = (String ) wiz.getProperty(WizardProperties.WSDL_FILE_PATH); 149 File normalizedWsdlFilePath = FileUtil.normalizeFile(new File (wsdlFilePath)); 150 final URL wsdlURL = normalizedWsdlFilePath.toURI().toURL(); 152 final WsdlService service = (WsdlService) wiz.getProperty(WizardProperties.WSDL_SERVICE); 153 if (service==null) { 154 handle.finish(); 155 return; 156 } else { 157 final WsdlPort port = (WsdlPort) wiz.getProperty(WizardProperties.WSDL_PORT); 158 WsdlModeler wsdlModeler = (WsdlModeler) wiz.getProperty(WizardProperties.WSDL_MODELER); 160 wsdlModeler.generateWsdlModel(new WsdlModelListener() { 162 public void modelCreated(WsdlModel model) { 163 WsdlService service1 = model.getServiceByName(service.getName()); 164 WsdlPort port1 = service1.getPortByName(port.getName()); 165 port1.setSOAPVersion(port.getSOAPVersion()); 166 FileObject targetFolder = Templates.getTargetFolder(wiz); 167 String targetName = Templates.getTargetName(wiz); 168 try { 169 generateProviderImplClass(project, targetFolder, targetName, service1, port1, wsdlURL); 170 handle.finish(); 171 } catch (Exception ex) { 172 handle.finish(); 173 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex); 174 } 175 } 176 }); 177 } 178 } 179 180 public void generateProviderImplClass(Project project, FileObject targetFolder, 181 String targetName, final WsdlService service, final WsdlPort port, URL wsdlURL) throws Exception { 182 initProjectInfo(project); 183 184 String serviceID = service.getName(); 185 186 JAXWSSupport jaxWsSupport = JAXWSSupport.getJAXWSSupport(project.getProjectDirectory()); 187 188 FileObject implClassFo = GenerationUtils.createClass(targetFolder, targetName, null); 189 ClassPath classPath = ClassPath.getClassPath(implClassFo, ClassPath.SOURCE); 190 String serviceImplPath = classPath.getResourceName(implClassFo, '.', false); 191 String portJavaName = port.getJavaName(); 192 String artifactsPckg = portJavaName.substring(0, portJavaName.lastIndexOf(".")); 193 194 serviceID = jaxWsSupport.addService(targetName, serviceImplPath, wsdlURL.toString(), service.getName(), port.getName(), artifactsPckg, jsr109Supported && isJavaEE5orHigher(project)); 195 final String wsdlLocation = jaxWsSupport.getWsdlLocation(serviceID); 196 197 JavaSource targetSource = JavaSource.forFileObject(implClassFo); 198 CancellableTask<WorkingCopy> task = new CancellableTask<WorkingCopy>() { 199 200 public void run(WorkingCopy workingCopy) throws java.io.IOException { 201 workingCopy.toPhase(Phase.RESOLVED); 202 GenerationUtils genUtils = GenerationUtils.newInstance(workingCopy); 203 if (genUtils!=null) { 204 TreeMaker make = workingCopy.getTreeMaker(); 205 ClassTree javaClass = genUtils.getClassTree(); 206 207 ClassTree modifiedClass = genUtils.addImplementsClause(javaClass, "javax.xml.ws.Provider"); 210 211 TypeElement WSAn = workingCopy.getElements().getTypeElement("javax.xml.ws.WebServiceProvider"); List <ExpressionTree> attrs = new ArrayList <ExpressionTree>(); 214 attrs.add( 215 make.Assignment(make.Identifier("serviceName"), make.Literal(service.getName()))); attrs.add( 217 make.Assignment(make.Identifier("portName"), make.Literal(port.getName()))); attrs.add( 219 make.Assignment(make.Identifier("targetNamespace"), make.Literal(port.getNamespaceURI()))); attrs.add( 221 make.Assignment(make.Identifier("wsdlLocation"), make.Literal(wsdlLocation))); AnnotationTree WSAnnotation = make.Annotation( 223 make.QualIdent(WSAn), 224 attrs 225 ); 226 modifiedClass = genUtils.addAnnotation(modifiedClass, WSAnnotation); 227 228 TypeElement modeAn = workingCopy.getElements().getTypeElement("javax.xml.ws.ServiceMode"); List <ExpressionTree> attrsM = new ArrayList <ExpressionTree>(); 231 232 attrsM.add( 233 make.Assignment(make.Identifier("value"), make.Identifier("Mode.PAYLOAD"))); AnnotationTree modeAnnot = make.Annotation( 235 make.QualIdent(modeAn), 236 attrsM 237 ); 238 modifiedClass = genUtils.addAnnotation(modifiedClass, modeAnnot); 239 240 if (projectType == EJB_PROJECT_TYPE) { TypeElement StatelessAn = workingCopy.getElements().getTypeElement("javax.ejb.Stateless"); AnnotationTree StatelessAnnotation = make.Annotation( 244 make.QualIdent(StatelessAn), 245 Collections.<ExpressionTree>emptyList() 246 ); 247 modifiedClass = genUtils.addAnnotation(modifiedClass, StatelessAnnotation); 248 } 249 250 List <VariableTree> params = new ArrayList <VariableTree>(); 252 params.add(make.Variable( 254 make.Modifiers( 255 Collections.<Modifier>emptySet(), 256 Collections.<AnnotationTree>emptyList() 257 ), 258 "source", make.Identifier("javax.xml.transform.Source"), null )); 262 263 ModifiersTree methodModifiers = make.Modifiers( 265 Collections.<Modifier>singleton(Modifier.PUBLIC), 266 Collections.<AnnotationTree>emptyList() 267 ); 268 269 List <ExpressionTree> exc = new ArrayList <ExpressionTree>(); 270 271 MethodTree method = make.Method( 272 methodModifiers, "invoke", make.Identifier("javax.xml.transform.Source"), Collections.<TypeParameterTree>emptyList(), params, 277 exc, "{ //TODO implement this method\nthrow new UnsupportedOperationException(\"Not implemented yet.\") }", null ); 281 modifiedClass = make.addClassMember(modifiedClass, method); 282 workingCopy.rewrite(javaClass, modifiedClass); 283 } 284 } 285 286 public void cancel() { 287 } 288 }; 289 290 targetSource.runModificationTask(task).commit(); 291 292 DataObject dobj = DataObject.find(implClassFo); 294 openFileInEditor(dobj); 295 } 296 297 private static void openFileInEditor(DataObject dobj){ 298 final EditorCookie ec = (EditorCookie)dobj.getCookie(EditorCookie.class); 299 RequestProcessor.getDefault().post(new Runnable (){ 300 public void run(){ 301 ec.open(); 302 } 303 }, 1000); 304 } 305 306 312 public static boolean isJavaEE5orHigher(Project project) { 313 if (project == null) { 314 return false; 315 } 316 J2eeModuleProvider j2eeModuleProvider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class); 317 if (j2eeModuleProvider != null) { 318 J2eeModule j2eeModule = j2eeModuleProvider.getJ2eeModule(); 319 if (j2eeModule != null) { 320 Object type = j2eeModule.getModuleType(); 321 double version = Double.parseDouble(j2eeModule.getModuleVersion()); 322 if (J2eeModule.EJB.equals(type) && (version > 2.1)) { 323 return true; 324 }; 325 if (J2eeModule.WAR.equals(type) && (version > 2.4)) { 326 return true; 327 } 328 if (J2eeModule.CLIENT.equals(type) && (version > 1.4)) { 329 return true; 330 } 331 } 332 } 333 return false; 334 } 335 336 } 337 | Popular Tags |