KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > core > jaxws > nodes > JaxWsClientChildren


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.websvc.core.jaxws.nodes;
20
21 /** WSDL children (Service elements)
22  *
23  * @author mkuchtiak
24  */

25 import java.io.IOException JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.net.URISyntaxException JavaDoc;
28 import java.net.UnknownHostException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.List JavaDoc;
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 JavaDoc keys=null;
93         if (wsdlModel!=null) {
94             keys=wsdlModel.getServices();
95         }
96         setKeys(keys==null?new ArrayList JavaDoc():keys);
97     }
98
99     protected Node[] createNodes(Object JavaDoc 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 JavaDoc keys=null;
109         // copy to local wsdl first
110
JAXWSClientSupport support = getJAXWSClientSupport();
111         final JaxWsClientNode clientNode = (JaxWsClientNode)getNode();
112         if (downloadWsdl) {
113             try {
114                 String JavaDoc clientName = clientNode.getName();
115                 WSUtils.retrieveResource(
116                     support.getLocalWsdlFolderForClient(clientName,true),
117                     new URI JavaDoc(client.getWsdlUrl()));
118                 
119                 // copy resources to WEB-INF[META-INF]/wsdl/client/${clientName}
120
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 JavaDoc ex) {
132                 ErrorManager.getDefault().notify(ex);
133             } catch (UnknownHostException JavaDoc ex) {
134                 ErrorManager.getDefault().annotate(ex,
135                         NbBundle.getMessage(JaxWsClientChildren.class,"MSG_ConnectionProblem"));
136                 return;
137             } catch (IOException JavaDoc 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 JavaDoc<WsdlService> wsdlServices = model.getServices();
167                             if (wsdlServices!=null && wsdlServices.size()>0) {
168                                 wsdlService = wsdlServices.get(0);
169                             }
170                             
171                             // test if package name for java artifacts hasn't changed
172
String JavaDoc oldPkgName = client.getPackageName();
173                             if (wsdlService!=null && oldPkgName!=null && !client.isPackageNameForceReplace()) {
174                                 String JavaDoc javaName = wsdlService.getJavaName();
175                                 int dotPosition = javaName.lastIndexOf(".");
176                                 if (dotPosition>=0) {
177                                     String JavaDoc newPkgName = javaName.substring(0,dotPosition);
178                                     if (!oldPkgName.equals(newPkgName)) {
179                                         client.setPackageName(newPkgName);
180                                         jaxWsModelChanged=true;
181                                     }
182                                 }
183                             }
184
185                             // save jax-ws model
186
if (jaxWsModelChanged) {
187                                 try {
188                                     clientNode.getJaxWsModel().write();
189                                 } catch (IOException JavaDoc ex) {
190                                     ErrorManager.getDefault().notify(ErrorManager.ERROR,ex);
191                                 }
192                             }
193                         }
194                     }
195                 }
196             });
197         }
198         // re-generate java artifacts
199
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 JavaDoc name = client.getName();
205                 ExecutorTask wsimportTask =
206                     ActionUtils.runTarget(buildImplFo,
207                         new String JavaDoc[]{"wsimport-client-clean-"+name,"wsimport-client-"+name},null); //NOI18N
208
wsimportTask.waitFinished();
209             } catch (IOException JavaDoc ex) {
210                 ErrorManager.getDefault().log(ex.getLocalizedMessage());
211             } catch (IllegalArgumentException JavaDoc ex) {
212                 ErrorManager.getDefault().log(ex.getLocalizedMessage());
213             }
214             // refresh client artifacts directory due to code copletion
215
String JavaDoc packageName = client.getPackageName().replace(".","/"); //NOI18N
216
FileObject clientArtifactsFolder = project.getProjectDirectory().getFileObject("build/generated/wsimport/client/"+packageName); //NOI18N
217
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 JavaDoc name) throws IOException JavaDoc {
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"); //NOI18N
245
if (clientWsdlFolder==null) clientWsdlFolder = globalWsdlFolder.createFolder("client"); //NOI18N
246
return clientWsdlFolder.createFolder(name);
247     }
248
249 }
250
Popular Tags