KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsitconf > wsdlmodelext > WSITModelSupport


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 2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.websvc.wsitconf.wsdlmodelext;
21
22 import java.io.FileWriter JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Vector JavaDoc;
30 import javax.swing.undo.UndoManager JavaDoc;
31 import javax.xml.namespace.QName JavaDoc;
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 /**
74  *
75  * @author MartinG
76  */

77 public class WSITModelSupport {
78
79     /* prefix for config wsdls */
80     public static final String JavaDoc CONFIG_WSDL_CLIENT_PREFIX = "wsit-client"; //NOI18N
81
public static final String JavaDoc CONFIG_WSDL_SERVICE_PREFIX = "wsit-"; //NOI18N
82

83     /* extensions for config wsdls */
84     public static final String JavaDoc MAIN_CONFIG_EXTENSION = "xml"; //NOI18N
85
public static final String JavaDoc CONFIG_WSDL_EXTENSION = "xml"; //NOI18N
86

87     /** Creates a new instance of WSITModelSupport */
88     public WSITModelSupport() {
89     }
90  
91     public static WSDLModel getModel(Node node, JaxWsModel jaxWsModel, UndoManagerHolder umHolder, boolean create, Collection JavaDoc createdFiles) throws MalformedURLException JavaDoc, Exception JavaDoc {
92                         
93         WSDLModel model = null;
94         
95         //is it a client node?
96
Client client = (Client)node.getLookup().lookup(Client.class);
97
98         //is it a service node?
99
Service service = (Service)node.getLookup().lookup(Service.class);
100
101         if (client != null) { //it is a client
102
return getModelForClient(node, client, create, createdFiles);
103         } else if (service != null) { //it is a service
104
try {
105                 String JavaDoc wsdlUrl = service.getWsdlUrl();
106                 if (wsdlUrl == null) { // WS from Java
107
model = getModelForServiceFromJava(node, jaxWsModel, create, createdFiles);
108                 } else {
109                     model = getModelForServiceFromWsdl(jaxWsModel, service);
110                 }
111             } catch (IOException JavaDoc ioe) {
112                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
113             }
114         } else { //neither a client nor a service, get out of here
115
ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Unable to identify node type: " + node); //NOI18N
116
}
117         
118         if ((model != null) && (umHolder != null) && (umHolder.getUndoManager() == null)) {
119             UndoManager JavaDoc undoManager = new UndoManager JavaDoc();
120             model.addUndoableEditListener(undoManager); //maybe use WeakListener instead
121
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 JavaDoc ex) {
135             ex.printStackTrace();
136         }
137         return model;
138     }
139         
140     /* Retrieves WSDL model for a WS client - always has a wsdl
141      */

142     public static WSDLModel getModelForClient(Node node, Client client, boolean create, Collection JavaDoc createdFiles) throws IOException JavaDoc {
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             // TODO - display warning dialog
153
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             // check whether config file already exists
167
FileObject configFO = srcFolder.getFileObject(originalWsdlFO.getNameExt());
168             if ((configFO != null) && (configFO.isValid())) {
169                 return getModelFromFO(configFO, true);
170             }
171   
172             if (create) {
173                 // check whether main config file exists
174
FileObject mainConfigFO = srcFolder.getFileObject(CONFIG_WSDL_CLIENT_PREFIX, MAIN_CONFIG_EXTENSION);
175                 if (mainConfigFO == null) {
176                     mainConfigFO = createMainConfig(srcFolder, createdFiles);
177                 }
178
179                 // config file doesn't exist - copy the wsdl and create wsdl model from it
180
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                 // import the model from client model
193
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"); //NOI18N
203
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 JavaDoc createdFiles) throws CatalogModelException {
234         Collection JavaDoc<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 JavaDoc e) {
249                 // ignore - this happens when files are imported recursively
250
}
251         }
252     }
253
254     private static FileObject getFOForModel(WSDLModel model) {
255         ModelSource ms = model.getModelSource();
256         return Utilities.getFileObject(ms);
257     }
258     
259     /** Creates new empty main client configuration file
260      *
261      */

262     private static FileObject createMainConfig(FileObject folder, Collection JavaDoc createdFiles) {
263         FileObject mainConfig = null;
264         try {
265             mainConfig = FileUtil.createData(folder, CONFIG_WSDL_CLIENT_PREFIX + "." + MAIN_CONFIG_EXTENSION); //NOI18N
266
if ((mainConfig != null) && (mainConfig.isValid()) && !(mainConfig.isVirtual())) {
267                 if (createdFiles != null) {
268                     createdFiles.add(mainConfig);
269                 }
270                 FileWriter JavaDoc fw = new FileWriter JavaDoc(FileUtil.toFile(mainConfig));
271                 fw.write(NbBundle.getMessage(WSITEditor.class, "EMPTY_WSDL")); //NOI18N
272
fw.close();
273                 mainConfig.refresh(true);
274             }
275         } catch (IOException JavaDoc 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); //NOI18N
285
if (mainConfig != null) {
286                 model = getModelFromFO(mainConfig, true);
287             }
288         }
289         return model;
290     }
291     
292     private static FileObject getWsdlFO(FileObject folder, String JavaDoc wsdlLocation) {
293         String JavaDoc relativePath = wsdlLocation.substring(wsdlLocation.indexOf("/wsdl/") + 6);
294         return folder.getFileObject(relativePath);
295     }
296
297     /* Retrieves WSDL model for a WS from Java - if config file exists, reuses that one, otherwise generates new one
298      */

299     public static WSDLModel getServiceModelForClient(JAXWSClientSupport supp, Client client) throws IOException JavaDoc, Exception JavaDoc {
300         FileObject originalWsdlFolder = supp.getLocalWsdlFolderForClient(client.getName(), false);
301         FileObject originalWsdlFO = originalWsdlFolder.getFileObject(client.getLocalWsdlFile());
302         
303         if ((originalWsdlFO != null) && (originalWsdlFO.isValid())) { //NOI18N
304
return getModelFromFO(originalWsdlFO, false);
305         }
306         return null;
307     }
308     
309     /* Retrieves WSDL model for a WS from Java - if config file exists, reuses that one, otherwise generates new one
310      */

311     private static WSDLModel getModelForServiceFromWsdl(JaxWsModel jaxWsModel, Service service) throws IOException JavaDoc, Exception JavaDoc {
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 JavaDoc 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 JavaDoc createdFiles) throws IOException JavaDoc, Exception JavaDoc {
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     /* Retrieves WSDL model for a WS from Java - if config file exists, reuses that one, otherwise generates new one
333      */

334     public static WSDLModel getModelForServiceFromJava(FileObject jc, JAXWSSupport supp, boolean create, Collection JavaDoc createdFiles) throws IOException JavaDoc {
335         
336         WSDLModel model = null;
337         String JavaDoc configWsdlName = CONFIG_WSDL_SERVICE_PREFIX;
338                 
339         try {
340             if (jc == null) return null;
341             final java.lang.String JavaDoc[] result = new java.lang.String JavaDoc[1];
342             
343             JavaSource js = JavaSource.forFileObject(jc);
344             js.runUserActionTask(new AbstractTask<CompilationController>() {
345                  public void run(CompilationController controller) throws java.io.IOException JavaDoc {
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 JavaDoc ex) {
355             Exceptions.printStackTrace(ex);
356         }
357         
358         // check whether config file already exists
359
if (supp.getWsdlFolder(false) != null) {
360             FileObject wsdlFO = supp.getWsdlFolder(false).getParent().getFileObject(configWsdlName, CONFIG_WSDL_EXTENSION); //NOI18N
361
if ((wsdlFO != null) && (wsdlFO.isValid())) { //NOI18N
362
return getModelFromFO(wsdlFO, true);
363             }
364         }
365
366         if (create) {
367             // config file doesn't exist - generate empty file
368
FileObject wsdlFolder = supp.getWsdlFolder(true).getParent();
369             FileObject wsdlFO = wsdlFolder.getFileObject(configWsdlName, CONFIG_WSDL_EXTENSION); //NOI18N
370
if ((wsdlFO == null) || !(FileUtil.toFile(wsdlFO).exists())) {
371                 wsdlFO = wsdlFolder.createData(configWsdlName, CONFIG_WSDL_EXTENSION); //NOI18N
372
if (createdFiles != null) {
373                     createdFiles.add(wsdlFO);
374                 }
375                 FileWriter JavaDoc fw = new FileWriter JavaDoc(FileUtil.toFile(wsdlFO));
376                 fw.write(NbBundle.getMessage(WSITEditor.class, "EMPTY_WSDL")); //NOI18N
377
fw.close();
378                 wsdlFO.refresh(true);
379             }
380
381             // and fill it with values
382
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 JavaDoc serviceQName = JavaWsdlMapper.getServiceName(jc);
401                     String JavaDoc serviceLocalName = serviceQName.getLocalPart();
402                     String JavaDoc serviceTargetNamespace = serviceQName.getNamespaceURI();
403
404                     d.setName(serviceLocalName);
405                     d.setTargetNamespace(serviceTargetNamespace);
406                     
407                     // create service element and add it to definitions
408
org.netbeans.modules.xml.wsdl.model.Service s = wcf.createService();
409                     s.setName(serviceLocalName);
410                     d.addService(s);
411                     
412                     // create port and add it to the service element
413
org.netbeans.modules.xml.wsdl.model.Port port = wcf.createPort();
414                     QName JavaDoc portName = JavaWsdlMapper.getPortName(jc, serviceTargetNamespace);
415                     if (portName != null) {
416                         port.setName(portName.getLocalPart());
417                     }
418                     s.addPort(port);
419                     
420                     // create binding and add it to the service element
421
org.netbeans.modules.xml.wsdl.model.Binding binding = wcf.createBinding();
422                     String JavaDoc bindingName = JavaWsdlMapper.getBindingName(jc, serviceTargetNamespace);
423                     binding.setName(bindingName);
424                     d.addBinding(binding);
425                     
426                     // attach binding to the port
427
port.setBinding(binding.createReferenceTo(binding, org.netbeans.modules.xml.wsdl.model.Binding.class));
428
429                     // create portType and add it to the definitions element
430
org.netbeans.modules.xml.wsdl.model.PortType portType = wcf.createPortType();
431                     QName JavaDoc portTypeName = JavaWsdlMapper.getPortTypeName(jc);
432                     portType.setName(portTypeName.getLocalPart());
433                     d.addPortType(portType);
434                     
435                     // create operations and add them to the binding element
436
List JavaDoc<String JavaDoc> bindingOperationNames = JavaWsdlMapper.getOperationNames(jc);
437                     for (String JavaDoc 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"); //NOI18N
449
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 JavaDoc<String JavaDoc> faults = JavaWsdlMapper.getOperationFaults(jc, name);
456                         for (String JavaDoc 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                         //add faults
481
List JavaDoc<String JavaDoc> operationFaults = JavaWsdlMapper.getOperationFaults(jc, name);
482                         for (String JavaDoc 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                     // attach portType to the binding
490
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 JavaDoc 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 JavaDoc());
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) { //it is a service
525
String JavaDoc wsdlUrl = service.getWsdlUrl();
526             if (wsdlUrl != null) { // it is a web service from wsdl
527
return true;
528             }
529         }
530         return false;
531     }
532     
533     /* transaction has to be started before this method is called */
534     private static void removePolicyElements(WSDLComponent c) {
535         List JavaDoc<Policy> policies = c.getExtensibilityElements(Policy.class);
536         for (Policy p : policies) {
537             c.removeExtensibilityElement(p);
538         }
539         List JavaDoc<PolicyReference> policyReferences = c.getExtensibilityElements(PolicyReference.class);
540         for (PolicyReference pr : policyReferences) {
541             c.removeExtensibilityElement(pr);
542         }
543         List JavaDoc<WSDLComponent> children = c.getChildren();
544         for (WSDLComponent ch : children) {
545             removePolicyElements(ch);
546         }
547     }
548     
549     public static void fillImportedBindings(final WSDLModel model, Collection JavaDoc<Binding> bindings, HashSet JavaDoc<FileObject> traversedModels) {
550         FileObject modelFO = getFOForModel(model);
551         // avoid neverending recursion for recursive imports
552
if (traversedModels.contains(modelFO)) {
553             return;
554         }
555         traversedModels.add(modelFO);
556         
557         Collection JavaDoc<Binding> importedBindings = model.getDefinitions().getBindings();
558         bindings.addAll(importedBindings);
559         
560         Collection JavaDoc<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