1 19 20 package org.netbeans.modules.websvc.jaxrpc.actions; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 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 51 public class RefreshServiceAction extends NodeAction { 52 53 protected boolean enable(Node[] activatedNodes) { 54 return true; 55 } 56 57 public HelpCtx getHelpCtx() { 58 return HelpCtx.DEFAULT_HELP; 60 } 61 62 public String getName() { 63 return NbBundle.getMessage(RefreshServiceAction.class, "LBL_RefreshWsdl"); } 65 66 protected void performAction(Node[] activatedNodes) { 67 68 assert (activatedNodes != null && activatedNodes.length == 1); 69 70 WebServicesClientSupport support = null; 73 FileObject fo = null; 74 75 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 mes = NbBundle.getMessage(RefreshServiceAction.class, "ERR_NoClientSupport", activatedNodes[0]); 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 [] supportedServices = wsdlModel.getServiceNames(); 94 95 final String wsdlName = dobj.getName(); 98 final String 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")); refreshPanel.setDescriptor(descriptor); 103 RequestProcessor.getDefault().post(new Runnable () { 106 public void run() { 107 DialogDisplayer.getDefault().notify(descriptor); 108 if (NotifyDescriptor.OK_OPTION.equals(descriptor.getValue())) { 109 String 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 url) { 131 DownloadWsdlPanel downloadPanel = new DownloadWsdlPanel(url); 132 DialogDescriptor descriptor = new DialogDescriptor(downloadPanel, 133 NbBundle.getMessage(RefreshServiceAction.class, "LBL_DownloadWsdl")); downloadPanel.setDescriptor(descriptor); 135 DialogDisplayer.getDefault().notify(descriptor); 136 if (NotifyDescriptor.OK_OPTION.equals(descriptor.getValue())) { 137 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 ex) { 145 String message = NbBundle.getMessage(RefreshServiceAction.class, 146 "ERR_CannotDeleteTempWsdlFile", ex.getLocalizedMessage()); ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, message); 148 } 149 } 150 } 151 } 152 153 private FileObject writeTempWsdl(String prefix, byte [] content) { 154 FileObject tmpFO = null; 155 156 try { 157 tmpFO = FileUtil.toFileObject(FileUtil.normalizeFile(File.createTempFile(prefix, "wsdl"))); FileLock lock = tmpFO.lock(); 159 OutputStream out = null; 160 161 try { 162 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 ex) { 176 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex); 177 } 178 179 return tmpFO; 180 } 181 182 private void refreshViaFile(FileObject wsdlFO, String path) { 183 File normalizedFile = FileUtil.normalizeFile(new File (path)); 184 FileObject newWsdlFO = FileUtil.toFileObject(normalizedFile); 185 if(newWsdlFO != null) { 186 updateWsdl(wsdlFO, newWsdlFO); 187 } else { 188 String message = NbBundle.getMessage(RefreshServiceAction.class, 189 "ERR_CannotGetFileObject", normalizedFile.getPath()); ErrorManager.getDefault().log(ErrorManager.ERROR, message); 191 } 192 } 193 194 private void updateWsdl(FileObject wsdlFO, FileObject newWsdlFO) { 195 try { 196 FileObject targetDir = wsdlFO.getParent(); 198 String name = wsdlFO.getName(); 199 String ext = wsdlFO.getExt(); 200 201 wsdlFO.delete(); 203 204 newWsdlFO.copy(targetDir, name, ext); 206 } catch(IOException ex) { 207 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex); 209 } 210 } 211 } 212 | Popular Tags |