1 19 20 package org.netbeans.modules.websvc.spi.jaxws.client; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.net.URI ; 25 import java.net.URISyntaxException ; 26 import java.net.URL ; 27 import java.net.UnknownHostException ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import org.apache.tools.ant.module.api.support.ActionUtils; 31 import org.netbeans.api.project.Project; 32 import org.netbeans.modules.websvc.api.jaxws.project.GeneratedFilesHelper; 33 import org.netbeans.modules.websvc.api.jaxws.project.config.Client; 34 import org.netbeans.modules.websvc.api.jaxws.project.config.ClientAlreadyExistsExeption; 35 import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel; 36 import org.netbeans.modules.websvc.api.jaxws.project.WSUtils; 37 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModel; 38 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModelListener; 39 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModeler; 40 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModelerFactory; 41 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlService; 42 import org.openide.DialogDisplayer; 43 import org.openide.ErrorManager; 44 import org.openide.NotifyDescriptor; 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.openide.nodes.Node; 52 import org.openide.util.NbBundle; 53 54 58 public abstract class ProjectJAXWSClientSupport implements JAXWSClientSupportImpl { 59 Project project; 60 private FileObject clientArtifactsFolder; 61 62 63 public ProjectJAXWSClientSupport(Project project) { 64 this.project=project; 65 } 66 67 public void removeServiceClient(String serviceName) { 68 JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class); 69 if (jaxWsModel!=null && jaxWsModel.removeClient(serviceName)) { 70 writeJaxWsModel(jaxWsModel); 71 } 72 } 73 74 public String getWsdlUrl(String serviceName) { 75 JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class); 76 if (jaxWsModel!=null) { 77 Client client = jaxWsModel.findClientByName(serviceName); 78 if (client!=null) return client.getWsdlUrl(); 79 } 80 return null; 81 } 82 83 public String addServiceClient(String clientName, String wsdlUrl, String packageName, boolean isJsr109) { 84 JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class); 85 String finalClientName=clientName; 86 boolean clientAdded=false; 87 if (jaxWsModel!=null) { 88 Client client=null; 89 finalClientName = findProperClientName(clientName, jaxWsModel); 90 91 clientArtifactsFolder = project.getProjectDirectory().getFileObject("build/generated/wsimport/client"); if (clientArtifactsFolder!=null) { 96 clientArtifactsFolder.getChildren(true); 97 } 98 99 FileObject localWsdl=null; 100 try { 101 localWsdl = WSUtils.retrieveResource( 102 getLocalWsdlFolderForClient(finalClientName,true), 103 new URI (wsdlUrl)); 104 } catch (URISyntaxException ex) { 105 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 106 String mes = NbBundle.getMessage(ProjectJAXWSClientSupport.class, "ERR_IncorrectURI", wsdlUrl); NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); 108 DialogDisplayer.getDefault().notify(desc); 109 } catch (UnknownHostException ex) { 110 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 111 String mes = NbBundle.getMessage(ProjectJAXWSClientSupport.class, "ERR_UnknownHost", ex.getMessage()); NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); 113 DialogDisplayer.getDefault().notify(desc); 114 } catch (IOException ex) { 115 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 116 String mes = NbBundle.getMessage(ProjectJAXWSClientSupport.class, "ERR_WsdlRetrieverFailure", wsdlUrl); NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); 118 DialogDisplayer.getDefault().notify(desc); 119 } 120 121 if (localWsdl!=null) { 122 Boolean value = jaxWsModel.getJsr109(); 123 if((value == null || Boolean.TRUE.equals(value)) && !isJsr109){ 124 jaxWsModel.setJsr109(Boolean.FALSE); 125 } else if (Boolean.FALSE.equals(value) && isJsr109) { 126 jaxWsModel.setJsr109(Boolean.TRUE); 127 } 128 try { 129 client = jaxWsModel.addClient(finalClientName, wsdlUrl, packageName); 130 } catch (ClientAlreadyExistsExeption ex) { 131 } 133 FileObject xmlResorcesFo = getLocalWsdlFolderForClient(finalClientName,false); 134 String localWsdlUrl = FileUtil.getRelativePath(xmlResorcesFo, localWsdl); 135 client.setLocalWsdlFile(localWsdlUrl); 136 FileObject catalog = getCatalogFileObject(); 137 if (catalog!=null) client.setCatalogFile(CATALOG_FILE); 138 writeJaxWsModel(jaxWsModel); 139 clientAdded=true; 140 try { 142 final WsdlModeler modeler = WsdlModelerFactory.getDefault().getWsdlModeler(localWsdl.getURL()); 143 if (modeler!=null) { 144 modeler.setPackageName(packageName); 145 modeler.generateWsdlModel(new WsdlModelListener() { 146 public void modelCreated(WsdlModel model) { 147 if (model==null) { 148 DialogDisplayer.getDefault().notify(new WsImportFailedMessage(modeler.getCreationException())); 149 } 150 } 151 }); 152 } 153 } catch (IOException ex) { 154 ErrorManager.getDefault().log(ex.getLocalizedMessage()); 155 } 156 } 157 if (clientAdded) { 158 if(!isJsr109){ 159 try{ 160 addJaxWs20Library(); 161 } catch(Exception e){ ErrorManager.getDefault().notify(e); 163 } 164 } 165 FileObject buildImplFo = project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_IMPL_XML_PATH); 166 try { 167 ExecutorTask wsimportTask = 168 ActionUtils.runTarget(buildImplFo,new String []{"wsimport-client-"+finalClientName},null); if (wsimportTask.result()==0) { 170 if (clientArtifactsFolder==null) 171 clientArtifactsFolder = project.getProjectDirectory().getFileObject("build/generated/wsimport/client"); if (clientArtifactsFolder!=null) { 173 FileObject clientArtifactsFolder1 = clientArtifactsFolder.getFileObject(packageName.replace('.','/')); 174 if (clientArtifactsFolder1!=null) { 175 wsimportTask= 176 ActionUtils.runTarget(buildImplFo,new String []{"wsimport-client-compile"},null); } 178 } 179 } 180 } catch (IOException ex) { 181 ErrorManager.getDefault().log(ex.getLocalizedMessage()); 182 } catch (IllegalArgumentException ex) { 183 ErrorManager.getDefault().log(ex.getLocalizedMessage()); 184 } 185 return finalClientName; 186 } 187 } 188 return null; 189 } 190 191 private String findProperClientName(String name, JaxWsModel jaxWsModel) { 192 String firstName=name.length()==0?NbBundle.getMessage(ProjectJAXWSClientSupport.class,"LBL_defaultClientName"):name; 193 if (jaxWsModel.findClientByName(firstName)==null) return firstName; 194 for (int i = 1;; i++) { 195 String finalName = firstName + "_" + i; if (jaxWsModel.findClientByName(finalName)==null) 197 return finalName; 198 } 199 } 200 201 private void writeJaxWsModel(final JaxWsModel jaxWsModel) { 202 try { 203 final FileObject jaxWsFo = project.getProjectDirectory().getFileObject("nbproject/jax-ws.xml"); jaxWsFo.getFileSystem().runAtomicAction(new AtomicAction() { 205 public void run() { 206 FileLock lock=null; 207 OutputStream os=null; 208 try { 209 lock = jaxWsFo.lock(); 210 os = jaxWsFo.getOutputStream(lock); 211 jaxWsModel.write(os); 212 os.close(); 213 } catch (java.io.IOException ex) { 214 ErrorManager.getDefault().notify(ex); 215 } finally { 216 if (os!=null) { 217 try { 218 os.close(); 219 } catch (IOException ex) {} 220 } 221 if (lock!=null) lock.releaseLock(); 222 } 223 } 224 }); 225 } catch (IOException ex) { 226 ErrorManager.getDefault().notify(ex); 227 } 228 } 229 230 public List getServiceClients() { 231 List jaxWsClients = new ArrayList (); 232 JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class); 233 if (jaxWsModel!=null) { 234 Client[] clients = jaxWsModel.getClients(); 235 for (int i=0;i<clients.length;i++) jaxWsClients.add(clients[i]); 236 } 237 return jaxWsClients; 238 } 239 240 243 public FileObject getLocalWsdlFolderForClient(String clientName, boolean createFolder) { 244 return getArtifactsFolder(clientName, createFolder, true); 245 } 246 247 250 public FileObject getBindingsFolderForClient(String clientName, boolean createFolder) { 251 return getArtifactsFolder(clientName, createFolder, false); 252 } 253 254 private FileObject getArtifactsFolder(String clientName, boolean createFolder, boolean forWsdl) { 255 String folderName = forWsdl?"wsdl":"bindings"; FileObject root = getXmlArtifactsRoot(); 257 assert root!=null; 258 FileObject wsdlLocalFolder = root.getFileObject(XML_RESOURCES_FOLDER+"/"+CLIENTS_LOCAL_FOLDER+"/"+clientName+"/"+folderName); if (wsdlLocalFolder==null && createFolder) { 260 try { 261 FileObject xmlLocalFolder = root.getFileObject(XML_RESOURCES_FOLDER); 262 if (xmlLocalFolder==null) xmlLocalFolder = root.createFolder(XML_RESOURCES_FOLDER); 263 FileObject servicesLocalFolder = xmlLocalFolder.getFileObject(CLIENTS_LOCAL_FOLDER); 264 if (servicesLocalFolder==null) servicesLocalFolder = xmlLocalFolder.createFolder(CLIENTS_LOCAL_FOLDER); 265 FileObject serviceLocalFolder = servicesLocalFolder.getFileObject(clientName); 266 if (serviceLocalFolder==null) serviceLocalFolder = servicesLocalFolder.createFolder(clientName); 267 wsdlLocalFolder=serviceLocalFolder.getFileObject(folderName); 268 if (wsdlLocalFolder==null) wsdlLocalFolder = serviceLocalFolder.createFolder(folderName); 269 } catch (IOException ex) { 270 return null; 271 } 272 } 273 return wsdlLocalFolder; 274 } 275 276 278 protected FileObject getXmlArtifactsRoot() { 279 return project.getProjectDirectory(); 280 } 281 282 private FileObject getCatalogFileObject() { 283 return project.getProjectDirectory().getFileObject(CATALOG_FILE); 284 } 285 286 public URL getCatalog() { 287 try { 288 FileObject catalog = getCatalogFileObject(); 289 return catalog==null?null:catalog.getURL(); 290 } catch (FileStateInvalidException ex) { 291 return null; 292 } 293 294 } 295 296 protected abstract void addJaxWs20Library() throws Exception ; 297 298 public abstract FileObject getWsdlFolder(boolean create) throws IOException ; 299 300 public String getServiceRefName(Node clientNode) { 301 WsdlService service = (WsdlService)clientNode.getLookup().lookup(WsdlService.class); 302 String serviceName = service.getName(); 303 return "service/" + serviceName; 304 } 305 306 private class WsImportFailedMessage extends NotifyDescriptor.Message { 307 public WsImportFailedMessage(Throwable ex) { 308 super(NbBundle.getMessage(ProjectJAXWSClientSupport.class,"TXT_CannotGenerateClient",ex.getLocalizedMessage()), 309 NotifyDescriptor.ERROR_MESSAGE); 310 } 311 312 } 313 314 316 protected FileObject getWsdlFolderForClient(String name) throws IOException { 317 FileObject globalWsdlFolder = getWsdlFolder(true); 318 FileObject oldWsdlFolder = globalWsdlFolder.getFileObject("client/"+name); if (oldWsdlFolder!=null) { 320 FileLock lock = oldWsdlFolder.lock(); 321 try { 322 oldWsdlFolder.delete(lock); 323 } finally { 324 lock.releaseLock(); 325 } 326 } 327 FileObject clientWsdlFolder = globalWsdlFolder.getFileObject("client"); if (clientWsdlFolder==null) clientWsdlFolder = globalWsdlFolder.createFolder("client"); return clientWsdlFolder.createFolder(name); 330 } 331 332 } 333 | Popular Tags |