KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > spi > jaxws > client > ProjectJAXWSClientSupport


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
20 package org.netbeans.modules.websvc.spi.jaxws.client;
21
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.net.URISyntaxException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.UnknownHostException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
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 /**
55  *
56  * @author mkuchtiak
57  */

58 public abstract class ProjectJAXWSClientSupport implements JAXWSClientSupportImpl {
59     Project project;
60     private FileObject clientArtifactsFolder;
61     
62     /** Creates a new instance of WebProjectJAXWSClientSupport */
63     public ProjectJAXWSClientSupport(Project project) {
64         this.project=project;
65     }
66     
67     public void removeServiceClient(String JavaDoc 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 JavaDoc getWsdlUrl(String JavaDoc 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 JavaDoc addServiceClient(String JavaDoc clientName, String JavaDoc wsdlUrl, String JavaDoc packageName, boolean isJsr109) {
84         JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class);
85         String JavaDoc finalClientName=clientName;
86         boolean clientAdded=false;
87         if (jaxWsModel!=null) {
88             Client client=null;
89             finalClientName = findProperClientName(clientName, jaxWsModel);
90             
91             // HACK to enable filesystems to fire events when new folder will be created
92
// need to ask for children recursively
93
//List subfolders = null;
94
clientArtifactsFolder = project.getProjectDirectory().getFileObject("build/generated/wsimport/client"); //NOI18N
95
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 JavaDoc(wsdlUrl));
104             } catch (URISyntaxException JavaDoc ex) {
105                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
106                 String JavaDoc mes = NbBundle.getMessage(ProjectJAXWSClientSupport.class, "ERR_IncorrectURI", wsdlUrl); // NOI18N
107
NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE);
108                 DialogDisplayer.getDefault().notify(desc);
109             } catch (UnknownHostException JavaDoc ex) {
110                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
111                 String JavaDoc mes = NbBundle.getMessage(ProjectJAXWSClientSupport.class, "ERR_UnknownHost", ex.getMessage()); // NOI18N
112
NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE);
113                 DialogDisplayer.getDefault().notify(desc);
114             } catch (IOException JavaDoc ex) {
115                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
116                 String JavaDoc mes = NbBundle.getMessage(ProjectJAXWSClientSupport.class, "ERR_WsdlRetrieverFailure", wsdlUrl); // NOI18N
117
NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE);
118                 DialogDisplayer.getDefault().notify(desc);
119             }
120             
121             if (localWsdl!=null) {
122                 Boolean JavaDoc 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                     //this shouldn't happen
132
}
133                 FileObject xmlResorcesFo = getLocalWsdlFolderForClient(finalClientName,false);
134                 String JavaDoc 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                 // generate wsdl model immediately
141
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 JavaDoc ex) {
154                     ErrorManager.getDefault().log(ex.getLocalizedMessage());
155                 }
156             }
157             if (clientAdded) {
158                 if(!isJsr109){
159                     try{
160                         addJaxWs20Library();
161                     } catch(Exception JavaDoc e){ //TODO handle this
162
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 JavaDoc[]{"wsimport-client-"+finalClientName},null); //NOI18N
169
if (wsimportTask.result()==0) {
170                         if (clientArtifactsFolder==null)
171                             clientArtifactsFolder = project.getProjectDirectory().getFileObject("build/generated/wsimport/client"); //NOI18N
172
if (clientArtifactsFolder!=null) {
173                             FileObject clientArtifactsFolder1 = clientArtifactsFolder.getFileObject(packageName.replace('.','/'));
174                             if (clientArtifactsFolder1!=null) {
175                                 wsimportTask=
176                                     ActionUtils.runTarget(buildImplFo,new String JavaDoc[]{"wsimport-client-compile"},null); //NOI18N
177
}
178                         }
179                     }
180                 } catch (IOException JavaDoc ex) {
181                     ErrorManager.getDefault().log(ex.getLocalizedMessage());
182                 } catch (IllegalArgumentException JavaDoc ex) {
183                     ErrorManager.getDefault().log(ex.getLocalizedMessage());
184                 }
185                 return finalClientName;
186             }
187         }
188         return null;
189     }
190     
191     private String JavaDoc findProperClientName(String JavaDoc name, JaxWsModel jaxWsModel) {
192         String JavaDoc 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 JavaDoc finalName = firstName + "_" + i; // NOI18N
196
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"); //NOI18N
204
jaxWsFo.getFileSystem().runAtomicAction(new AtomicAction() {
205                 public void run() {
206                     FileLock lock=null;
207                     OutputStream JavaDoc 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 JavaDoc ex) {
214                         ErrorManager.getDefault().notify(ex);
215                     } finally {
216                         if (os!=null) {
217                             try {
218                                 os.close();
219                             } catch (IOException JavaDoc ex) {}
220                         }
221                         if (lock!=null) lock.releaseLock();
222                     }
223                 }
224             });
225         } catch (IOException JavaDoc ex) {
226             ErrorManager.getDefault().notify(ex);
227         }
228     }
229     
230     public List JavaDoc getServiceClients() {
231         List JavaDoc jaxWsClients = new ArrayList JavaDoc();
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     /**
241      * return root folder for wsdl artifacts
242      */

243     public FileObject getLocalWsdlFolderForClient(String JavaDoc clientName, boolean createFolder) {
244         return getArtifactsFolder(clientName, createFolder, true);
245     }
246     
247     /**
248      * return folder for local wsdl bindings
249      */

250     public FileObject getBindingsFolderForClient(String JavaDoc clientName, boolean createFolder) {
251         return getArtifactsFolder(clientName, createFolder, false);
252     }
253     
254     private FileObject getArtifactsFolder(String JavaDoc clientName, boolean createFolder, boolean forWsdl) {
255         String JavaDoc folderName = forWsdl?"wsdl":"bindings"; //NOI18N
256
FileObject root = getXmlArtifactsRoot();
257         assert root!=null;
258         FileObject wsdlLocalFolder = root.getFileObject(XML_RESOURCES_FOLDER+"/"+CLIENTS_LOCAL_FOLDER+"/"+clientName+"/"+folderName); //NOI18N
259
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 JavaDoc ex) {
270                 return null;
271             }
272         }
273         return wsdlLocalFolder;
274     }
275     
276     /** return root folder for xml artifacts
277      */

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 JavaDoc 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 JavaDoc;
297     
298     public abstract FileObject getWsdlFolder(boolean create) throws IOException JavaDoc;
299
300     public String JavaDoc getServiceRefName(Node clientNode) {
301         WsdlService service = (WsdlService)clientNode.getLookup().lookup(WsdlService.class);
302         String JavaDoc serviceName = service.getName();
303         return "service/" + serviceName;
304     }
305
306     private class WsImportFailedMessage extends NotifyDescriptor.Message {
307         public WsImportFailedMessage(Throwable JavaDoc ex) {
308             super(NbBundle.getMessage(ProjectJAXWSClientSupport.class,"TXT_CannotGenerateClient",ex.getLocalizedMessage()),
309                     NotifyDescriptor.ERROR_MESSAGE);
310         }
311         
312     }
313     
314     /** folder where xml client artifacts should be saved, e.g. WEB-INF/wsdl/client/SampleClient
315      */

316     protected FileObject getWsdlFolderForClient(String JavaDoc name) throws IOException JavaDoc {
317         FileObject globalWsdlFolder = getWsdlFolder(true);
318         FileObject oldWsdlFolder = globalWsdlFolder.getFileObject("client/"+name); //NOI18N
319
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"); //NOI18N
328
if (clientWsdlFolder==null) clientWsdlFolder = globalWsdlFolder.createFolder("client"); //NOI18N
329
return clientWsdlFolder.createFolder(name);
330     }
331
332 }
333
Popular Tags