KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.awt.Dialog JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.swing.Action JavaDoc;
28 import org.apache.tools.ant.module.api.support.ActionUtils;
29 import org.netbeans.api.project.FileOwnerQuery;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
32 import org.netbeans.modules.websvc.api.jaxws.client.JAXWSClientSupport;
33 import org.netbeans.modules.websvc.api.jaxws.project.GeneratedFilesHelper;
34 import org.netbeans.modules.websvc.api.jaxws.project.config.Binding;
35 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModel;
36 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModelListener;
37 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModeler;
38 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModelerFactory;
39 import org.netbeans.modules.websvc.core.jaxws.actions.JaxWsRefreshClientAction;
40 import org.netbeans.modules.websvc.core.jaxws.bindings.model.BindingsHandler;
41 import org.netbeans.modules.websvc.core.jaxws.bindings.model.BindingsHandlerChain;
42 import org.netbeans.modules.websvc.core.jaxws.bindings.model.BindingsHandlerChains;
43 import org.netbeans.modules.websvc.core.jaxws.bindings.model.BindingsHandlerClass;
44 import org.netbeans.modules.websvc.core.jaxws.bindings.model.BindingsModel;
45 import org.netbeans.modules.websvc.core.jaxws.bindings.model.BindingsModelFactory;
46 import org.netbeans.modules.websvc.core.jaxws.bindings.model.DefinitionsBindings;
47 import org.netbeans.modules.websvc.core.jaxws.bindings.model.GlobalBindings;
48 import org.netbeans.modules.websvc.core.webservices.action.ConfigureHandlerAction;
49 import org.netbeans.modules.websvc.core.webservices.action.ConfigureHandlerCookie;
50 import org.netbeans.modules.websvc.core.webservices.ui.panels.MessageHandlerPanel;
51 import org.netbeans.modules.websvc.core.wseditor.support.EditWSAttributesCookie;
52 import org.netbeans.modules.websvc.core.wseditor.support.WSEditAttributesAction;
53 import org.netbeans.modules.websvc.jaxws.api.JaxWsRefreshCookie;
54 import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel;
55 import org.netbeans.modules.websvc.api.jaxws.project.config.Client;
56 import org.netbeans.modules.websvc.core.wseditor.support.EditWSAttributesCookieImpl;
57 import org.netbeans.modules.xml.retriever.catalog.Utilities;
58 import org.netbeans.modules.xml.xam.ModelSource;
59 import org.openide.DialogDescriptor;
60 import org.openide.DialogDisplayer;
61 import org.openide.ErrorManager;
62 import org.openide.NotifyDescriptor;
63 import org.openide.actions.DeleteAction;
64 import org.openide.actions.OpenAction;
65 import org.openide.actions.OpenLocalExplorerAction;
66 import org.openide.actions.PropertiesAction;
67 import org.openide.cookies.EditCookie;
68 import org.openide.cookies.OpenCookie;
69 import org.openide.execution.ExecutorTask;
70 import org.openide.filesystems.FileLock;
71 import org.openide.filesystems.FileObject;
72 import org.openide.filesystems.FileStateInvalidException;
73 import org.openide.loaders.DataObject;
74 import org.openide.nodes.AbstractNode;
75 import org.openide.nodes.Node;
76 import org.openide.util.HelpCtx;
77 import org.openide.util.NbBundle;
78 import org.openide.util.actions.SystemAction;
79 import org.openide.util.lookup.AbstractLookup;
80 import org.openide.util.lookup.InstanceContent;
81
82 public class JaxWsClientNode extends AbstractNode implements OpenCookie, JaxWsRefreshCookie,
83         ConfigureHandlerCookie{
84     Client client;
85     FileObject srcRoot;
86     JaxWsModel jaxWsModel;
87     InstanceContent content;
88     private FileObject wsdlFileObject;
89     boolean modelGenerationFinished;
90     
91     public JaxWsClientNode(JaxWsModel jaxWsModel, Client client, FileObject srcRoot) {
92         this(jaxWsModel, client, srcRoot, new InstanceContent());
93     }
94     
95     private JaxWsClientNode(JaxWsModel jaxWsModel, Client client, FileObject srcRoot, InstanceContent content) {
96         super(new JaxWsClientChildren(client),new AbstractLookup(content));
97         this.jaxWsModel=jaxWsModel;
98         this.client=client;
99         this.srcRoot=srcRoot;
100         this.content = content;
101         setName(client.getName());
102         setDisplayName(client.getName());
103         content.add(this);
104         content.add(client);
105         content.add(srcRoot);
106         WsdlModeler modeler = getWsdlModeler();
107         if (modeler!=null) {
108             changeIcon();
109             modeler.generateWsdlModel(new WsdlModelListener(){
110                 public void modelCreated(WsdlModel model) {
111                     modelGenerationFinished=true;
112                     changeIcon();
113                 }
114             });
115         }
116         content.add(new EditWSAttributesCookieImpl(this, jaxWsModel));
117     }
118     
119     public String JavaDoc getShortDescription() {
120         return client.getWsdlUrl();
121     }
122     
123     private static final java.awt.Image JavaDoc WAITING_BADGE =
124             org.openide.util.Utilities.loadImage( "org/netbeans/modules/websvc/core/webservices/ui/resources/waiting.png"); // NOI18N
125
private static final java.awt.Image JavaDoc ERROR_BADGE =
126             org.openide.util.Utilities.loadImage( "org/netbeans/modules/websvc/core/webservices/ui/resources/error-badge.gif" ); //NOI18N
127
private static final java.awt.Image JavaDoc SERVICE_BADGE =
128             org.openide.util.Utilities.loadImage( "org/netbeans/modules/websvc/core/webservices/ui/resources/XMLServiceDataIcon.gif" ); //NOI18N
129

130     public java.awt.Image JavaDoc getIcon(int type) {
131         if (((JaxWsClientChildren)getChildren()).getWsdlModel()!=null) {
132             return SERVICE_BADGE;
133         } else {
134             WsdlModeler wsdlModeler = getWsdlModeler();
135             if (wsdlModeler!=null && wsdlModeler.getCreationException()==null) {
136                 if (modelGenerationFinished)
137                     return SERVICE_BADGE;
138                 else
139                     return org.openide.util.Utilities.mergeImages(SERVICE_BADGE, WAITING_BADGE, 15, 8);
140             } else {
141                 java.awt.Image JavaDoc dirtyNodeImage = org.openide.util.Utilities.mergeImages(SERVICE_BADGE, ERROR_BADGE, 6, 6);
142                 if (modelGenerationFinished)
143                     return dirtyNodeImage;
144                 else
145                     return org.openide.util.Utilities.mergeImages(dirtyNodeImage, WAITING_BADGE, 15, 8);
146             }
147         }
148     }
149     
150     public java.awt.Image JavaDoc getOpenedIcon(int type){
151         return getIcon( type);
152     }
153     
154     public void open() {
155         EditCookie ec = getEditCookie();
156         if (ec != null) {
157             ec.edit();
158         }
159     }
160     
161     void changeIcon() {
162         fireIconChange();
163     }
164     /*
165     public Node.Cookie getCookie(Class type){
166         System.out.println("getCookie "+type+":"+type.getName());
167         if(type == EditWSAttributesCookie.class){
168             return new EditWSAttributesCookieImpl(this, jaxWsModel); //NOI18N
169         }
170         return super.getCookie(type);
171     }
172     */

173     private EditCookie getEditCookie() {
174         try {
175             FileObject wsdlFo =
176                     JAXWSClientSupport.getJaxWsClientSupport(srcRoot).getLocalWsdlFolderForClient(client.getName(),false).getFileObject(client.getLocalWsdlFile());
177             assert wsdlFo!=null: "Cannot find local WSDL file"; //NOI18N
178
if (wsdlFo!=null) {
179                 DataObject dObj = DataObject.find(wsdlFo);
180                 return (EditCookie)dObj.getCookie(EditCookie.class);
181             }
182         } catch (java.io.IOException JavaDoc ex) {
183             ErrorManager.getDefault().log(ex.getLocalizedMessage());
184             return null;
185         }
186         return null;
187     }
188     
189     public Action JavaDoc getPreferredAction() {
190         return SystemAction.get(OpenAction.class);
191     }
192     
193     // Create the popup menu:
194
public Action JavaDoc[] getActions(boolean context) {
195         return new SystemAction[] {
196             SystemAction.get(OpenAction.class),
197             SystemAction.get(JaxWsRefreshClientAction.class),
198             null,
199             SystemAction.get(WSEditAttributesAction.class),
200             null,
201             SystemAction.get(ConfigureHandlerAction.class),
202             null,
203             SystemAction.get(DeleteAction.class),
204             null,
205             SystemAction.get(OpenLocalExplorerAction.class),
206             null,
207             SystemAction.get(PropertiesAction.class),
208         };
209     }
210     
211     public HelpCtx getHelpCtx() {
212         return HelpCtx.DEFAULT_HELP;
213     }
214     
215     // Handle deleting:
216
public boolean canDestroy() {
217         return true;
218     }
219     
220     public void destroy() throws java.io.IOException JavaDoc {
221         String JavaDoc clientName = client.getName();
222         NotifyDescriptor.Confirmation notifyDesc =
223                 new NotifyDescriptor.Confirmation(NbBundle.getMessage(JaxWsClientNode.class, "MSG_CONFIRM_DELETE", clientName));
224         DialogDisplayer.getDefault().notify(notifyDesc);
225         if(notifyDesc.getValue() == NotifyDescriptor.YES_OPTION){
226             
227             JAXWSClientSupport support = JAXWSClientSupport.getJaxWsClientSupport(srcRoot);
228             // removing local wsdl and xml artifacts
229
FileObject localWsdlFolder = support.getLocalWsdlFolderForClient(clientName,false);
230             if (localWsdlFolder!=null) {
231                 FileObject clientArtifactsFolder = localWsdlFolder.getParent();
232                 FileLock lock=null;
233                 try {
234                     lock = clientArtifactsFolder.lock();
235                     clientArtifactsFolder.delete(lock);
236                 } finally {
237                     if (lock!=null) lock.releaseLock();
238                 }
239             }
240             
241             Project project = FileOwnerQuery.getOwner(srcRoot);
242             // remove also client xml artifacs from WEB-INF[META-INF]/wsdl
243
if (client.getWsdlUrl().startsWith("file:")) { //NOI18N
244
if (project.getLookup().lookup(J2eeModuleProvider.class)!=null) {
245                     FileObject webInfClientFolder = findWsdlFolderForClient(support, clientName);
246                     if (webInfClientFolder!=null) {
247                         FileObject webInfClientRootFolder = webInfClientFolder.getParent();
248                         FileLock lock=null;
249                         try {
250                             lock = webInfClientFolder.lock();
251                             webInfClientFolder.delete(lock);
252                         } finally {
253                             if (lock!=null) lock.releaseLock();
254                         }
255                         if (webInfClientRootFolder.getChildren().length==0) {
256                             try {
257                                 lock = webInfClientRootFolder.lock();
258                                 webInfClientRootFolder.delete(lock);
259                             } finally {
260                                 if (lock!=null) lock.releaseLock();
261                             }
262                         }
263                     }
264                 }
265             }
266             // cleaning java artifacts
267
FileObject buildImplFo = project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_IMPL_XML_PATH);
268             try {
269                 ExecutorTask wsimportTask =
270                         ActionUtils.runTarget(buildImplFo,
271                         new String JavaDoc[]{"wsimport-client-clean-"+clientName},null); //NOI18N
272
wsimportTask.waitFinished();
273             } catch (java.io.IOException JavaDoc ex) {
274                 ErrorManager.getDefault().log(ex.getLocalizedMessage());
275             } catch (IllegalArgumentException JavaDoc ex) {
276                 ErrorManager.getDefault().log(ex.getLocalizedMessage());
277             }
278             // removing entry from jax-ws.xml
279
support.removeServiceClient(clientName);
280             super.destroy();
281         }
282     }
283     
284     /**
285      * refresh service information obtained from wsdl (when wsdl file was changed)
286      */

287     public void refreshService(boolean downloadWsdl) {
288         if (downloadWsdl) {
289             int result = RefreshClientDialog.open(client.getWsdlUrl());
290             if (RefreshClientDialog.CLOSE==result) return;
291             if (RefreshClientDialog.DOWNLOAD_WSDL==result){
292                 removeWsdlFolderContents();
293                 ((JaxWsClientChildren)getChildren()).refreshKeys(true);
294             } else
295                 ((JaxWsClientChildren)getChildren()).refreshKeys(false);
296         } else {
297             Object JavaDoc result = DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
298                     NbBundle.getMessage(JaxWsClientNode.class,
299                     "HINT_RefreshClient"))); //NOI18N
300
((JaxWsClientChildren)getChildren()).refreshKeys(false);
301         }
302     }
303     
304     private void removeWsdlFolderContents(){
305         FileObject wsdlFolder = getJAXWSClientSupport().getLocalWsdlFolderForClient(getName(), false);
306         if(wsdlFolder != null){
307             FileLock lock = null;
308             
309             FileObject[] files = wsdlFolder.getChildren();
310             for(int i = 0; i < files.length; i++){
311                 try{
312                     FileObject file = files[i];
313                     lock = file.lock();
314                     file.delete(lock);
315                 }catch(IOException JavaDoc e){
316                     ErrorManager.getDefault().notify(e);
317                 }
318                 finally{
319                     if(lock != null){
320                         lock.releaseLock();
321                         lock = null;
322                     }
323                 }
324             }
325         }
326     }
327     
328     
329     public void configureHandler() {
330         Project project = FileOwnerQuery.getOwner(srcRoot);
331         ArrayList JavaDoc<String JavaDoc> handlerClasses = new ArrayList JavaDoc<String JavaDoc>();
332         BindingsModel bindingsModel = getBindingsModel();
333         if(bindingsModel != null){ //if there is an existing bindings file, load it
334
GlobalBindings gb = bindingsModel.getGlobalBindings();
335             if(gb != null){
336                 DefinitionsBindings db = gb.getDefinitionsBindings();
337                 if(db != null){
338                     BindingsHandlerChains handlerChains = db.getHandlerChains();
339                     //there is only one handler chain
340
BindingsHandlerChain handlerChain =
341                             handlerChains.getHandlerChains().iterator().next();
342                     Collection JavaDoc<BindingsHandler> handlers = handlerChain.getHandlers();
343                     for(BindingsHandler handler : handlers){
344                         BindingsHandlerClass handlerClass = handler.getHandlerClass();
345                         handlerClasses.add(handlerClass.getClassName());
346                     }
347                 }
348             }
349         }
350         final MessageHandlerPanel panel = new MessageHandlerPanel(project,
351                 (String JavaDoc[])handlerClasses.toArray(new String JavaDoc[handlerClasses.size()]), true, client.getName());
352         String JavaDoc title = NbBundle.getMessage(JaxWsNode.class,"TTL_MessageHandlerPanel");
353         DialogDescriptor dialogDesc = new DialogDescriptor(panel, title);
354         dialogDesc.setButtonListener(new ClientHandlerButtonListener(panel,
355                 bindingsModel, client, this, jaxWsModel));
356         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(dialogDesc);
357         dialog.setVisible(true);
358     }
359     
360     private BindingsModel getBindingsModel(){
361         String JavaDoc handlerBindingFile = client.getHandlerBindingFile();
362         BindingsModel bindingsModel = null;
363         
364         //if there is an existing handlerBindingFile, load it
365
try{
366             if(handlerBindingFile != null){
367                 JAXWSClientSupport support = JAXWSClientSupport.getJaxWsClientSupport(srcRoot);
368                 FileObject bindingsFolder = support.getBindingsFolderForClient(getName(), false);
369                 if(bindingsFolder != null){
370                     FileObject handlerBindingFO = bindingsFolder.getFileObject(handlerBindingFile);
371                     if(handlerBindingFO != null){
372                         ModelSource ms = Utilities.getModelSource(handlerBindingFO, true);
373                         bindingsModel = BindingsModelFactory.getDefault().getModel(ms);
374                     }
375                 }
376             }
377         }catch(Exception JavaDoc e){
378             ErrorManager.getDefault().notify(e);
379             return null;
380         }
381         return bindingsModel;
382     }
383     
384     WsdlModeler getWsdlModeler() {
385         if (getLocalWsdl()!=null) {
386             try {
387                 WsdlModeler modeler = WsdlModelerFactory.getDefault().getWsdlModeler(wsdlFileObject.getURL());
388                 if (modeler!=null) {
389                     String JavaDoc packageName = client.getPackageName();
390                     if (packageName!=null && client.isPackageNameForceReplace()) {
391                         // set the package name for the modeler
392
modeler.setPackageName(packageName);
393                     } else {
394                         modeler.setPackageName(null);
395                     }
396                     modeler.setCatalog(getJAXWSClientSupport().getCatalog());
397                     setBindings(modeler);
398                     return modeler;
399                 }
400             } catch (FileStateInvalidException ex) {
401                 ErrorManager.getDefault().log(ex.getLocalizedMessage());
402             }
403         } else {
404             ErrorManager.getDefault().log(ErrorManager.ERROR, NbBundle.getMessage(JaxWsNode.class,"ERR_missingLocalWsdl"));
405         }
406         return null;
407     }
408     
409     FileObject getLocalWsdl() {
410         if (wsdlFileObject==null) {
411             FileObject localWsdlocalFolder = getJAXWSClientSupport().getLocalWsdlFolderForClient(client.getName(),false);
412             if (localWsdlocalFolder!=null)
413                 wsdlFileObject=localWsdlocalFolder.getFileObject(client.getLocalWsdlFile());
414         }
415         return wsdlFileObject;
416     }
417     
418     JAXWSClientSupport getJAXWSClientSupport() {
419         return JAXWSClientSupport.getJaxWsClientSupport(srcRoot);
420     }
421     
422     private void setBindings(WsdlModeler wsdlModeler) {
423         Binding[] extbindings = client.getBindings();
424         if (extbindings==null || extbindings.length==0) {
425             wsdlModeler.setJAXBBindings(null);
426             return;
427         }
428         String JavaDoc[] bindingFiles = new String JavaDoc[extbindings.length];
429         for(int i = 0; i < extbindings.length; i++){
430             bindingFiles[i] = extbindings[i].getFileName();
431         }
432         
433         FileObject bindingsFolder = getJAXWSClientSupport().getBindingsFolderForClient(getName(),true);
434         List JavaDoc<URL JavaDoc> list = new ArrayList JavaDoc<URL JavaDoc>();
435         for (int i=0;i<bindingFiles.length;i++) {
436             FileObject fo = bindingsFolder.getFileObject(bindingFiles[i]);
437             try {
438                 list.add(fo.getURL());
439             } catch (FileStateInvalidException ex) {
440                 // if there is problem no bindings will be added
441
}
442         }
443         URL JavaDoc[] bindings = new URL JavaDoc[list.size()];
444         list.<URL JavaDoc>toArray(bindings);
445         wsdlModeler.setJAXBBindings(bindings);
446     }
447     
448     void setModelGenerationFinished(boolean value) {
449         modelGenerationFinished=value;
450     }
451     
452     JaxWsModel getJaxWsModel() {
453         return jaxWsModel;
454     }
455     
456     private FileObject findWsdlFolderForClient(JAXWSClientSupport support, String JavaDoc name) throws IOException JavaDoc {
457         FileObject globalWsdlFolder = support.getWsdlFolder(false);
458         if (globalWsdlFolder!=null) {
459             return globalWsdlFolder.getFileObject("client/"+name);
460         }
461         return null;
462     }
463 }
464
Popular Tags