1 19 20 package org.netbeans.modules.websvc.wsitconf; 21 22 import java.util.Collection ; 23 import javax.swing.undo.UndoManager ; 24 import org.netbeans.api.project.FileOwnerQuery; 25 import org.netbeans.api.project.Project; 26 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 27 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform; 28 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 29 import org.netbeans.modules.websvc.api.jaxws.client.JAXWSClientSupport; 30 import org.netbeans.modules.websvc.api.jaxws.project.config.Client; 31 import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel; 32 import org.netbeans.modules.websvc.api.jaxws.project.config.Service; 33 import org.netbeans.modules.websvc.jaxws.api.JAXWSSupport; 34 import org.netbeans.modules.websvc.wsitconf.spi.SecurityProfile; 35 import org.netbeans.modules.websvc.wsitconf.spi.SecurityProfileRegistry; 36 import org.netbeans.modules.websvc.wsitconf.ui.ErrorTopComponent; 37 import org.netbeans.modules.websvc.wsitconf.ui.service.ServiceTopComponent; 38 import org.netbeans.modules.websvc.wsitconf.ui.client.ClientTopComponent; 39 import org.netbeans.modules.websvc.wsitconf.util.UndoManagerHolder; 40 import org.netbeans.modules.websvc.wsitconf.wsdlmodelext.WSITModelSupport; 41 import org.netbeans.modules.xml.retriever.catalog.Utilities; 42 import org.netbeans.modules.xml.wsdl.model.*; 43 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 44 import org.openide.ErrorManager; 45 import org.openide.cookies.SaveCookie; 46 import org.openide.filesystems.FileObject; 47 import org.openide.loaders.DataObject; 48 import org.openide.nodes.Node; 49 import org.openide.util.Lookup; 50 import org.openide.util.NbBundle; 51 import java.io.IOException ; 52 import java.net.MalformedURLException ; 53 import java.util.LinkedList ; 54 import javax.swing.JComponent ; 55 import org.netbeans.api.java.project.JavaProjectConstants; 56 import org.netbeans.api.project.ProjectUtils; 57 import org.netbeans.api.project.SourceGroup; 58 import org.netbeans.api.project.Sources; 59 import org.netbeans.modules.websvc.core.wseditor.spi.WSEditor; 60 import org.netbeans.modules.websvc.wsitconf.util.Util; 61 import org.netbeans.modules.xml.xam.ModelSource; 62 import org.openide.loaders.DataObjectNotFoundException; 63 64 68 public class WSITEditor implements WSEditor, UndoManagerHolder { 69 70 private UndoManager undoManager; 71 private Collection <FileObject> createdFiles = new LinkedList (); 72 73 76 public WSITEditor() { 77 populateProfileRegistry(); 78 } 79 80 public String getTitle() { 81 return NbBundle.getMessage(WSITEditor.class, "EDITOR_TITLE"); } 83 84 public JComponent createWSEditorComponent(Node node, JaxWsModel jaxWsModel) { 85 86 WSDLModel clientWsdlModel; 87 WSDLModel wsdlModel; 88 89 Client client = (Client)node.getLookup().lookup(Client.class); 91 Service service = (Service)node.getLookup().lookup(Service.class); 93 94 Project p = null; 95 if (jaxWsModel != null) { 96 p = FileOwnerQuery.getOwner(jaxWsModel.getJaxWsFile()); 97 } 98 99 boolean wsitSupported = false; 100 if (client != null){ if (p != null) { 102 JAXWSClientSupport wscs = JAXWSClientSupport.getJaxWsClientSupport(p.getProjectDirectory()); 103 if (wscs != null) { 104 wsitSupported = Util.isWsitSupported(p); 105 if (wsitSupported) { 106 try { 107 clientWsdlModel = WSITModelSupport.getModel(node, jaxWsModel, this, true, createdFiles); 108 wsdlModel = WSITModelSupport.getServiceModelForClient(wscs, client); 109 return new ClientTopComponent(client, jaxWsModel, clientWsdlModel, wsdlModel, node); 110 } catch(Exception e){ 111 ErrorManager.getDefault().notify(e); 112 } 113 } else { 114 return new ErrorTopComponent(NbBundle.getMessage(WSITEditor.class, "TXT_WSIT_NotDetected", getServerName(p))); 115 } 116 } 117 } 118 } else { 119 if (p != null) { 120 JAXWSSupport wss = JAXWSSupport.getJAXWSSupport(p.getProjectDirectory()); 121 if (wss != null) { 122 wsitSupported = Util.isWsitSupported(p); 123 if (wsitSupported) { 124 try { 125 wsdlModel = WSITModelSupport.getModel(node, jaxWsModel, this, true, createdFiles); 126 return new ServiceTopComponent(service, jaxWsModel, wsdlModel, node, getUndoManager()); 127 } catch(Exception e){ 128 ErrorManager.getDefault().notify(e); 129 } 130 } else { 131 return new ErrorTopComponent(NbBundle.getMessage(WSITEditor.class, "TXT_WSIT_NotDetected", getServerName(p))); 132 } 133 } 134 } 135 } 136 return new ErrorTopComponent(NbBundle.getMessage(WSITEditor.class, "TXT_WSIT_NotSupported")); 137 } 138 139 public void save(Node node, JaxWsModel jaxWsModel) { 140 if (node == null) return; 141 try { 142 WSDLModel model = WSITModelSupport.getModel(node, jaxWsModel, this, false, createdFiles); 143 if (model != null) { 144 FileObject wsdlFO = Utilities.getFileObject(model.getModelSource()); 145 if (wsdlFO == null) { 146 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Cannot find fileobject in lookup for: " + model.getModelSource()); } 148 DataObject wsdlDO = DataObject.find(wsdlFO); 149 if ((wsdlDO != null) && (wsdlDO.isModified())) { 150 SaveCookie wsdlSaveCookie = (SaveCookie)wsdlDO.getCookie(SaveCookie.class); 151 if(wsdlSaveCookie != null){ 152 wsdlSaveCookie.save(); 153 } 154 wsdlDO.setModified(false); 155 } 156 } 157 } catch (Exception e){ 158 ErrorManager.getDefault().notify(e); 159 } 160 } 161 162 public void cancel(Node node, JaxWsModel jaxWsModel) { 163 if (node == null) return; 164 WSDLModel model = null; 165 166 168 FileObject srcRoot = (FileObject) node.getLookup().lookup(FileObject.class); 169 if (srcRoot != null) { 170 Project p = FileOwnerQuery.getOwner(srcRoot); 171 if (p != null) { 172 Sources sources = ProjectUtils.getSources(p); 173 if (sources != null) { 174 SourceGroup[] sourceGroups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 175 if ((sourceGroups != null) || (sourceGroups.length > 0)) { 176 FileObject srcFolder = sourceGroups[0].getRootFolder(); 177 WSDLModel mainModel = WSITModelSupport.getMainClientModel(srcFolder); 178 if (mainModel != null) { 179 Collection <Import> imports = mainModel.getDefinitions().getImports(); 180 for (Import i : imports) { 181 try { 182 WSDLModel importedModel = i.getImportedWSDLModel(); 183 ModelSource importedms = importedModel.getModelSource(); 184 FileObject importedfo = org.netbeans.modules.xml.retriever.catalog.Utilities.getFileObject(importedms); 185 mainModel.startTransaction(); 186 if (createdFiles.contains(importedfo)) { 187 mainModel.getDefinitions().removeImport(i); 188 } 189 mainModel.endTransaction(); 190 FileObject mainFO = Utilities.getFileObject(mainModel.getModelSource()); 191 if (mainFO == null) { 192 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, 193 "Cannot find fileobject in lookup for: " + model.getModelSource()); } 195 try { 196 DataObject mainDO = DataObject.find(mainFO); 197 if ((mainDO != null) && (mainDO.isModified())) { 198 SaveCookie wsdlSaveCookie = (SaveCookie)mainDO.getCookie(SaveCookie.class); 199 if(wsdlSaveCookie != null){ 200 wsdlSaveCookie.save(); 201 } 202 mainDO.setModified(false); 203 } 204 } catch (IOException ioe) { 205 } 207 } catch (CatalogModelException ex) { 208 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage()); 209 } 210 } 211 } 212 } 213 } 214 } 215 } 216 217 try { 219 model = WSITModelSupport.getModel(node, jaxWsModel, this, false, createdFiles); 220 } catch (MalformedURLException ex) { 221 ex.printStackTrace(); 222 } catch (Exception ex) { 223 ex.printStackTrace(); 224 } 225 226 if (model != null) { 227 try { 228 if (getUndoManager() != null) { 229 while (getUndoManager().canUndo()) { 230 getUndoManager().undo(); 231 } 232 } 233 } catch (Exception e){ 234 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, e.getMessage()); 235 } 236 FileObject fo = org.netbeans.modules.xml.retriever.catalog.Utilities.getFileObject(model.getModelSource()); 237 DataObject dO = null; 238 try { 239 dO = DataObject.find(fo); 240 } catch (DataObjectNotFoundException ex) { 241 ErrorManager.getDefault().notify(ex); 242 } 243 if (dO != null) { 244 try { 245 model.sync(); 246 } catch (IOException ex) { 247 ErrorManager.getDefault().notify(ex); 248 } 249 dO.setModified(false); 250 } 251 } 252 253 if ((createdFiles != null) && (createdFiles.size() > 0)) { 255 for (FileObject fo : createdFiles) { 256 if (fo != null) { 257 try { 258 DataObject dO = DataObject.find(fo); 259 if (dO != null) { 260 dO.delete(); 261 } 262 } catch (IOException ex) { 263 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage()); } 265 } 266 } 267 } 268 } 269 270 private String getServerName(Project p) { 271 J2eeModuleProvider mp = (J2eeModuleProvider)p.getLookup().lookup(J2eeModuleProvider.class); 272 if (mp != null) { 273 String sID = mp.getServerInstanceID(); 274 J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(sID); 275 return j2eePlatform.getDisplayName(); 276 } 277 return null; 278 } 279 280 public UndoManager getUndoManager() { 281 return undoManager; 282 } 283 284 public void setUndoManager(UndoManager undoManager) { 285 this.undoManager = undoManager; 286 } 287 288 private SecurityProfileRegistry populateProfileRegistry() { 289 SecurityProfileRegistry registry = SecurityProfileRegistry.getDefault(); 290 if (registry.getSecurityProfiles().isEmpty()) { 291 Lookup.Result results = Lookup.getDefault(). 292 lookup(new Lookup.Template(SecurityProfile.class)); 293 Collection <SecurityProfile> profiles = results.allInstances(); 294 for (SecurityProfile p : profiles) { 295 registry.register(p); 296 } 297 } 298 299 return registry; 300 } 301 302 } 303 | Popular Tags |