001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.websvc.jaxrpc.actions;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.io.OutputStream;
047: import org.netbeans.modules.websvc.api.registry.WebServicesRegistryView;
048:
049: import org.openide.NotifyDescriptor;
050: import org.openide.DialogDescriptor;
051: import org.openide.DialogDisplayer;
052: import org.openide.ErrorManager;
053: import org.openide.nodes.Node;
054: import org.openide.filesystems.FileLock;
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileUtil;
057: import org.openide.loaders.DataObject;
058: import org.openide.util.HelpCtx;
059: import org.openide.util.Lookup;
060: import org.openide.util.NbBundle;
061: import org.openide.util.RequestProcessor;
062: import org.openide.util.actions.NodeAction;
063:
064: import org.netbeans.modules.websvc.api.client.WebServicesClientSupport;
065: import org.netbeans.modules.websvc.jaxrpc.client.ui.RefreshWsdlPanel;
066: import org.netbeans.modules.websvc.jaxrpc.client.ui.DownloadWsdlPanel;
067: import org.netbeans.modules.websvc.wsdl.config.ServiceInformationImpl;
068:
069: /**
070: *
071: * @author Peter Williams
072: */
073: public class RefreshServiceAction extends NodeAction {
074:
075: protected boolean enable(Node[] activatedNodes) {
076: return (activatedNodes != null && activatedNodes.length == 1);
077: }
078:
079: public HelpCtx getHelpCtx() {
080: // !PW FIXME use correct help context when known.
081: return HelpCtx.DEFAULT_HELP;
082: }
083:
084: public String getName() {
085: return NbBundle.getMessage(RefreshServiceAction.class,
086: "LBL_RefreshWsdl"); // NOI18N
087: }
088:
089: protected void performAction(Node[] activatedNodes) {
090:
091: assert (activatedNodes != null && activatedNodes.length == 1);
092:
093: // Invoked on ClientRootNode to refresh the list of webservice clients
094: // in this project.
095: WebServicesClientSupport support = null;
096: FileObject fo = null;
097:
098: // Find WebServicesClientSupport from activated node.
099: DataObject dobj = (DataObject) activatedNodes[0].getLookup()
100: .lookup(DataObject.class);
101: if (dobj != null) {
102: fo = dobj.getPrimaryFile();
103: support = WebServicesClientSupport
104: .getWebServicesClientSupport(fo);
105: }
106:
107: final WebServicesClientSupport clientSupport = support;
108: final FileObject wsdlFO = fo;
109:
110: if (clientSupport == null) {
111: String mes = NbBundle.getMessage(
112: RefreshServiceAction.class, "ERR_NoClientSupport",
113: activatedNodes[0]); // NOI18N
114: NotifyDescriptor desc = new NotifyDescriptor.Message(mes,
115: NotifyDescriptor.Message.ERROR_MESSAGE);
116: DialogDisplayer.getDefault().notify(desc);
117: return;
118: }
119:
120: ServiceInformationImpl wsdlModel = new ServiceInformationImpl(
121: dobj);
122: String[] supportedServices = wsdlModel.getServiceNames();
123:
124: // When looking up service information in the project, use the name of the wsdl file, not the
125: // name of the service.
126: final String wsdlName = dobj.getName();
127: final String wsdlSource = clientSupport.getWsdlSource(wsdlName);
128: final RefreshWsdlPanel refreshPanel = new RefreshWsdlPanel(
129: wsdlSource, supportedServices);
130: final DialogDescriptor descriptor = new DialogDescriptor(
131: refreshPanel, NbBundle.getMessage(
132: RefreshServiceAction.class,
133: "LBL_RefreshWsdlForService")); // NOI18N
134: refreshPanel.setDescriptor(descriptor);
135: // !PW FIXME put help context here when known to get a displayed help button on the panel.
136: // descriptor.setHelpCtx(new HelpCtx("HelpCtx_RefreshClientWsdlHelp"));
137: RequestProcessor.getDefault().post(new Runnable() {
138: public void run() {
139: DialogDisplayer.getDefault().notify(descriptor);
140: if (NotifyDescriptor.OK_OPTION.equals(descriptor
141: .getValue())) {
142: String newWsdlSource = refreshPanel.getWsdlSource();
143: if (newWsdlSource.indexOf("://") != -1) {
144: refreshViaUrl(wsdlFO, newWsdlSource);
145: } else {
146: refreshViaFile(wsdlFO, newWsdlSource);
147: }
148: if (!newWsdlSource.equals(wsdlSource)) {
149: clientSupport.setWsdlSource(wsdlName,
150: newWsdlSource);
151: }
152: WebServicesRegistryView registryView = (WebServicesRegistryView) Lookup
153: .getDefault().lookup(
154: WebServicesRegistryView.class);
155: registryView.registerService(wsdlFO, true);
156: }
157: }
158:
159: });
160: }
161:
162: protected boolean asynchronous() {
163: return false;
164: }
165:
166: private void refreshViaUrl(FileObject wsdlFO, String url) {
167: DownloadWsdlPanel downloadPanel = new DownloadWsdlPanel(url);
168: DialogDescriptor descriptor = new DialogDescriptor(
169: downloadPanel, NbBundle.getMessage(
170: RefreshServiceAction.class, "LBL_DownloadWsdl")); // NOI18N
171: downloadPanel.setDescriptor(descriptor);
172: DialogDisplayer.getDefault().notify(descriptor);
173: if (NotifyDescriptor.OK_OPTION.equals(descriptor.getValue())) {
174: // grab wsdl string and write to target file.
175: byte[] wsdlBuf = downloadPanel.getWsdl();
176: FileObject tmpFO = writeTempWsdl(wsdlFO.getName(), wsdlBuf);
177: if (tmpFO != null) {
178: updateWsdl(wsdlFO, tmpFO);
179: try {
180: tmpFO.delete();
181: } catch (IOException ex) {
182: String message = NbBundle.getMessage(
183: RefreshServiceAction.class,
184: "ERR_CannotDeleteTempWsdlFile", ex
185: .getLocalizedMessage()); // NOI18N
186: ErrorManager.getDefault().log(
187: ErrorManager.INFORMATIONAL, message);
188: }
189: }
190: }
191: }
192:
193: private FileObject writeTempWsdl(String prefix, byte[] content) {
194: FileObject tmpFO = null;
195:
196: try {
197: if (prefix == null || prefix.length() < 3) {
198: prefix = "tmp";// need prefix atleast 3 characters long, fix for bug 113430
199: }
200: tmpFO = FileUtil.toFileObject(FileUtil.normalizeFile(File
201: .createTempFile(prefix, "wsdl"))); // NOI18N
202: FileLock lock = tmpFO.lock();
203: OutputStream out = null;
204:
205: try {
206: // write content to the file.
207: out = tmpFO.getOutputStream(lock);
208: try {
209: out.write(content);
210: out.flush();
211: } finally {
212: if (out != null) {
213: out.close();
214: }
215: }
216: } finally {
217: lock.releaseLock();
218: }
219: } catch (IOException ex) {
220: ErrorManager.getDefault()
221: .notify(ErrorManager.EXCEPTION, ex);
222: }
223:
224: return tmpFO;
225: }
226:
227: private void refreshViaFile(FileObject wsdlFO, String path) {
228: File normalizedFile = FileUtil.normalizeFile(new File(path));
229: FileObject newWsdlFO = FileUtil.toFileObject(normalizedFile);
230: if (newWsdlFO != null) {
231: updateWsdl(wsdlFO, newWsdlFO);
232: } else {
233: String message = NbBundle
234: .getMessage(RefreshServiceAction.class,
235: "ERR_CannotGetFileObject", normalizedFile
236: .getPath()); // NOI18N
237: ErrorManager.getDefault().log(ErrorManager.ERROR, message);
238: }
239: }
240:
241: private void updateWsdl(FileObject wsdlFO, FileObject newWsdlFO) {
242: try {
243: // Cache what we need to do the copy.
244: FileObject targetDir = wsdlFO.getParent();
245: String name = wsdlFO.getName();
246: String ext = wsdlFO.getExt();
247:
248: // delete existing file.
249: wsdlFO.delete();
250:
251: // copy new file into place.
252: newWsdlFO.copy(targetDir, name, ext);
253: } catch (IOException ex) {
254: // !PW Should we beautify this sort of error?
255: ErrorManager.getDefault()
256: .notify(ErrorManager.EXCEPTION, ex);
257: }
258: }
259: }
|