1 19 20 package org.netbeans.modules.websvc.wsitconf.wsdlmodelext; 21 22 import java.io.FileWriter ; 23 import java.io.IOException ; 24 import java.net.MalformedURLException ; 25 import java.net.URI ; 26 import java.util.Collection ; 27 import java.util.HashSet ; 28 import java.util.List ; 29 import java.util.Vector ; 30 import javax.swing.undo.UndoManager ; 31 import javax.xml.namespace.QName ; 32 import org.netbeans.api.java.project.JavaProjectConstants; 33 import org.netbeans.api.java.source.CompilationController; 34 import org.netbeans.api.java.source.JavaSource; 35 import org.netbeans.api.project.FileOwnerQuery; 36 import org.netbeans.api.project.Project; 37 import org.netbeans.api.project.ProjectUtils; 38 import org.netbeans.api.project.SourceGroup; 39 import org.netbeans.api.project.Sources; 40 import org.netbeans.modules.websvc.api.jaxws.client.JAXWSClientSupport; 41 import org.netbeans.modules.websvc.api.jaxws.project.config.Client; 42 import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel; 43 import org.netbeans.modules.websvc.api.jaxws.project.config.Service; 44 import org.netbeans.modules.websvc.jaxws.api.JAXWSSupport; 45 import org.netbeans.modules.websvc.jaxwsruntimemodel.JavaWsdlMapper; 46 import org.netbeans.modules.websvc.wsitconf.util.UndoManagerHolder; 47 import org.netbeans.modules.websvc.wsitconf.WSITEditor; 48 import org.netbeans.modules.websvc.wsitconf.util.AbstractTask; 49 import org.netbeans.modules.websvc.wsitconf.util.SourceUtils; 50 import org.netbeans.modules.websvc.wsitmodelext.policy.Policy; 51 import org.netbeans.modules.websvc.wsitmodelext.policy.PolicyReference; 52 import org.netbeans.modules.xml.retriever.catalog.Utilities; 53 import org.netbeans.modules.xml.wsdl.model.Binding; 54 import org.netbeans.modules.xml.wsdl.model.Definitions; 55 import org.netbeans.modules.xml.wsdl.model.Import; 56 import org.netbeans.modules.xml.wsdl.model.Types; 57 import org.netbeans.modules.xml.wsdl.model.WSDLComponent; 58 import org.netbeans.modules.xml.wsdl.model.WSDLComponentFactory; 59 import org.netbeans.modules.xml.wsdl.model.WSDLModel; 60 import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory; 61 import org.netbeans.modules.xml.xam.ModelSource; 62 import org.netbeans.modules.xml.xam.locator.CatalogModel; 63 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 64 import org.openide.ErrorManager; 65 import org.openide.cookies.SaveCookie; 66 import org.openide.filesystems.FileObject; 67 import org.openide.filesystems.FileUtil; 68 import org.openide.loaders.DataObject; 69 import org.openide.nodes.Node; 70 import org.openide.util.Exceptions; 71 import org.openide.util.NbBundle; 72 73 77 public class WSITModelSupport { 78 79 80 public static final String CONFIG_WSDL_CLIENT_PREFIX = "wsit-client"; public static final String CONFIG_WSDL_SERVICE_PREFIX = "wsit-"; 83 84 public static final String MAIN_CONFIG_EXTENSION = "xml"; public static final String CONFIG_WSDL_EXTENSION = "xml"; 87 88 public WSITModelSupport() { 89 } 90 91 public static WSDLModel getModel(Node node, JaxWsModel jaxWsModel, UndoManagerHolder umHolder, boolean create, Collection createdFiles) throws MalformedURLException , Exception { 92 93 WSDLModel model = null; 94 95 Client client = (Client)node.getLookup().lookup(Client.class); 97 98 Service service = (Service)node.getLookup().lookup(Service.class); 100 101 if (client != null) { return getModelForClient(node, client, create, createdFiles); 103 } else if (service != null) { try { 105 String wsdlUrl = service.getWsdlUrl(); 106 if (wsdlUrl == null) { model = getModelForServiceFromJava(node, jaxWsModel, create, createdFiles); 108 } else { 109 model = getModelForServiceFromWsdl(jaxWsModel, service); 110 } 111 } catch (IOException ioe) { 112 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage()); 113 } 114 } else { ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Unable to identify node type: " + node); } 117 118 if ((model != null) && (umHolder != null) && (umHolder.getUndoManager() == null)) { 119 UndoManager undoManager = new UndoManager (); 120 model.addUndoableEditListener(undoManager); umHolder.setUndoManager(undoManager); 122 } 123 return model; 124 } 125 126 private static WSDLModel getModelFromFO(FileObject wsdlFO, boolean editable) { 127 WSDLModel model = null; 128 ModelSource ms = org.netbeans.modules.xml.retriever.catalog.Utilities.getModelSource(wsdlFO, editable); 129 try { 130 model = WSDLModelFactory.getDefault().getModel(ms); 131 if (model != null) { 132 model.sync(); 133 } 134 } catch (IOException ex) { 135 ex.printStackTrace(); 136 } 137 return model; 138 } 139 140 142 public static WSDLModel getModelForClient(Node node, Client client, boolean create, Collection createdFiles) throws IOException { 143 144 WSDLModel model = null; 145 146 FileObject srcRoot = (FileObject) node.getLookup().lookup(FileObject.class); 147 Project p = FileOwnerQuery.getOwner(srcRoot); 148 Sources sources = ProjectUtils.getSources(p); 149 SourceGroup[] sourceGroups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 150 151 if ((sourceGroups == null) || (sourceGroups.length <= 0)) { 152 return null; 154 } 155 FileObject srcFolder = sourceGroups[0].getRootFolder(); 156 157 FileObject catalogfo = Utilities.getProjectCatalogFileObject(p); 158 ModelSource catalogms = Utilities.getModelSource(catalogfo, false); 159 160 try { 161 CatalogModel cm = Utilities.getCatalogModel(catalogms); 162 ModelSource originalms = cm.getModelSource(URI.create(client.getWsdlUrl())); 163 FileObject originalWsdlFO = Utilities.getFileObject(originalms); 164 WSDLModel originalwsdlmodel = getModelFromFO(originalWsdlFO, true); 165 166 FileObject configFO = srcFolder.getFileObject(originalWsdlFO.getNameExt()); 168 if ((configFO != null) && (configFO.isValid())) { 169 return getModelFromFO(configFO, true); 170 } 171 172 if (create) { 173 FileObject mainConfigFO = srcFolder.getFileObject(CONFIG_WSDL_CLIENT_PREFIX, MAIN_CONFIG_EXTENSION); 175 if (mainConfigFO == null) { 176 mainConfigFO = createMainConfig(srcFolder, createdFiles); 177 } 178 179 configFO = FileUtil.copyFile(originalWsdlFO, srcFolder, originalWsdlFO.getName()); 181 if (configFO != null) { 182 if (createdFiles != null) { 183 createdFiles.add(configFO); 184 } 185 model = getModelFromFO(configFO, true); 186 removePolicies(model); 187 removeTypes(model); 188 } 189 190 copyImports(originalwsdlmodel, srcFolder, createdFiles); 191 192 WSDLModel mainModel = getModelFromFO(mainConfigFO, true); 194 195 mainModel.startTransaction(); 196 WSDLComponentFactory wcf = mainModel.getFactory(); 197 198 org.netbeans.modules.xml.wsdl.model.Import imp = wcf.createImport(); 199 imp.setLocation(originalWsdlFO.getNameExt()); 200 imp.setNamespace(model.getDefinitions().getTargetNamespace()); 201 Definitions def = mainModel.getDefinitions(); 202 def.setName("mainclientconfig"); def.addImport(imp); 204 205 mainModel.endTransaction(); 206 207 DataObject mainConfigDO = DataObject.find(mainConfigFO); 208 if ((mainConfigDO != null) && (mainConfigDO.isModified())) { 209 SaveCookie wsdlSaveCookie = (SaveCookie)mainConfigDO.getCookie(SaveCookie.class); 210 if(wsdlSaveCookie != null){ 211 wsdlSaveCookie.save(); 212 } 213 mainConfigDO.setModified(false); 214 } 215 216 DataObject configDO = DataObject.find(configFO); 217 if ((configDO != null) && (configDO.isModified())) { 218 SaveCookie wsdlSaveCookie = (SaveCookie)configDO.getCookie(SaveCookie.class); 219 if(wsdlSaveCookie != null){ 220 wsdlSaveCookie.save(); 221 } 222 configDO.setModified(false); 223 } 224 } 225 226 } catch (CatalogModelException ex) { 227 ex.printStackTrace(); 228 } 229 230 return model; 231 } 232 233 private static void copyImports(final WSDLModel model, final FileObject srcFolder, Collection createdFiles) throws CatalogModelException { 234 Collection <Import> imports = model.getDefinitions().getImports(); 235 for (Import i : imports) { 236 WSDLModel importedModel = i.getImportedWSDLModel(); 237 ModelSource importedms = importedModel.getModelSource(); 238 FileObject importedfo = Utilities.getFileObject(importedms); 239 model.startTransaction(); 240 i.setLocation(importedfo.getNameExt()); 241 model.endTransaction(); 242 try { 243 FileObject configFO = FileUtil.copyFile(importedfo, srcFolder, importedfo.getName()); 244 if (createdFiles != null) { 245 createdFiles.add(configFO); 246 } 247 copyImports(importedModel, srcFolder, createdFiles); 248 } catch (IOException e) { 249 } 251 } 252 } 253 254 private static FileObject getFOForModel(WSDLModel model) { 255 ModelSource ms = model.getModelSource(); 256 return Utilities.getFileObject(ms); 257 } 258 259 262 private static FileObject createMainConfig(FileObject folder, Collection createdFiles) { 263 FileObject mainConfig = null; 264 try { 265 mainConfig = FileUtil.createData(folder, CONFIG_WSDL_CLIENT_PREFIX + "." + MAIN_CONFIG_EXTENSION); if ((mainConfig != null) && (mainConfig.isValid()) && !(mainConfig.isVirtual())) { 267 if (createdFiles != null) { 268 createdFiles.add(mainConfig); 269 } 270 FileWriter fw = new FileWriter (FileUtil.toFile(mainConfig)); 271 fw.write(NbBundle.getMessage(WSITEditor.class, "EMPTY_WSDL")); fw.close(); 273 mainConfig.refresh(true); 274 } 275 } catch (IOException ex) { 276 ex.printStackTrace(); 277 } 278 return mainConfig; 279 } 280 281 public static WSDLModel getMainClientModel(FileObject folder) { 282 WSDLModel model = null; 283 if (folder != null) { 284 FileObject mainConfig = folder.getFileObject(CONFIG_WSDL_CLIENT_PREFIX, MAIN_CONFIG_EXTENSION); if (mainConfig != null) { 286 model = getModelFromFO(mainConfig, true); 287 } 288 } 289 return model; 290 } 291 292 private static FileObject getWsdlFO(FileObject folder, String wsdlLocation) { 293 String relativePath = wsdlLocation.substring(wsdlLocation.indexOf("/wsdl/") + 6); 294 return folder.getFileObject(relativePath); 295 } 296 297 299 public static WSDLModel getServiceModelForClient(JAXWSClientSupport supp, Client client) throws IOException , Exception { 300 FileObject originalWsdlFolder = supp.getLocalWsdlFolderForClient(client.getName(), false); 301 FileObject originalWsdlFO = originalWsdlFolder.getFileObject(client.getLocalWsdlFile()); 302 303 if ((originalWsdlFO != null) && (originalWsdlFO.isValid())) { return getModelFromFO(originalWsdlFO, false); 305 } 306 return null; 307 } 308 309 311 private static WSDLModel getModelForServiceFromWsdl(JaxWsModel jaxWsModel, Service service) throws IOException , Exception { 312 313 if (jaxWsModel == null) return null; 314 FileObject wsdlFO = null; 315 316 Project p = FileOwnerQuery.getOwner(jaxWsModel.getJaxWsFile()); 317 JAXWSSupport supp = JAXWSSupport.getJAXWSSupport(p.getProjectDirectory()); 318 String wsdlLocation = supp.getWsdlLocation(service.getName()); 319 320 wsdlFO = getWsdlFO(supp.getWsdlFolder(false), wsdlLocation); 321 return getModelFromFO(wsdlFO, true); 322 } 323 324 private static WSDLModel getModelForServiceFromJava(Node node, JaxWsModel jaxWsModel, boolean create, Collection createdFiles) throws IOException , Exception { 325 if ((jaxWsModel == null) || (node == null)) return null; 326 FileObject fo = (FileObject)node.getLookup().lookup(FileObject.class); 327 Project p = FileOwnerQuery.getOwner(jaxWsModel.getJaxWsFile()); 328 JAXWSSupport supp = JAXWSSupport.getJAXWSSupport(p.getProjectDirectory()); 329 return getModelForServiceFromJava(fo, supp, create, createdFiles); 330 } 331 332 334 public static WSDLModel getModelForServiceFromJava(FileObject jc, JAXWSSupport supp, boolean create, Collection createdFiles) throws IOException { 335 336 WSDLModel model = null; 337 String configWsdlName = CONFIG_WSDL_SERVICE_PREFIX; 338 339 try { 340 if (jc == null) return null; 341 final java.lang.String [] result = new java.lang.String [1]; 342 343 JavaSource js = JavaSource.forFileObject(jc); 344 js.runUserActionTask(new AbstractTask<CompilationController>() { 345 public void run(CompilationController controller) throws java.io.IOException { 346 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 347 SourceUtils sourceUtils = SourceUtils.newInstance(controller); 348 result[0] = sourceUtils.getTypeElement().getQualifiedName().toString(); 349 } 350 }, true); 351 352 configWsdlName += result[0]; 353 354 } catch (IOException ex) { 355 Exceptions.printStackTrace(ex); 356 } 357 358 if (supp.getWsdlFolder(false) != null) { 360 FileObject wsdlFO = supp.getWsdlFolder(false).getParent().getFileObject(configWsdlName, CONFIG_WSDL_EXTENSION); if ((wsdlFO != null) && (wsdlFO.isValid())) { return getModelFromFO(wsdlFO, true); 363 } 364 } 365 366 if (create) { 367 FileObject wsdlFolder = supp.getWsdlFolder(true).getParent(); 369 FileObject wsdlFO = wsdlFolder.getFileObject(configWsdlName, CONFIG_WSDL_EXTENSION); if ((wsdlFO == null) || !(FileUtil.toFile(wsdlFO).exists())) { 371 wsdlFO = wsdlFolder.createData(configWsdlName, CONFIG_WSDL_EXTENSION); if (createdFiles != null) { 373 createdFiles.add(wsdlFO); 374 } 375 FileWriter fw = new FileWriter (FileUtil.toFile(wsdlFO)); 376 fw.write(NbBundle.getMessage(WSITEditor.class, "EMPTY_WSDL")); fw.close(); 378 wsdlFO.refresh(true); 379 } 380 381 model = createModelFromFO(wsdlFO, jc); 383 wsdlFO.refresh(true); 384 } 385 386 return model; 387 } 388 389 private static WSDLModel createModelFromFO(FileObject wsdlFO, FileObject jc) { 390 WSDLModel model = null; 391 ModelSource ms = org.netbeans.modules.xml.retriever.catalog.Utilities.getModelSource(wsdlFO, true); 392 try { 393 model = WSDLModelFactory.getDefault().getModel(ms); 394 if (model != null) { 395 Definitions d = model.getDefinitions(); 396 if (d != null) { 397 398 model.startTransaction(); 399 WSDLComponentFactory wcf = model.getFactory(); 400 QName serviceQName = JavaWsdlMapper.getServiceName(jc); 401 String serviceLocalName = serviceQName.getLocalPart(); 402 String serviceTargetNamespace = serviceQName.getNamespaceURI(); 403 404 d.setName(serviceLocalName); 405 d.setTargetNamespace(serviceTargetNamespace); 406 407 org.netbeans.modules.xml.wsdl.model.Service s = wcf.createService(); 409 s.setName(serviceLocalName); 410 d.addService(s); 411 412 org.netbeans.modules.xml.wsdl.model.Port port = wcf.createPort(); 414 QName portName = JavaWsdlMapper.getPortName(jc, serviceTargetNamespace); 415 if (portName != null) { 416 port.setName(portName.getLocalPart()); 417 } 418 s.addPort(port); 419 420 org.netbeans.modules.xml.wsdl.model.Binding binding = wcf.createBinding(); 422 String bindingName = JavaWsdlMapper.getBindingName(jc, serviceTargetNamespace); 423 binding.setName(bindingName); 424 d.addBinding(binding); 425 426 port.setBinding(binding.createReferenceTo(binding, org.netbeans.modules.xml.wsdl.model.Binding.class)); 428 429 org.netbeans.modules.xml.wsdl.model.PortType portType = wcf.createPortType(); 431 QName portTypeName = JavaWsdlMapper.getPortTypeName(jc); 432 portType.setName(portTypeName.getLocalPart()); 433 d.addPortType(portType); 434 435 List <String > bindingOperationNames = JavaWsdlMapper.getOperationNames(jc); 437 for (String name : bindingOperationNames) { 438 439 org.netbeans.modules.xml.wsdl.model.BindingOperation bindingOperation = wcf.createBindingOperation(); 440 bindingOperation.setName(name); 441 binding.addBindingOperation(bindingOperation); 442 443 org.netbeans.modules.xml.wsdl.model.Message inputMsg = wcf.createMessage(); 444 inputMsg.setName(name); 445 d.addMessage(inputMsg); 446 447 org.netbeans.modules.xml.wsdl.model.Message outMsg = wcf.createMessage(); 448 outMsg.setName(name + "Response"); d.addMessage(outMsg); 450 451 org.netbeans.modules.xml.wsdl.model.RequestResponseOperation oper = wcf.createRequestResponseOperation(); 452 oper.setName(name); 453 portType.addOperation(oper); 454 455 List <String > faults = JavaWsdlMapper.getOperationFaults(jc, name); 456 for (String faultstr : faults) { 457 org.netbeans.modules.xml.wsdl.model.Message fMsg = wcf.createMessage(); 458 fMsg.setName(faultstr); 459 d.addMessage(fMsg); 460 461 org.netbeans.modules.xml.wsdl.model.Fault fault = wcf.createFault(); 462 fault.setName(faultstr); 463 oper.addFault(fault); 464 fault.setMessage(fault.createReferenceTo(fMsg, org.netbeans.modules.xml.wsdl.model.Message.class)); 465 } 466 467 org.netbeans.modules.xml.wsdl.model.Input input = wcf.createInput(); 468 oper.setInput(input); 469 input.setMessage(input.createReferenceTo(inputMsg, org.netbeans.modules.xml.wsdl.model.Message.class)); 470 471 org.netbeans.modules.xml.wsdl.model.Output out = wcf.createOutput(); 472 oper.setOutput(out); 473 out.setMessage(out.createReferenceTo(outMsg, org.netbeans.modules.xml.wsdl.model.Message.class)); 474 475 org.netbeans.modules.xml.wsdl.model.BindingOutput bindingOutput = wcf.createBindingOutput(); 476 bindingOperation.setBindingOutput(bindingOutput); 477 org.netbeans.modules.xml.wsdl.model.BindingInput bindingInput = wcf.createBindingInput(); 478 bindingOperation.setBindingInput(bindingInput); 479 480 List <String > operationFaults = JavaWsdlMapper.getOperationFaults(jc, name); 482 for (String fault : operationFaults) { 483 org.netbeans.modules.xml.wsdl.model.BindingFault bindingFault = wcf.createBindingFault(); 484 bindingFault.setName(fault); 485 bindingOperation.addBindingFault(bindingFault); 486 } 487 } 488 489 binding.setType(binding.createReferenceTo(portType, org.netbeans.modules.xml.wsdl.model.PortType.class)); 491 492 model.endTransaction(); 493 494 DataObject dO = DataObject.find(wsdlFO); 495 SaveCookie sc = (SaveCookie) dO.getCookie(SaveCookie.class); 496 sc.save(); 497 dO.setModified(false); 498 } 499 } 500 } catch (IOException ex) { 501 ex.printStackTrace(); 502 } 503 return model; 504 } 505 506 private static void removeTypes(WSDLModel model) { 507 model.startTransaction(); 508 Definitions d = model.getDefinitions(); 509 Types t = d.getTypes(); 510 if (t != null) { 511 t.getSchemas().retainAll(new Vector ()); 512 } 513 model.endTransaction(); 514 } 515 516 private static void removePolicies(WSDLModel model) { 517 model.startTransaction(); 518 removePolicyElements(model.getDefinitions()); 519 model.endTransaction(); 520 } 521 522 public static boolean isServiceFromWsdl(Node node) { 523 Service service = (Service)node.getLookup().lookup(Service.class); 524 if (service != null) { String wsdlUrl = service.getWsdlUrl(); 526 if (wsdlUrl != null) { return true; 528 } 529 } 530 return false; 531 } 532 533 534 private static void removePolicyElements(WSDLComponent c) { 535 List <Policy> policies = c.getExtensibilityElements(Policy.class); 536 for (Policy p : policies) { 537 c.removeExtensibilityElement(p); 538 } 539 List <PolicyReference> policyReferences = c.getExtensibilityElements(PolicyReference.class); 540 for (PolicyReference pr : policyReferences) { 541 c.removeExtensibilityElement(pr); 542 } 543 List <WSDLComponent> children = c.getChildren(); 544 for (WSDLComponent ch : children) { 545 removePolicyElements(ch); 546 } 547 } 548 549 public static void fillImportedBindings(final WSDLModel model, Collection <Binding> bindings, HashSet <FileObject> traversedModels) { 550 FileObject modelFO = getFOForModel(model); 551 if (traversedModels.contains(modelFO)) { 553 return; 554 } 555 traversedModels.add(modelFO); 556 557 Collection <Binding> importedBindings = model.getDefinitions().getBindings(); 558 bindings.addAll(importedBindings); 559 560 Collection <Import> imports = model.getDefinitions().getImports(); 561 for (Import i : imports) { 562 WSDLModel importedModel; 563 try { 564 importedModel = i.getImportedWSDLModel(); 565 fillImportedBindings(importedModel, bindings, traversedModels); 566 } catch (CatalogModelException ex) { 567 ex.printStackTrace(); 568 } 569 } 570 } 571 572 } 573 | Popular Tags |