KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > jaxrpc > actions > RefreshServiceAction


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.jaxrpc.actions;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import org.netbeans.modules.websvc.api.registry.WebServicesRegistryView;
26
27 import org.openide.NotifyDescriptor;
28 import org.openide.DialogDescriptor;
29 import org.openide.DialogDisplayer;
30 import org.openide.ErrorManager;
31 import org.openide.nodes.Node;
32 import org.openide.filesystems.FileLock;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.loaders.DataObject;
36 import org.openide.util.HelpCtx;
37 import org.openide.util.Lookup;
38 import org.openide.util.NbBundle;
39 import org.openide.util.RequestProcessor;
40 import org.openide.util.actions.NodeAction;
41
42 import org.netbeans.modules.websvc.api.client.WebServicesClientSupport;
43 import org.netbeans.modules.websvc.jaxrpc.client.ui.RefreshWsdlPanel;
44 import org.netbeans.modules.websvc.jaxrpc.client.ui.DownloadWsdlPanel;
45 import org.netbeans.modules.websvc.wsdl.config.ServiceInformationImpl;
46
47 /**
48  *
49  * @author Peter Williams
50  */

51 public class RefreshServiceAction extends NodeAction {
52
53     protected boolean enable(Node[] activatedNodes) {
54         return true;
55     }
56
57     public HelpCtx getHelpCtx() {
58         // !PW FIXME use correct help context when known.
59
return HelpCtx.DEFAULT_HELP;
60     }
61
62     public String JavaDoc getName() {
63         return NbBundle.getMessage(RefreshServiceAction.class, "LBL_RefreshWsdl"); // NOI18N
64
}
65
66     protected void performAction(Node[] activatedNodes) {
67
68         assert (activatedNodes != null && activatedNodes.length == 1);
69
70         // Invoked on ClientRootNode to refresh the list of webservice clients
71
// in this project.
72
WebServicesClientSupport support = null;
73         FileObject fo = null;
74
75         // Find WebServicesClientSupport from activated node.
76
DataObject dobj = (DataObject) activatedNodes[0].getLookup().lookup(DataObject.class);
77         if(dobj != null) {
78             fo = dobj.getPrimaryFile();
79             support = WebServicesClientSupport.getWebServicesClientSupport(fo);
80         }
81         
82         final WebServicesClientSupport clientSupport = support;
83         final FileObject wsdlFO = fo;
84         
85         if(clientSupport == null) {
86             String JavaDoc mes = NbBundle.getMessage(RefreshServiceAction.class, "ERR_NoClientSupport", activatedNodes[0]); // NOI18N
87
NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE);
88             DialogDisplayer.getDefault().notify(desc);
89             return;
90         }
91
92         ServiceInformationImpl wsdlModel = new ServiceInformationImpl(dobj);
93         String JavaDoc [] supportedServices = wsdlModel.getServiceNames();
94
95         // When looking up service information in the project, use the name of the wsdl file, not the
96
// name of the service.
97
final String JavaDoc wsdlName = dobj.getName();
98         final String JavaDoc wsdlSource = clientSupport.getWsdlSource(wsdlName);
99         final RefreshWsdlPanel refreshPanel = new RefreshWsdlPanel(wsdlSource, supportedServices);
100         final DialogDescriptor descriptor = new DialogDescriptor(refreshPanel,
101             NbBundle.getMessage(RefreshServiceAction.class, "LBL_RefreshWsdlForService")); // NOI18N
102
refreshPanel.setDescriptor(descriptor);
103         // !PW FIXME put help context here when known to get a displayed help button on the panel.
104
// descriptor.setHelpCtx(new HelpCtx("HelpCtx_RefreshClientWsdlHelp"));
105
RequestProcessor.getDefault().post(new Runnable JavaDoc() {
106             public void run() {
107                 DialogDisplayer.getDefault().notify(descriptor);
108                 if (NotifyDescriptor.OK_OPTION.equals(descriptor.getValue())) {
109                     String JavaDoc newWsdlSource = refreshPanel.getWsdlSource();
110                     if(newWsdlSource.indexOf("://") != -1) {
111                         refreshViaUrl(wsdlFO, newWsdlSource);
112                     } else {
113                         refreshViaFile(wsdlFO, newWsdlSource);
114                     }
115                     if(!newWsdlSource.equals(wsdlSource)) {
116                         clientSupport.setWsdlSource(wsdlName, newWsdlSource);
117                     }
118                     WebServicesRegistryView registryView = (WebServicesRegistryView) Lookup.getDefault().lookup(WebServicesRegistryView.class);
119                     registryView.registerService(wsdlFO, true);
120                 }
121             }
122             
123         });
124     }
125
126     protected boolean asynchronous() {
127         return false;
128     }
129
130     private void refreshViaUrl(FileObject wsdlFO, String JavaDoc url) {
131         DownloadWsdlPanel downloadPanel = new DownloadWsdlPanel(url);
132         DialogDescriptor descriptor = new DialogDescriptor(downloadPanel,
133             NbBundle.getMessage(RefreshServiceAction.class, "LBL_DownloadWsdl")); // NOI18N
134
downloadPanel.setDescriptor(descriptor);
135         DialogDisplayer.getDefault().notify(descriptor);
136         if (NotifyDescriptor.OK_OPTION.equals(descriptor.getValue())) {
137             // grab wsdl string and write to target file.
138
byte [] wsdlBuf = downloadPanel.getWsdl();
139             FileObject tmpFO = writeTempWsdl(wsdlFO.getName(), wsdlBuf);
140             if(tmpFO != null) {
141                 updateWsdl(wsdlFO, tmpFO);
142                 try {
143                     tmpFO.delete();
144                 } catch(IOException JavaDoc ex) {
145                     String JavaDoc message = NbBundle.getMessage(RefreshServiceAction.class,
146                         "ERR_CannotDeleteTempWsdlFile", ex.getLocalizedMessage()); // NOI18N
147
ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, message);
148                 }
149             }
150         }
151     }
152
153     private FileObject writeTempWsdl(String JavaDoc prefix, byte [] content) {
154         FileObject tmpFO = null;
155
156         try {
157             tmpFO = FileUtil.toFileObject(FileUtil.normalizeFile(File.createTempFile(prefix, "wsdl"))); // NOI18N
158
FileLock lock = tmpFO.lock();
159             OutputStream JavaDoc out = null;
160
161             try {
162                 // write content to the file.
163
out = tmpFO.getOutputStream(lock);
164                 try {
165                     out.write(content);
166                     out.flush();
167                 } finally {
168                     if(out != null) {
169                         out.close();
170                     }
171                 }
172             } finally {
173                 lock.releaseLock();
174             }
175         } catch(IOException JavaDoc ex) {
176             ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex);
177         }
178
179         return tmpFO;
180     }
181
182     private void refreshViaFile(FileObject wsdlFO, String JavaDoc path) {
183         File JavaDoc normalizedFile = FileUtil.normalizeFile(new File JavaDoc(path));
184         FileObject newWsdlFO = FileUtil.toFileObject(normalizedFile);
185         if(newWsdlFO != null) {
186             updateWsdl(wsdlFO, newWsdlFO);
187         } else {
188             String JavaDoc message = NbBundle.getMessage(RefreshServiceAction.class,
189                 "ERR_CannotGetFileObject", normalizedFile.getPath()); // NOI18N
190
ErrorManager.getDefault().log(ErrorManager.ERROR, message);
191         }
192     }
193
194     private void updateWsdl(FileObject wsdlFO, FileObject newWsdlFO) {
195         try {
196             // Cache what we need to do the copy.
197
FileObject targetDir = wsdlFO.getParent();
198             String JavaDoc name = wsdlFO.getName();
199             String JavaDoc ext = wsdlFO.getExt();
200
201             // delete existing file.
202
wsdlFO.delete();
203
204             // copy new file into place.
205
newWsdlFO.copy(targetDir, name, ext);
206         } catch(IOException JavaDoc ex) {
207             // !PW Should we beautify this sort of error?
208
ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex);
209         }
210     }
211 }
212
Popular Tags