KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Collection JavaDoc;
23 import javax.xml.namespace.QName JavaDoc;
24 import org.netbeans.modules.websvc.wsitmodelext.policy.All;
25 import org.netbeans.modules.websvc.wsitmodelext.policy.ExactlyOne;
26 import org.netbeans.modules.websvc.wsitmodelext.policy.Policy;
27 import org.netbeans.modules.websvc.wsitmodelext.policy.PolicyQName;
28 import org.netbeans.modules.websvc.wsitmodelext.policy.PolicyReference;
29 import org.netbeans.modules.xml.wsdl.model.*;
30
31 import java.util.List JavaDoc;
32 import org.netbeans.modules.websvc.wsitmodelext.addressing.Addressing10WsdlQName;
33 import org.netbeans.modules.websvc.wsitmodelext.addressing.Addressing10WsdlUsingAddressing;
34 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
35
36 /**
37  *
38  * @author Martin Grebac
39  */

40 public class PolicyModelHelper {
41     
42     /**
43      * Creates top level policy (Policy/ExactlyOne/All) elements if they don't exist. Used for creating nested policies.
44      *
45      * @param p - policy element, under which ExactlyOne/All gets created
46      * @return the bottom-most All element
47      */

48     public static All createTopExactlyOneAll(final Policy p) {
49         WSDLModel model = p.getModel();
50         ExactlyOne eo = createElement(p, PolicyQName.EXACTLYONE.getQName(), ExactlyOne.class, false);
51         All all = createElement(eo, PolicyQName.ALL.getQName(), All.class, false);
52         return all;
53     }
54         
55     /** Returns existing, or creates a new policy (Policy/ExactlyOne/All) and a PolicyReference
56      * attached to a Binding/BindingOperation/BindingOutput/Input/Fault element
57      * should be used in order to create a policy, or to access the All element
58      * component c must not be null
59      */

60     public static All createPolicy(final WSDLComponent c) {
61
62         WSDLModel model = c.getModel();
63         WSDLComponentFactory wcf = model.getFactory();
64         Definitions d = model.getDefinitions();
65         String JavaDoc policyName = null;
66         String JavaDoc msgName = null;
67         Policy policy = null;
68         
69         policy = getPolicyForElement(c);
70         if (policy == null) {
71             policyName = getPolicyUriForElement(c);
72             if (policyName == null) {
73                 if (c instanceof Binding) {
74                     policyName = ((Binding)c).getName() + "Policy"; //NOI18N
75
}
76                 if (c instanceof BindingInput) {
77                     msgName = ((BindingInput)c).getName();
78                     if (msgName == null) {
79                         msgName = ((BindingOperation)c.getParent()).getName() + "_Input"; //NOI18N
80
}
81                     Binding b = (Binding)c.getParent().getParent();
82                     policyName = b.getName() + "_" + msgName + "_" + "Policy"; //NOI18N
83
}
84                 if (c instanceof BindingOutput) {
85                     msgName = ((BindingOutput)c).getName();
86                     if (msgName == null) {
87                         msgName = ((BindingOperation)c.getParent()).getName() + "_Output"; //NOI18N
88
}
89                     Binding b = (Binding)c.getParent().getParent();
90                     policyName = b.getName() + "_" + msgName + "_" + "Policy"; //NOI18N
91
}
92                 if (c instanceof BindingFault) {
93                     msgName = ((BindingFault)c).getName();
94                     if (msgName == null) {
95                         msgName = ((BindingOperation)c.getParent()).getName() + "_Fault"; //NOI18N
96
}
97                     Binding b = (Binding)c.getParent().getParent();
98                     policyName = b.getName() + "_" + msgName + "_" + "Policy"; //NOI18N
99
}
100                 if (c instanceof BindingOperation) {
101                     msgName = ((BindingOperation)c).getName();
102                     if (msgName == null) {
103                         msgName = ((BindingOperation)c.getParent()).getName(); //NOI18N
104
}
105                     Binding b = (Binding)c.getParent();
106                     policyName = b.getName() + "_" + msgName + "_" + "Policy"; //NOI18N
107
}
108             }
109
110             boolean isTransaction = model.isIntransaction();
111             if (!isTransaction) {
112                 model.startTransaction();
113             }
114             
115             try {
116                 List JavaDoc<Policy> policies = d.getExtensibilityElements(Policy.class);
117                 for (Policy p : policies) {
118                     if (policyName.equals(p.getID())) {
119                         List JavaDoc<PolicyReference> policyRefs = c.getExtensibilityElements(PolicyReference.class);
120                         PolicyReference policyRef;
121                         if ((policyRefs == null) || (policyRefs.isEmpty())) {
122                             policyRef = (PolicyReference)model.getFactory().create(c, PolicyQName.POLICYREFERENCE.getQName());
123                         } else {
124                             policyRef = policyRefs.get(0);
125                         }
126                         policyRef.setPolicyURI("#" + policyName); //NOI18N
127
c.addExtensibilityElement(policyRef);
128                         return createTopExactlyOneAll(p);
129                     }
130                 }
131                 policy = (Policy)wcf.create(d, PolicyQName.POLICY.getQName());
132                 policy.setID(policyName);
133                 PolicyReference policyRef = (PolicyReference)model.getFactory().create(c, PolicyQName.POLICYREFERENCE.getQName());
134                 policyRef.setPolicyURI("#" + policyName); //NOI18N
135
c.addExtensibilityElement(policyRef);
136                 d.addExtensibilityElement(policy);
137             } finally {
138                 if (!isTransaction) {
139                     model.endTransaction();
140                 }
141             }
142         }
143                         
144         All all = createTopExactlyOneAll(policy);
145         if (c instanceof Binding) {
146             PolicyModelHelper.createElement(all, Addressing10WsdlQName.USINGADDRESSING.getQName(), Addressing10WsdlUsingAddressing.class, false);
147         }
148         return all;
149     }
150     
151     /* Used to get specific domain elements under top of the policy - under POLICY/ExactlyOne/All/*SPECIFICELEMENT*
152      * Does not create any elements
153      */

154     public static <T extends ExtensibilityElement> T getTopLevelElement(WSDLComponent c, Class JavaDoc elementClass) {
155         ExtensibilityElement e = null;
156         if (c == null) return null;
157         if (c instanceof Policy) {
158             ExactlyOne eo = ((Policy)c).getExactlyOne();
159             if (eo != null) {
160                 All all = eo.getAll();
161                 e = getTopLevelElement(all, elementClass);
162             } else {
163                 List JavaDoc<ExtensibilityElement> l = c.getExtensibilityElements(elementClass);
164                 if ((l != null) && !(l.isEmpty())) {
165                     e = l.get(0);
166                 }
167             }
168         } else {
169             List JavaDoc<ExtensibilityElement> l = c.getExtensibilityElements(elementClass);
170             if ((l != null) && !(l.isEmpty())) {
171                 e = l.get(0);
172             }
173         }
174         return (T)e;
175     }
176
177     /* Returns name of policy attached to a wsdl component */
178     private static String JavaDoc getPolicyUriForElement(WSDLComponent c) {
179         List JavaDoc<PolicyReference> extPRefElems = c.getExtensibilityElements(PolicyReference.class);
180         if ((extPRefElems != null) && (extPRefElems.size() > 0)) {
181             PolicyReference pref = extPRefElems.get(0);
182             String JavaDoc policyURI = pref.getPolicyURI();
183             return policyURI;
184         }
185         return null;
186     }
187
188     /* Returns policy with specific uri */
189     private static Policy getPolicyForPolicyUri(String JavaDoc policyURI, Definitions d) {
190         String JavaDoc uri = policyURI;
191         if ((policyURI != null) && (policyURI.startsWith("#"))) { //NOI18N
192
policyURI = policyURI.substring(1);
193         }
194         List JavaDoc<Policy> extPElems = d.getExtensibilityElements(Policy.class);
195         for (Policy p : extPElems) {
196             String JavaDoc id = p.getID();
197             if (policyURI.equals(id)) {
198                 return p;
199             }
200         }
201         return null;
202     }
203
204     /* Returns policy attached to a wsdl component */
205     public static Policy getPolicyForElement(WSDLComponent c) {
206         if (c == null) return null;
207         WSDLModel model = c.getModel();
208         if (model != null) {
209             String JavaDoc policyUri = getPolicyUriForElement(c);
210             Definitions d = model.getDefinitions();
211             if ((d != null) && (policyUri != null)) {
212                 Policy p = getPolicyForPolicyUri(policyUri, d);
213                 return p;
214             }
215         }
216         return null;
217     }
218     
219     public static void removePolicyForElement(WSDLComponent c) {
220         WSDLModel model = c.getModel();
221         if (model != null) {
222             String JavaDoc policyUri = getPolicyUriForElement(c);
223             Definitions d = model.getDefinitions();
224             
225             boolean isTransaction = model.isIntransaction();
226             if (!isTransaction) {
227                 model.startTransaction();
228             }
229             
230             try {
231                 if ((d != null) && (policyUri != null)) {
232                     Policy p = getPolicyForPolicyUri(policyUri, d);
233                     if (p != null) {
234                         p.getParent().removeExtensibilityElement(p);
235                     }
236                     List JavaDoc<PolicyReference> extPRefElems = c.getExtensibilityElements(PolicyReference.class);
237                     if ((extPRefElems != null) && (extPRefElems.size() > 0)) {
238                         PolicyReference pref = extPRefElems.get(0);
239                         pref.getParent().removeExtensibilityElement(pref);
240                     }
241                 }
242             } finally {
243                 if (!isTransaction) {
244                     model.endTransaction();
245                 }
246             }
247         }
248     }
249     
250     public static Binding getBinding(WSDLModel model, String JavaDoc bindingName) {
251         Binding b = model.findComponentByName(bindingName, Binding.class);
252         if (b == null) {
253             Collection JavaDoc<Import> imports = model.getDefinitions().getImports();
254             for (Import i : imports) {
255                 WSDLModel importedModel;
256                 try {
257                     importedModel = i.getImportedWSDLModel();
258                     return getBinding(importedModel, bindingName);
259                 } catch (CatalogModelException ex) {
260                     ex.printStackTrace();
261                 }
262             }
263         }
264         return b;
265     }
266
267     /**
268      * Creates element with QName qname, of type cl, under wsdlcomponent c and returns it; if such element already exists,
269      * returns the existing element
270      */

271     public static <T extends WSDLComponent> T createElement(WSDLComponent c, QName JavaDoc qname, Class JavaDoc cl, boolean withPolicy) {
272         if (c == null) return null;
273         
274         WSDLModel model = c.getModel();
275         WSDLComponentFactory wcf = model.getFactory();
276         boolean isTransaction = model.isIntransaction();
277         if (!isTransaction) {
278             model.startTransaction();
279         }
280         try {
281             if (withPolicy) {
282                 c = createElement(c, PolicyQName.POLICY.getQName(), Policy.class, false);
283             }
284             List JavaDoc<T> ts = c.getExtensibilityElements(cl);
285             T t = null;
286             if ((ts == null) || (ts.isEmpty())) {
287                 t = (T) wcf.create(c, qname);
288                 c.addExtensibilityElement((ExtensibilityElement) t);
289             } else {
290                 t = ts.get(0);
291             }
292             return t;
293         } finally {
294             if (!isTransaction) {
295                 model.endTransaction();
296             }
297         }
298     }
299     
300     /** Removes first element of class cl from under component c
301      */

302     public static void removeElement(WSDLComponent c, Class JavaDoc cl, boolean underPolicy) {
303         if (c == null) return;
304
305         WSDLModel model = c.getModel();
306
307         boolean isTransaction = model.isIntransaction();
308         if (!isTransaction) {
309             model.startTransaction();
310         }
311         try {
312             if (underPolicy) {
313                 List JavaDoc<Policy> policies = c.getExtensibilityElements(Policy.class);
314                 if ((policies != null) && (!policies.isEmpty())) {
315                     c = policies.get(0);
316                 }
317             }
318             List JavaDoc l = c.getExtensibilityElements(cl);
319             if ((l != null) && (!l.isEmpty())) {
320                 ExtensibilityElement tok = (ExtensibilityElement)l.get(0);
321                 tok.getParent().removeExtensibilityElement(tok);
322             }
323         } finally {
324             if (!isTransaction) {
325                 model.endTransaction();
326             }
327         }
328     }
329
330     public static void removeElement(WSDLComponent c) {
331         if (c == null) return;
332         WSDLModel model = c.getModel();
333         boolean isTransaction = model.isIntransaction();
334         if (!isTransaction) {
335             model.startTransaction();
336         }
337         try {
338             c.getParent().removeExtensibilityElement((ExtensibilityElement) c);
339         } finally {
340             if (!isTransaction) {
341                 model.endTransaction();
342             }
343         }
344     }
345     
346     public static void cleanPolicies(WSDLComponent c) {
347         Policy p = PolicyModelHelper.getPolicyForElement(c);
348         if ((p != null) && (isEmpty(p))) {
349             removePolicyForElement(c);
350         }
351     }
352     
353     /**
354      * policy is empty if it contains only policy/all/exactlyone elements
355      * comp must be non-null
356      */

357     private static boolean isEmpty(WSDLComponent comp) {
358         List JavaDoc<WSDLComponent> children = comp.getChildren();
359         for (WSDLComponent c : children) {
360             if ((c instanceof Policy) ||
361                 (c instanceof All) || (c instanceof ExactlyOne)) {
362                 return isEmpty(c);
363             }
364             if (!(c instanceof Addressing10WsdlUsingAddressing)) {
365                 return false;
366             }
367         }
368         return true;
369     }
370     
371 }
372
Popular Tags