1 19 20 package org.netbeans.modules.websvc.wsitconf.wsdlmodelext; 21 22 import java.util.Collection ; 23 import javax.xml.namespace.QName ; 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 ; 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 40 public class PolicyModelHelper { 41 42 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 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 policyName = null; 66 String 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"; } 76 if (c instanceof BindingInput) { 77 msgName = ((BindingInput)c).getName(); 78 if (msgName == null) { 79 msgName = ((BindingOperation)c.getParent()).getName() + "_Input"; } 81 Binding b = (Binding)c.getParent().getParent(); 82 policyName = b.getName() + "_" + msgName + "_" + "Policy"; } 84 if (c instanceof BindingOutput) { 85 msgName = ((BindingOutput)c).getName(); 86 if (msgName == null) { 87 msgName = ((BindingOperation)c.getParent()).getName() + "_Output"; } 89 Binding b = (Binding)c.getParent().getParent(); 90 policyName = b.getName() + "_" + msgName + "_" + "Policy"; } 92 if (c instanceof BindingFault) { 93 msgName = ((BindingFault)c).getName(); 94 if (msgName == null) { 95 msgName = ((BindingOperation)c.getParent()).getName() + "_Fault"; } 97 Binding b = (Binding)c.getParent().getParent(); 98 policyName = b.getName() + "_" + msgName + "_" + "Policy"; } 100 if (c instanceof BindingOperation) { 101 msgName = ((BindingOperation)c).getName(); 102 if (msgName == null) { 103 msgName = ((BindingOperation)c.getParent()).getName(); } 105 Binding b = (Binding)c.getParent(); 106 policyName = b.getName() + "_" + msgName + "_" + "Policy"; } 108 } 109 110 boolean isTransaction = model.isIntransaction(); 111 if (!isTransaction) { 112 model.startTransaction(); 113 } 114 115 try { 116 List <Policy> policies = d.getExtensibilityElements(Policy.class); 117 for (Policy p : policies) { 118 if (policyName.equals(p.getID())) { 119 List <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); 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); 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 154 public static <T extends ExtensibilityElement> T getTopLevelElement(WSDLComponent c, Class 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 <ExtensibilityElement> l = c.getExtensibilityElements(elementClass); 164 if ((l != null) && !(l.isEmpty())) { 165 e = l.get(0); 166 } 167 } 168 } else { 169 List <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 178 private static String getPolicyUriForElement(WSDLComponent c) { 179 List <PolicyReference> extPRefElems = c.getExtensibilityElements(PolicyReference.class); 180 if ((extPRefElems != null) && (extPRefElems.size() > 0)) { 181 PolicyReference pref = extPRefElems.get(0); 182 String policyURI = pref.getPolicyURI(); 183 return policyURI; 184 } 185 return null; 186 } 187 188 189 private static Policy getPolicyForPolicyUri(String policyURI, Definitions d) { 190 String uri = policyURI; 191 if ((policyURI != null) && (policyURI.startsWith("#"))) { policyURI = policyURI.substring(1); 193 } 194 List <Policy> extPElems = d.getExtensibilityElements(Policy.class); 195 for (Policy p : extPElems) { 196 String id = p.getID(); 197 if (policyURI.equals(id)) { 198 return p; 199 } 200 } 201 return null; 202 } 203 204 205 public static Policy getPolicyForElement(WSDLComponent c) { 206 if (c == null) return null; 207 WSDLModel model = c.getModel(); 208 if (model != null) { 209 String 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 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 <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 bindingName) { 251 Binding b = model.findComponentByName(bindingName, Binding.class); 252 if (b == null) { 253 Collection <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 271 public static <T extends WSDLComponent> T createElement(WSDLComponent c, QName qname, Class 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 <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 302 public static void removeElement(WSDLComponent c, Class 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 <Policy> policies = c.getExtensibilityElements(Policy.class); 314 if ((policies != null) && (!policies.isEmpty())) { 315 c = policies.get(0); 316 } 317 } 318 List 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 357 private static boolean isEmpty(WSDLComponent comp) { 358 List <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 |