1 24 25 package org.objectweb.jonas_ws.wsgen.ddmodifier; 26 27 import org.objectweb.util.monolog.api.BasicLevel; 28 import org.w3c.dom.Document ; 29 import org.w3c.dom.Element ; 30 import org.w3c.dom.Node ; 31 import org.w3c.dom.NodeList ; 32 import org.w3c.dom.Text ; 33 34 38 public class WsEndpointDDModifier extends DeploymentDescModifier { 39 40 43 private static final String SERVLET = "servlet"; 44 45 48 private static final String SERVLET_NAME = "servlet-name"; 49 50 53 private static final String SERVLET_CLASS = "servlet-class"; 54 55 58 private static final String SERVLET_MAPPING = "servlet-mapping"; 59 60 63 private static final String URL_MAPPING = "url-pattern"; 64 65 68 private static final String INIT_PARAM = "init-param"; 69 70 73 private static final String PARAM_NAME = "param-name"; 74 75 78 private static final String PARAM_VALUE = "param-value"; 79 80 83 private static final String SECURITY_ROLE_REF = "security-role-ref"; 84 85 88 private static final String URL_PATTERN = "url-pattern"; 89 90 93 private static final String SECURITY_CONSTRAINT = "security-constraint"; 94 95 98 private static final String LOGIN_CONFIG = "login-config"; 99 100 103 private static final String SECURITY_ROLE = "security-role"; 104 105 109 public WsEndpointDDModifier(Document web) { 110 super(web.getDocumentElement(), web); 111 } 112 113 118 public void addServlet(String name, String classname) { 119 Element servlet = newJ2EEElement(SERVLET); 120 Element servletName = newJ2EEElement(SERVLET_NAME, name); 121 Element servletClass = newJ2EEElement(SERVLET_CLASS, classname); 122 123 servlet.appendChild(servletName); 124 servlet.appendChild(servletClass); 125 126 getElement().appendChild(servlet); 128 129 } 130 131 136 public void addEndpointSecurityConstraint(Node securityConstraint) { 137 Element sConstraintElement = newJ2EEElement (SECURITY_CONSTRAINT); 138 139 Node newSecurityConstraint = getDocument().importNode(securityConstraint, true); 141 142 while (newSecurityConstraint.hasChildNodes()) { 143 sConstraintElement.appendChild(newSecurityConstraint.getFirstChild()); 144 } 145 146 getElement().appendChild(sConstraintElement); 148 } 149 150 155 public void addEndpointLoginConfig (Node loginConfig) { 156 Element lConfigElement = newJ2EEElement (LOGIN_CONFIG); 157 158 Node newLoginConfig = getDocument().importNode(loginConfig, true); 160 161 while (newLoginConfig.hasChildNodes()) { 162 lConfigElement.appendChild(newLoginConfig.getFirstChild()); 163 } 164 165 getElement().appendChild(lConfigElement); 167 } 168 169 173 public void addSecurityRole (Node securityRole) { 174 Element sRole = newJ2EEElement (SECURITY_ROLE); 175 176 Node newSecurityRole = getDocument().importNode(securityRole, true); 178 179 while (newSecurityRole.hasChildNodes()) { 180 sRole.appendChild(newSecurityRole.getFirstChild()); 181 } 182 183 getElement().appendChild(sRole); 185 } 186 187 191 public void removeServlet(String name) { 192 Element servlet = findServlet(name); 193 194 if (servlet != null) { 195 getElement().removeChild(servlet); 196 } 197 } 198 199 204 public NodeList removeServletWithSecurity(String name) { 205 Element servlet = findServlet(name); 206 NodeList elements = null; 207 208 if (servlet != null) { 209 210 elements = servlet.getElementsByTagNameNS(J2EE_NS, SECURITY_ROLE_REF); 211 if (elements.getLength() == 0) { 213 elements = null; 214 } 215 getElement().removeChild(servlet); 216 } 217 return elements; 218 } 219 220 225 public void addServletMapping(String name, String mapping) { 226 Element servletMapping = newJ2EEElement(SERVLET_MAPPING); 227 Element servletName = newJ2EEElement(SERVLET_NAME, name); 228 Element urlMapping = newJ2EEElement(URL_MAPPING, mapping); 229 230 servletMapping.appendChild(servletName); 231 servletMapping.appendChild(urlMapping); 232 233 getElement().appendChild(servletMapping); 235 } 236 237 243 public void addServletParam(String servletName, String pName, String pValue) { 244 Element ip = newJ2EEElement(INIT_PARAM); 245 Element pn = newJ2EEElement(PARAM_NAME, pName); 246 Element pv = newJ2EEElement(PARAM_VALUE, pValue); 247 248 ip.appendChild(pn); 249 ip.appendChild(pv); 250 251 Element servlet = findServlet(servletName); 252 servlet.appendChild(ip); 253 254 } 255 256 262 public void addServletSecurityRoleRefs(String servletName, NodeList securityRoleRefs) { 263 Element servlet = findServlet(servletName); 265 for (int i = 0; i < securityRoleRefs.getLength(); i++) { 267 Node securityRoleRefItem = securityRoleRefs.item(i); 268 servlet.appendChild(securityRoleRefItem); 269 } 270 } 271 272 278 private Element findServlet(String name) { 279 NodeList nl = getElement().getElementsByTagNameNS(J2EE_NS, SERVLET); 280 Element servlet = null; 281 282 for (int i = 0; (i < nl.getLength()) && (servlet == null); i++) { 283 Element e = (Element ) nl.item(i); 284 285 NodeList names = e.getElementsByTagNameNS(J2EE_NS, SERVLET_NAME); 286 287 if (names.item(0).getFirstChild().getNodeValue().equals(name)) { 289 servlet = e; 290 } 291 } 292 293 return servlet; 294 } 295 296 301 public String removeServletMapping(String sName) { 302 NodeList nl = getElement().getElementsByTagNameNS(J2EE_NS, SERVLET_MAPPING); 303 Element mapping = null; 304 String urlPatternValue = null; 305 306 for (int i = 0; (i < nl.getLength()) && (mapping == null); i++) { 307 Element e = (Element ) nl.item(i); 308 309 NodeList names = e.getElementsByTagNameNS(J2EE_NS, SERVLET_NAME); 310 311 if (names.item(0).getFirstChild().getNodeValue().equals(sName)) { 313 mapping = e; 314 } 315 } 316 317 if (mapping != null) { 318 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 319 getLogger().log(BasicLevel.DEBUG, "mapping element found : " + mapping); 320 } 321 NodeList urlPatterns = mapping.getElementsByTagNameNS(J2EE_NS, URL_PATTERN); 323 urlPatternValue = urlPatterns.item(0).getFirstChild().getNodeValue(); 324 325 getElement().removeChild(mapping); 326 } 327 328 return urlPatternValue; 329 } 330 331 337 public void updateSecurityConstraint(String oldUrlPatter, String newUrlPatterValue) { 338 339 NodeList nl = getElement().getElementsByTagNameNS(J2EE_NS, SECURITY_CONSTRAINT); 340 341 for (int i = 0; i < nl.getLength(); i++) { 343 Element e = (Element ) nl.item(i); 344 345 NodeList urlPatternCollection = e.getElementsByTagNameNS(J2EE_NS, URL_PATTERN); 347 for (int j = 0; j < urlPatternCollection.getLength(); j++) { 348 349 Element urlPatternElement = (Element ) urlPatternCollection.item(j); 350 Text urlPatternText = (Text ) urlPatternElement.getFirstChild(); 351 352 if (urlPatternText.getNodeValue().equals(oldUrlPatter)) { 354 urlPatternText.setNodeValue(newUrlPatterValue); 355 } 356 } 357 } 358 } 359 360 367 public void addEjbRef(String name, String home, String remote, String link) { 368 369 Element ejbRef = newJ2EEElement("ejb-ref"); 370 Element ejbRefName = newJ2EEElement("ejb-ref-name", name); 371 Element ejbRefType = newJ2EEElement("ejb-ref-type", "Session"); 372 Element ejbHome = newJ2EEElement("home", home); 373 Element ejbRemote = newJ2EEElement("remote", remote); 374 Element ejbLink = newJ2EEElement("ejb-link", link); 375 376 ejbRef.appendChild(ejbRefName); 377 ejbRef.appendChild(ejbRefType); 378 ejbRef.appendChild(ejbHome); 379 ejbRef.appendChild(ejbRemote); 380 ejbRef.appendChild(ejbLink); 381 382 getElement().appendChild(ejbRef); 383 384 } 385 386 393 public void addEjbLocalRef(String name, String home, String remote, String link) { 394 395 Element ejbRef = newJ2EEElement("ejb-local-ref"); 396 Element ejbRefName = newJ2EEElement("ejb-ref-name", name); 397 Element ejbRefType = newJ2EEElement("ejb-ref-type", "Session"); 398 Element ejbHome = newJ2EEElement("local-home", home); 399 Element ejbRemote = newJ2EEElement("local", remote); 400 Element ejbLink = newJ2EEElement("ejb-link", link); 401 402 ejbRef.appendChild(ejbRefName); 403 ejbRef.appendChild(ejbRefType); 404 ejbRef.appendChild(ejbHome); 405 ejbRef.appendChild(ejbRemote); 406 ejbRef.appendChild(ejbLink); 407 408 getElement().appendChild(ejbRef); 409 410 } 411 } 412 | Popular Tags |