1 24 25 package com.bull.eclipse.jonas.utils.xml; 26 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.util.jar.JarFile ; 30 31 import javax.xml.parsers.DocumentBuilder ; 32 import javax.xml.parsers.DocumentBuilderFactory ; 33 import javax.xml.parsers.ParserConfigurationException ; 34 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.Element ; 37 import org.w3c.dom.NodeList ; 38 import org.w3c.dom.Text ; 39 import org.xml.sax.SAXException ; 40 41 import com.bull.eclipse.jonas.utils.xml.desc.AppClientDTDs; 42 import com.bull.eclipse.jonas.utils.xml.desc.AppClientSchemas; 43 import com.bull.eclipse.jonas.utils.xml.desc.EarDTDs; 44 import com.bull.eclipse.jonas.utils.xml.desc.EarSchemas; 45 import com.bull.eclipse.jonas.utils.xml.desc.EjbjarDTDs; 46 import com.bull.eclipse.jonas.utils.xml.desc.EjbjarSchemas; 47 import com.bull.eclipse.jonas.utils.xml.desc.HibernateDTDs; 48 import com.bull.eclipse.jonas.utils.xml.desc.JonasAppClientDTDs; 49 import com.bull.eclipse.jonas.utils.xml.desc.JonasAppClientSchemas; 50 import com.bull.eclipse.jonas.utils.xml.desc.JonasEjbjarDTDs; 51 import com.bull.eclipse.jonas.utils.xml.desc.JonasEjbjarSchemas; 52 import com.bull.eclipse.jonas.utils.xml.desc.JonasWebAppDTDs; 53 import com.bull.eclipse.jonas.utils.xml.desc.JonasWebAppSchemas; 54 import com.bull.eclipse.jonas.utils.xml.desc.WebAppDTDs; 55 import com.bull.eclipse.jonas.utils.xml.desc.WebAppSchemas; 56 57 58 63 public class XMLUtils { 64 65 private static DocumentBuilderFactory factory = null; 66 67 70 private static final String J2EE_NS = "http://java.sun.com/xml/ns/j2ee"; 71 72 75 private static final String JONAS_NS = "http://www.objectweb.org/jonas/ns"; 76 77 78 private static final String EJB_JAR = "ejb-jar"; 79 private static final String STANDARD_SESSION_BEAN = "session"; 80 private static final String STANDARD_ENTITY_BEAN = "entity"; 81 private static final String STANDARD_MESSAGE_BEAN = "message-driven"; 82 private static final String STANDARD_EJB_NAME = "ejb-name"; 83 private static final String PK_TYPE = "prim-key-class"; 84 85 86 private static final String JONAS_EJB_JAR = "jonas-ejb-jar"; 87 private static final String SESSION_BEAN = "jonas-session"; 88 private static final String ENTITY_BEAN = "jonas-entity"; 89 private static final String MESSAGE_BEAN = "jonas-message-driven"; 90 private static final String EJB_NAME = "ejb-name"; 91 private static final String SERVLET = "servlet"; 92 private static final String SERVLET_NAME = "servlet-name"; 93 private static final String JONAS_SERVICE_REF = "jonas-service-ref"; 94 private static final String SR_NAME = "service-ref-name"; 95 96 103 private static DocumentBuilderFactory newFactory(boolean validating) { 104 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 105 factory.setNamespaceAware(true); 106 factory.setValidating(validating); 107 factory.setIgnoringElementContentWhitespace(true); 108 return factory; 111 } 112 113 124 public static Document newDocument(InputStream is, boolean validate) 125 throws ParserConfigurationException , SAXException , IOException { 126 factory = newFactory(validate); 128 130 DocumentBuilder builder = factory.newDocumentBuilder(); 131 132 if (factory.isValidating()) { 134 JEntityResolver jer = new JEntityResolver(); 135 jer.addSchemas(new WebAppSchemas()); 136 jer.addSchemas(new JonasWebAppSchemas()); 137 jer.addSchemas(new JonasEjbjarSchemas()); 138 jer.addSchemas(new EjbjarSchemas()); 139 jer.addSchemas(new JonasAppClientSchemas()); 140 jer.addSchemas(new AppClientSchemas()); 141 jer.addSchemas(new EarSchemas()); 142 143 144 jer.addDtds(new WebAppDTDs()); 145 jer.addDtds(new JonasWebAppDTDs()); 146 jer.addDtds(new EjbjarDTDs()); 147 jer.addDtds(new JonasEjbjarDTDs()); 148 jer.addDtds(new AppClientDTDs()); 149 jer.addDtds(new JonasAppClientDTDs()); 150 jer.addDtds(new EarDTDs()); 151 jer.addDtds(new HibernateDTDs()); 152 153 builder.setEntityResolver(jer); 154 } 155 156 return builder.parse(is); 157 } 158 159 171 public static Element getBeanElementInJOnASDesc(Element base, String bName) { 172 Element found = null; 173 174 176 NodeList sessions = base.getElementsByTagName(SESSION_BEAN); 179 for(int i = 0; (i < sessions.getLength()) && (found == null); i++) { 180 Element session = (Element ) sessions.item(i); 181 NodeList names = session.getElementsByTagName(EJB_NAME); 184 Element name = (Element ) names.item(0); 186 187 if(name.getFirstChild() 188 .getNodeValue() 189 .equals(bName)) { 190 found = session; 191 } 192 } 193 NodeList entities = base.getElementsByTagName(STANDARD_ENTITY_BEAN); 197 for(int i = 0; (i < entities.getLength()) && (found == null); i++) { 198 Element entity = (Element ) entities.item(i); 199 NodeList names = entity.getElementsByTagName(STANDARD_EJB_NAME); 202 Element name = (Element ) names.item(0); 204 205 if(name.getFirstChild() 206 .getNodeValue() 207 .equals(bName)) { 208 found = entity; 209 } 210 } 211 212 NodeList messages = base.getElementsByTagName(STANDARD_MESSAGE_BEAN); 216 for(int i = 0; (i < messages.getLength()) && (found == null); i++) { 217 Element message = (Element ) messages.item(i); 218 NodeList names = message.getElementsByTagName(STANDARD_EJB_NAME); 221 Element name = (Element ) names.item(0); 223 224 if(name.getFirstChild() 225 .getNodeValue() 226 .equals(bName)) { 227 found = message; 228 } 229 } 230 231 return found; 232 } 233 234 246 public static Element getBeanElementInStandardDesc(Element base, String bName) { 247 Element found = null; 248 249 251 NodeList sessions = base.getElementsByTagName(STANDARD_SESSION_BEAN); 254 for(int i = 0; (i < sessions.getLength()) && (found == null); i++) { 255 Element session = (Element ) sessions.item(i); 256 NodeList names = session.getElementsByTagName(STANDARD_EJB_NAME); 259 Element name = (Element ) names.item(0); 261 262 if(name.getFirstChild() 263 .getNodeValue() 264 .equals(bName)) { 265 found = session; 266 } 267 } 268 269 NodeList entities = base.getElementsByTagName(STANDARD_ENTITY_BEAN); 273 for(int i = 0; (i < entities.getLength()) && (found == null); i++) { 274 Element entity = (Element ) entities.item(i); 275 NodeList names = entity.getElementsByTagName(STANDARD_EJB_NAME); 278 Element name = (Element ) names.item(0); 280 281 if(name.getFirstChild() 282 .getNodeValue() 283 .equals(bName)) { 284 found = entity; 285 } 286 } 287 288 NodeList messages = base.getElementsByTagNameNS(J2EE_NS, STANDARD_MESSAGE_BEAN); 290 291 for(int i = 0; (i < messages.getLength()) && (found == null); i++) { 292 Element message = (Element ) messages.item(i); 293 NodeList names = message.getElementsByTagNameNS(J2EE_NS, STANDARD_EJB_NAME); 294 295 Element name = (Element ) names.item(0); 297 298 if(name.getFirstChild() 299 .getNodeValue() 300 .equals(bName)) { 301 found = message; 302 } 303 } 304 305 return found; 306 } 307 308 316 public static Element getServletElement(Element base, String sName) { 317 Element found = null; 318 319 322 NodeList servlets = base.getElementsByTagName(SERVLET); 324 for(int i = 0; (i < servlets.getLength()) && (found == null); i++) { 325 Element servlet = (Element ) servlets.item(i); 326 NodeList names = 327 servlet.getElementsByTagName(SERVLET_NAME); 329 Element name = (Element ) names.item(0); 331 332 if(name.getFirstChild() 333 .getNodeValue() 334 .equals(sName)) { 335 found = servlet; 336 } 337 } 338 339 return found; 340 } 341 342 351 public static Element getJonasServiceRef(Element base, String srName) { 352 Element found = null; 353 354 NodeList jsrs = 356 base.getElementsByTagNameNS(JONAS_NS, JONAS_SERVICE_REF); 357 358 for(int i = 0; (i < jsrs.getLength()) && (found == null); i++) { 359 Element jsr = (Element ) jsrs.item(i); 360 NodeList names = jsr.getElementsByTagNameNS(JONAS_NS, SR_NAME); 361 362 Element name = (Element ) names.item(0); 364 365 if(name.getFirstChild() 366 .getNodeValue() 367 .equals(srName)) { 368 found = jsr; 369 } 370 } 371 372 return found; 373 } 374 375 381 public static void cleanDummyApplication(Document doc) { 382 Element root = doc.getDocumentElement(); 383 NodeList nl = root.getElementsByTagNameNS(J2EE_NS, "module"); 384 root.removeChild(nl.item(0)); 385 } 386 387 393 public static void addEjb(Document app, JarFile ejbjar) { 394 Element module = app.createElementNS(J2EE_NS, "module"); 395 Element ejb = app.createElementNS(J2EE_NS, "ejb"); 396 Text ejbText = app.createTextNode(ejbjar.getName()); 397 ejb.appendChild(ejbText); 398 module.appendChild(ejb); 399 400 insertModule(app, module); 401 } 402 403 409 public static void addClient(Document app, JarFile client) { 410 Element module = app.createElementNS(J2EE_NS, "module"); 411 Element clt = app.createElementNS(J2EE_NS, "java"); 412 Text cltText = app.createTextNode(client.getName()); 413 clt.appendChild(cltText); 414 module.appendChild(clt); 415 416 insertModule(app, module); 417 } 418 419 426 public static void addWebApp(Document app, JarFile webapp, String ctx) { 427 Element module = app.createElementNS(J2EE_NS, "module"); 428 Element web = app.createElementNS(J2EE_NS, "web"); 429 Element webUri = app.createElementNS(J2EE_NS, "web-uri"); 430 Element context = app.createElementNS(J2EE_NS, "context-root"); 431 432 Text webText = app.createTextNode(webapp.getName()); 433 Text ctxText = app.createTextNode(ctx); 434 webUri.appendChild(webText); 435 context.appendChild(ctxText); 436 437 web.appendChild(webUri); 438 web.appendChild(context); 439 440 module.appendChild(web); 441 442 insertModule(app, module); 443 } 444 445 451 private static void insertModule(Document app, Element module) { 452 Element application = app.getDocumentElement(); 453 Element first = findFirstSecurityRole(application); 454 application.insertBefore(module, first); 455 } 456 457 464 private static Element findFirstSecurityRole(Element app) { 465 NodeList nl = app.getElementsByTagNameNS(J2EE_NS, "security-role"); 466 467 if(nl.getLength() == 0) { 468 return null; 469 } else { 470 return (Element ) nl.item(0); 471 } 472 } 473 474 } 475 | Popular Tags |