1 19 20 package org.netbeans.modules.websvc.jaxws.spi; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.net.URI ; 26 import java.net.URISyntaxException ; 27 import java.net.URL ; 28 import java.net.UnknownHostException ; 29 import java.util.ArrayList ; 30 import java.util.Enumeration ; 31 import java.util.List ; 32 import javax.xml.parsers.ParserConfigurationException ; 33 import javax.xml.transform.stream.StreamSource ; 34 import org.apache.tools.ant.module.api.support.ActionUtils; 35 import org.netbeans.api.project.Project; 36 import org.netbeans.modules.websvc.api.jaxws.project.GeneratedFilesHelper; 37 import org.netbeans.modules.websvc.api.jaxws.project.WSUtils; 38 import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel; 39 import org.netbeans.modules.websvc.api.jaxws.project.config.Service; 40 import org.netbeans.modules.websvc.api.jaxws.project.config.ServiceAlreadyExistsExeption; 41 import org.netbeans.modules.websvc.jaxws.api.WsdlWrapperGenerator; 42 import org.netbeans.modules.websvc.jaxws.api.WsdlWrapperHandler; 43 import org.netbeans.spi.project.support.ant.AntProjectHelper; 44 import org.openide.ErrorManager; 45 import org.openide.execution.ExecutorTask; 46 import org.openide.filesystems.FileLock; 47 import org.openide.filesystems.FileObject; 48 import org.openide.filesystems.FileStateInvalidException; 49 import org.openide.filesystems.FileSystem.AtomicAction; 50 import org.openide.filesystems.FileUtil; 51 import org.xml.sax.SAXException ; 52 53 59 public abstract class ProjectJAXWSSupport implements JAXWSSupportImpl { 60 private Project project; 61 private AntProjectHelper antProjectHelper; 62 63 64 public ProjectJAXWSSupport(Project project, AntProjectHelper antProjectHelper) { 65 this.project = project; 66 this.antProjectHelper = antProjectHelper; 67 } 68 69 public void removeService(String serviceName) { 70 JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class); 71 if (jaxWsModel!=null) { 72 Service service = jaxWsModel.findServiceByName(serviceName); 73 if (service!=null) { 74 jaxWsModel.removeService(serviceName); 76 writeJaxWsModel(jaxWsModel); 77 WSUtils.removeImplClass(project,service.getImplementationClass()); 78 } 79 } 80 } 81 87 public void serviceFromJavaRemoved(String serviceName) {} 88 89 public boolean isFromWSDL(String serviceName) { 90 JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class); 91 Service service = jaxWsModel.findServiceByName(serviceName); 92 if (service!=null && service.getWsdlUrl()!=null) return true; 93 else return false; 94 } 95 96 100 public String getServiceImpl(String serviceName) { 101 JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class); 102 if (jaxWsModel!=null) { 103 Service service = jaxWsModel.findServiceByName(serviceName); 104 return service==null?null:service.getImplementationClass(); 105 } 106 return null; 107 } 108 109 public AntProjectHelper getAntProjectHelper() { 110 return antProjectHelper; 111 } 112 113 public void addService(String serviceName, String serviceImpl, boolean isJsr109) { 114 JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class); 115 if (jaxWsModel!=null) { 116 Boolean value = jaxWsModel.getJsr109(); 117 if((value == null || Boolean.TRUE.equals(value)) && !isJsr109){ 118 jaxWsModel.setJsr109(Boolean.FALSE); 119 writeJaxWsModel(jaxWsModel); 120 } else if (Boolean.FALSE.equals(value) && isJsr109) { 121 jaxWsModel.setJsr109(Boolean.TRUE); 122 writeJaxWsModel(jaxWsModel); 123 } 124 } 125 126 if(!isJsr109 ){ 127 try{ 128 addJaxwsArtifacts(project, serviceName, serviceImpl); 129 } catch(Exception e){ 130 ErrorManager.getDefault().notify(e); } 132 } 133 } 134 135 protected abstract void addJaxwsArtifacts(Project project, String wsName, 136 String serviceImpl) throws Exception ; 137 138 143 144 public String addService(String name, String serviceImpl, String wsdlUrl, String serviceName, 145 String portName, String packageName, boolean isJsr109) { 146 JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class); 147 if (jaxWsModel!=null) { 148 String finalServiceName = WSUtils.findProperServiceName(name, jaxWsModel); 149 boolean serviceAdded=false; 150 151 List <FileObject> subfolders = null; 154 FileObject serviceArtifactsFolder = project.getProjectDirectory().getFileObject("build/generated/wsimport/service"); if (serviceArtifactsFolder!=null) { 156 Enumeration en = serviceArtifactsFolder.getChildren(true); 157 subfolders = new ArrayList <FileObject>(); 158 while (en.hasMoreElements()) { 159 FileObject ch = (FileObject) en.nextElement(); 160 if (ch.isFolder()) subfolders.add(ch); 161 } 162 } 163 164 FileObject localWsdl=null; 165 try { 166 FileObject xmlResorcesFo = getLocalWsdlFolderForService(finalServiceName,true); 168 localWsdl = WSUtils.retrieveResource( 169 xmlResorcesFo, 170 new URI (wsdlUrl)); 171 if (localWsdl!=null) { 172 173 WsdlWrapperHandler handler = null; 174 try { 175 handler = WsdlWrapperGenerator.parse(localWsdl.getURL().toExternalForm()); 176 } catch (ParserConfigurationException ex) { 177 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex); 178 } catch (SAXException ex) { 179 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex); 180 } catch (IOException ex) { 181 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex); 182 } 183 if (!handler.isServiceElement()) { 184 StreamSource source = new StreamSource (localWsdl.getURL().toExternalForm()); 185 try { 186 File wrapperWsdlFile = new File (FileUtil.toFile(localWsdl.getParent()), WsdlWrapperGenerator.getWrapperName(localWsdl.getURL())); 188 if(!wrapperWsdlFile.exists()) { 189 try { 190 wrapperWsdlFile.createNewFile(); 191 } catch(IOException ex) { 192 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION,ex); 193 } 194 } 195 if (wrapperWsdlFile.exists()) { 196 WsdlWrapperGenerator.generateWrapperWSDLContent(wrapperWsdlFile, source, handler.getTargetNsPrefix(),localWsdl.getNameExt()); 197 localWsdl=FileUtil.toFileObject(wrapperWsdlFile); 198 } 199 } catch (IOException ex) { 200 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex); 201 } 202 } 203 204 Boolean value = jaxWsModel.getJsr109(); 205 if((value == null || Boolean.TRUE.equals(value)) && !isJsr109){ 206 jaxWsModel.setJsr109(Boolean.FALSE); 207 } else if (Boolean.FALSE.equals(value) && isJsr109) { 208 jaxWsModel.setJsr109(Boolean.TRUE); 209 } 210 Service service=null; 211 try { 212 service = jaxWsModel.addService(finalServiceName, serviceImpl, wsdlUrl, serviceName, portName, packageName); 213 } catch (ServiceAlreadyExistsExeption ex) { 214 } 216 String localWsdlUrl = FileUtil.getRelativePath(xmlResorcesFo, localWsdl); 217 service.setLocalWsdlFile(localWsdlUrl); 218 FileObject catalog = getCatalogFileObject(); 219 if (catalog!=null) service.setCatalogFile(CATALOG_FILE); 220 writeJaxWsModel(jaxWsModel); 221 serviceAdded=true; 222 FileObject wsdlFolder = getWsdlFolderForService(finalServiceName); 224 WSUtils.copyFiles(xmlResorcesFo, wsdlFolder); 225 } 226 227 } catch (URISyntaxException ex) { 228 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION,ex); 229 } catch (UnknownHostException ex) { 230 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION,ex); 231 } catch (IOException ex) { 232 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION,ex); 233 } 234 235 if (serviceAdded) { 236 if(!isJsr109 ){ 237 try{ 238 addJaxwsArtifacts(project, serviceName, serviceImpl); 239 } catch(Exception e){ 240 ErrorManager.getDefault().notify(e); } 242 } 243 FileObject buildImplFo = project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_IMPL_XML_PATH); 244 try { 245 ExecutorTask wsimportTask = 246 ActionUtils.runTarget(buildImplFo,new String []{"wsimport-service-"+finalServiceName},null); wsimportTask.waitFinished(); 248 249 if (serviceArtifactsFolder==null) 251 serviceArtifactsFolder = project.getProjectDirectory().getFileObject("build/generated/wsimport/service/"+packageName.replace('.','/')); 252 if (serviceArtifactsFolder!=null) { 253 serviceArtifactsFolder.refresh(); 254 if (subfolders!=null) { 256 for (int i=0;i<subfolders.size();i++) { 257 ((FileObject) subfolders.get(i)).refresh(); 258 } 259 } 260 } 261 } catch (IOException ex) { 262 ErrorManager.getDefault().log(ex.getLocalizedMessage()); 263 } catch (IllegalArgumentException ex) { 264 ErrorManager.getDefault().log(ex.getLocalizedMessage()); 265 } 266 return finalServiceName; 267 } 268 } 269 return null; 270 } 271 272 275 public List <Service> getServices() { 276 JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class); 277 if (jaxWsModel!=null) { 278 Service[] services = jaxWsModel.getServices(); 279 if (services!=null) { 280 List <Service> list = new ArrayList <Service>(); 281 for (int i=0;i<services.length;i++) { 282 list.add(services[i]); 283 } 284 return list; 285 } 286 } 287 return new ArrayList <Service>(); 288 } 289 290 private void writeJaxWsModel(final JaxWsModel jaxWsModel) { 291 try { 292 final FileObject jaxWsFo = project.getProjectDirectory().getFileObject("nbproject/jax-ws.xml"); jaxWsFo.getFileSystem().runAtomicAction(new AtomicAction() { 294 public void run() { 295 FileLock lock=null; 296 OutputStream os=null; 297 try { 298 lock = jaxWsFo.lock(); 299 os = jaxWsFo.getOutputStream(lock); 300 jaxWsModel.write(os); 301 os.close(); 302 } catch (java.io.IOException ex) { 303 ErrorManager.getDefault().notify(ex); 304 } finally { 305 if (os!=null) { 306 try { 307 os.close(); 308 } catch (IOException ex) {} 309 } 310 if (lock!=null) lock.releaseLock(); 311 } 312 } 313 }); 314 } catch (IOException ex) { 315 ErrorManager.getDefault().notify(ex); 316 } 317 } 318 319 322 public FileObject getLocalWsdlFolderForService(String serviceName, boolean createFolder) { 323 return getArtifactsFolder(serviceName, createFolder, true); 324 } 325 328 public FileObject getBindingsFolderForService(String serviceName, boolean createFolder) { 329 return getArtifactsFolder(serviceName, createFolder, false); 330 } 331 332 private FileObject getArtifactsFolder(String serviceName, boolean createFolder, boolean forWsdl) { 333 String folderName = forWsdl?"wsdl":"bindings"; FileObject root = getXmlArtifactsRoot(); 335 if (root==null) { 336 assert !createFolder : "Cannot create XML artifacts folder"; return null; 338 } 339 FileObject wsdlLocalFolder = root.getFileObject(XML_RESOURCES_FOLDER+"/"+SERVICES_LOCAL_FOLDER+"/"+serviceName+"/"+folderName); if (wsdlLocalFolder==null && createFolder) { 341 try { 342 FileObject xmlLocalFolder = root.getFileObject(XML_RESOURCES_FOLDER); 343 if (xmlLocalFolder==null) xmlLocalFolder = root.createFolder(XML_RESOURCES_FOLDER); 344 FileObject servicesLocalFolder = xmlLocalFolder.getFileObject(SERVICES_LOCAL_FOLDER); 345 if (servicesLocalFolder==null) servicesLocalFolder = xmlLocalFolder.createFolder(SERVICES_LOCAL_FOLDER); 346 FileObject serviceLocalFolder = servicesLocalFolder.getFileObject(serviceName); 347 if (serviceLocalFolder==null) serviceLocalFolder = servicesLocalFolder.createFolder(serviceName); 348 wsdlLocalFolder=serviceLocalFolder.getFileObject(folderName); 349 if (wsdlLocalFolder==null) wsdlLocalFolder = serviceLocalFolder.createFolder(folderName); 350 } catch (IOException ex) { 351 return null; 352 } 353 } 354 return wsdlLocalFolder; 355 } 356 357 359 protected FileObject getXmlArtifactsRoot() { 360 return project.getProjectDirectory(); 361 } 362 363 private FileObject getCatalogFileObject() { 364 return project.getProjectDirectory().getFileObject(CATALOG_FILE); 365 } 366 367 public URL getCatalog() { 368 try { 369 FileObject catalog = getCatalogFileObject(); 370 return catalog==null?null:catalog.getURL(); 371 } catch (FileStateInvalidException ex) { 372 return null; 373 } 374 375 } 376 377 private FileObject getWsdlFolderForService(String name) throws IOException { 378 FileObject globalWsdlFolder = getWsdlFolder(true); 379 FileObject oldWsdlFolder = globalWsdlFolder.getFileObject(name); 380 if (oldWsdlFolder!=null) { 381 FileLock lock = oldWsdlFolder.lock(); 382 try { 383 oldWsdlFolder.delete(lock); 384 } finally { 385 lock.releaseLock(); 386 } 387 } 388 return globalWsdlFolder.createFolder(name); 389 } 390 391 public abstract FileObject getWsdlFolder(boolean create) throws java.io.IOException ; 392 393 397 public abstract String getWsdlLocation(String serviceName); 398 } 399 | Popular Tags |