1 19 package org.netbeans.modules.websvc.core.jaxws.nodes; 20 21 25 import java.io.IOException ; 26 import java.net.URI ; 27 import java.net.URISyntaxException ; 28 import java.net.UnknownHostException ; 29 import java.util.ArrayList ; 30 import java.util.Collections ; 31 import java.util.List ; 32 import org.apache.tools.ant.module.api.support.ActionUtils; 33 import org.netbeans.api.project.FileOwnerQuery; 34 import org.netbeans.api.project.Project; 35 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 36 import org.netbeans.modules.websvc.api.jaxws.client.JAXWSClientSupport; 37 import org.netbeans.modules.websvc.api.jaxws.project.GeneratedFilesHelper; 38 import org.netbeans.modules.websvc.api.jaxws.project.config.Client; 39 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModel; 40 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModelListener; 41 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModeler; 42 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlService; 43 import org.netbeans.modules.websvc.api.jaxws.project.WSUtils; 44 import org.netbeans.modules.websvc.core.JaxWsUtils; 45 import org.openide.DialogDisplayer; 46 import org.openide.ErrorManager; 47 import org.openide.execution.ExecutorTask; 48 import org.openide.filesystems.FileLock; 49 import org.openide.filesystems.FileObject; 50 import org.openide.nodes.Children; 51 import org.openide.nodes.Node; 52 import org.openide.util.NbBundle; 53 54 public class JaxWsClientChildren extends Children.Keys { 55 Client client; 56 WsdlModel wsdlModel; 57 58 public JaxWsClientChildren(Client client) { 59 this.client=client; 60 } 61 62 protected void addNotify() { 63 super.addNotify(); 64 FileObject wsdlFo = getLocalWsdl(); 65 final WsdlModeler wsdlModeler = ((JaxWsClientNode)getNode()).getWsdlModeler(); 66 if (wsdlModeler!=null) { 67 wsdlModel = wsdlModeler.getWsdlModel(); 68 if (wsdlModel==null) { 69 wsdlModeler.generateWsdlModel(new WsdlModelListener() { 70 public void modelCreated(WsdlModel model) { 71 wsdlModel=model; 72 ((JaxWsClientNode)getNode()).changeIcon(); 73 if (model==null) { 74 DialogDisplayer.getDefault().notify( 75 new JaxWsUtils.WsImportClientFailedMessage(wsdlModeler.getCreationException())); 76 } 77 updateKeys(); 78 } 79 }); 80 } else { 81 updateKeys(); 82 } 83 } 84 } 85 86 protected void removeNotify() { 87 setKeys(Collections.EMPTY_SET); 88 super.removeNotify(); 89 } 90 91 private void updateKeys() { 92 List keys=null; 93 if (wsdlModel!=null) { 94 keys=wsdlModel.getServices(); 95 } 96 setKeys(keys==null?new ArrayList ():keys); 97 } 98 99 protected Node[] createNodes(Object key) { 100 if(key instanceof WsdlService) { 101 return new Node[] {new ServiceNode((WsdlService)key)}; 102 } 103 return new Node[0]; 104 } 105 106 void refreshKeys(boolean downloadWsdl) { 107 super.addNotify(); 108 List keys=null; 109 JAXWSClientSupport support = getJAXWSClientSupport(); 111 final JaxWsClientNode clientNode = (JaxWsClientNode)getNode(); 112 if (downloadWsdl) { 113 try { 114 String clientName = clientNode.getName(); 115 WSUtils.retrieveResource( 116 support.getLocalWsdlFolderForClient(clientName,true), 117 new URI (client.getWsdlUrl())); 118 119 if (client.getWsdlUrl().startsWith("file:")) { 121 FileObject srcRoot = (FileObject)getNode().getLookup().lookup(FileObject.class); 122 Project project = FileOwnerQuery.getOwner(srcRoot); 123 if (project.getLookup().lookup(J2eeModuleProvider.class)!=null) { 124 FileObject xmlResorcesFo = support.getLocalWsdlFolderForClient(clientName,false); 125 if (xmlResorcesFo!=null) { 126 FileObject wsdlFolder = getWsdlFolderForClient(support, clientName); 127 WSUtils.copyFiles(xmlResorcesFo, wsdlFolder); 128 } 129 } 130 } 131 } catch (URISyntaxException ex) { 132 ErrorManager.getDefault().notify(ex); 133 } catch (UnknownHostException ex) { 134 ErrorManager.getDefault().annotate(ex, 135 NbBundle.getMessage(JaxWsClientChildren.class,"MSG_ConnectionProblem")); 136 return; 137 } catch (IOException ex) { 138 ErrorManager.getDefault().annotate(ex, 139 NbBundle.getMessage(JaxWsClientChildren.class,"MSG_ConnectionProblem")); 140 return; 141 } 142 143 } 144 145 FileObject wsdlFo = getLocalWsdl(); 146 final WsdlModeler wsdlModeler = clientNode.getWsdlModeler(); 147 clientNode.setModelGenerationFinished(false); 148 clientNode.changeIcon(); 149 if (wsdlModeler!=null) { 150 wsdlModeler.generateWsdlModel(new WsdlModelListener() { 151 public void modelCreated(WsdlModel model) { 152 wsdlModel=model; 153 clientNode.setModelGenerationFinished(true); 154 clientNode.changeIcon(); 155 if (model==null) { 156 DialogDisplayer.getDefault().notify( 157 new JaxWsUtils.WsImportClientFailedMessage(wsdlModeler.getCreationException())); 158 } 159 updateKeys(); 160 161 if (model!=null) { 162 Client client = clientNode.getJaxWsModel().findClientByName(clientNode.getName()); 163 if (client!=null) { 164 WsdlService wsdlService = null; 165 boolean jaxWsModelChanged=false; 166 List <WsdlService> wsdlServices = model.getServices(); 167 if (wsdlServices!=null && wsdlServices.size()>0) { 168 wsdlService = wsdlServices.get(0); 169 } 170 171 String oldPkgName = client.getPackageName(); 173 if (wsdlService!=null && oldPkgName!=null && !client.isPackageNameForceReplace()) { 174 String javaName = wsdlService.getJavaName(); 175 int dotPosition = javaName.lastIndexOf("."); 176 if (dotPosition>=0) { 177 String newPkgName = javaName.substring(0,dotPosition); 178 if (!oldPkgName.equals(newPkgName)) { 179 client.setPackageName(newPkgName); 180 jaxWsModelChanged=true; 181 } 182 } 183 } 184 185 if (jaxWsModelChanged) { 187 try { 188 clientNode.getJaxWsModel().write(); 189 } catch (IOException ex) { 190 ErrorManager.getDefault().notify(ErrorManager.ERROR,ex); 191 } 192 } 193 } 194 } 195 } 196 }); 197 } 198 FileObject srcRoot = (FileObject)getNode().getLookup().lookup(FileObject.class); 200 Project project = FileOwnerQuery.getOwner(srcRoot); 201 if (project!=null) { 202 FileObject buildImplFo = project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_IMPL_XML_PATH); 203 try { 204 String name = client.getName(); 205 ExecutorTask wsimportTask = 206 ActionUtils.runTarget(buildImplFo, 207 new String []{"wsimport-client-clean-"+name,"wsimport-client-"+name},null); wsimportTask.waitFinished(); 209 } catch (IOException ex) { 210 ErrorManager.getDefault().log(ex.getLocalizedMessage()); 211 } catch (IllegalArgumentException ex) { 212 ErrorManager.getDefault().log(ex.getLocalizedMessage()); 213 } 214 String packageName = client.getPackageName().replace(".","/"); FileObject clientArtifactsFolder = project.getProjectDirectory().getFileObject("build/generated/wsimport/client/"+packageName); if (clientArtifactsFolder!=null) clientArtifactsFolder.refresh(); 218 } 219 } 220 221 private JAXWSClientSupport getJAXWSClientSupport() { 222 return ((JaxWsClientNode)getNode()).getJAXWSClientSupport(); 223 } 224 225 private FileObject getLocalWsdl() { 226 return ((JaxWsClientNode)getNode()).getLocalWsdl(); 227 } 228 229 WsdlModel getWsdlModel() { 230 return wsdlModel; 231 } 232 233 private FileObject getWsdlFolderForClient(JAXWSClientSupport support, String name) throws IOException { 234 FileObject globalWsdlFolder = support.getWsdlFolder(true); 235 FileObject oldWsdlFolder = globalWsdlFolder.getFileObject("client/"+name); 236 if (oldWsdlFolder!=null) { 237 FileLock lock = oldWsdlFolder.lock(); 238 try { 239 oldWsdlFolder.delete(lock); 240 } finally { 241 lock.releaseLock(); 242 } 243 } 244 FileObject clientWsdlFolder = globalWsdlFolder.getFileObject("client"); if (clientWsdlFolder==null) clientWsdlFolder = globalWsdlFolder.createFolder("client"); return clientWsdlFolder.createFolder(name); 247 } 248 249 } 250 | Popular Tags |