1 23 24 package com.sun.enterprise.admin.server.core.mbean.config; 25 26 import java.net.URL ; 27 28 import javax.xml.parsers.DocumentBuilder ; 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 import javax.xml.parsers.FactoryConfigurationError ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 33 import org.xml.sax.SAXException ; 34 import org.xml.sax.SAXParseException ; 35 import org.xml.sax.InputSource ; 36 37 import java.io.File ; 38 import java.io.IOException ; 39 40 import org.w3c.dom.Document ; 41 import org.w3c.dom.DOMException ; 42 import org.w3c.dom.Node ; 43 import org.w3c.dom.NodeList ; 44 import org.w3c.dom.NamedNodeMap ; 45 46 import java.util.Vector ; 47 import java.util.Properties ; 48 import java.util.Iterator ; 49 import java.util.Enumeration ; 50 51 import com.sun.enterprise.config.serverbeans.ElementProperty; 52 import com.sun.enterprise.admin.common.constant.AdminConstants; 53 54 import java.util.logging.Level ; 55 import java.util.logging.Logger ; 56 57 import com.sun.enterprise.util.i18n.StringManager; 59 60 64 public class ResourcesXMLParser 65 { 66 67 private File resourceFile = null; 68 private Document document; 69 private Vector resources; 70 71 private static final String CUSTOM_RESOURCE = "custom-resource"; 73 private static final String EXT_JNDI_RESOURCE = "external-jndi-resource"; 74 private static final String JDBC_RESOURCE = "jdbc-resource"; 75 private static final String JDBC_CONN_POOL = "jdbc-connection-pool"; 76 private static final String MAIL_RESOURCE = "mail-resource"; 77 private static final String PERSISTENCE_RESOURCE = 78 "persistence-manager-factory-resource"; 79 private static final String JMS_RESOURCE = "jms-resource"; 80 81 private static StringManager localStrings = 83 StringManager.getManager( ResourcesXMLParser.class ); 84 85 public static final String JNDI_NAME = "jndi-name"; 88 public static final String POOL_NAME = "pool-name"; 89 public static final String RES_TYPE = "res-type"; 91 public static final String FACTORY_CLASS = "factory-class"; 92 public static final String ENABLED = "enabled"; 93 94 public static final String JNDI_LOOKUP = "jndi-lookup-name"; 96 97 public static final String CONNECTION_POOL_NAME = "name"; 100 public static final String STEADY_POOL_SIZE = "steady-pool-size"; 101 public static final String MAX_POOL_SIZE = "max-pool-size"; 102 public static final String MAX_WAIT_TIME_IN_MILLIS = "max-wait-time-in-millis"; 103 public static final String POOL_SIZE_QUANTITY = "pool-resize-quantity"; 104 public static final String IDLE_TIME_OUT_IN_SECONDS = "idle-timeout-in-seconds"; 105 public static final String IS_CONNECTION_VALIDATION_REQUIRED = "is-connection-validation-required"; 106 public static final String CONNECTION_VALIDATION_METHOD = "connection-validation-method"; 107 public static final String FAIL_ALL_CONNECTIONS = "fail-all-connections"; 108 public static final String VALIDATION_TABLE_NAME = "validation-table-name"; 109 public static final String DATASOURCE_CLASS = "datasource-classname"; 110 public static final String TRANS_ISOLATION_LEVEL = "transaction-isolation-level"; 111 public static final String IS_ISOLATION_LEVEL_GUARANTEED = "is-isolation-level-guaranteed"; 112 113 public static final String MAIL_HOST = "host"; 115 public static final String MAIL_USER = "user"; 116 public static final String MAIL_FROM_ADDRESS = "from"; 117 public static final String MAIL_STORE_PROTO = "store-protocol"; 118 public static final String MAIL_STORE_PROTO_CLASS = "store-protocol-class"; 119 public static final String MAIL_TRANS_PROTO = "transport-protocol"; 120 public static final String MAIL_TRANS_PROTO_CLASS = "transport-protocol-class"; 121 public static final String MAIL_DEBUG = "debug"; 122 123 public static final String JDBC_RESOURCE_JNDI_NAME = "jdbc-resource-jndi-name"; 125 126 127 public ResourcesXMLParser(String resourceFileName) throws Exception 128 { 129 resourceFile = new File (resourceFileName); 130 initProperties(); 131 resources = new Vector (); 132 generateResourceObjects(); 133 } 134 135 138 public void initProperties() throws Exception 139 { 140 DocumentBuilderFactory factory = 141 DocumentBuilderFactory.newInstance(); 142 try 143 { 144 factory.setValidating(false); 145 DocumentBuilder builder = factory.newDocumentBuilder(); 146 if (resourceFile == null) 147 { 148 String msg = localStrings.getString( "admin.server.core.mbean.config.no_resource_file" ); 149 throw new Exception ( msg ); 150 } 151 InputSource is = new InputSource (resourceFile.toString()); 152 document = builder.parse(is); 153 154 } 155 catch (SAXException sxe) 156 { 157 Exception x = sxe; 158 if (sxe.getException() != null) 159 x = sxe.getException(); 160 throw new Exception (x.getLocalizedMessage()); 162 163 } 164 catch (ParserConfigurationException pce) 165 { 166 throw new Exception (pce.getLocalizedMessage()); 168 } 169 catch (IOException ioe) 170 { 171 throw new Exception (ioe.getLocalizedMessage()); 173 } 174 } 175 176 180 private void generateResourceObjects() throws Exception 181 { 182 if (document != null) 183 { 184 for (Node nextKid = document.getDocumentElement().getFirstChild(); 185 nextKid != null; nextKid = nextKid.getNextSibling()) 186 { 187 String nodeName = nextKid.getNodeName(); 188 if (nodeName.equalsIgnoreCase(CUSTOM_RESOURCE)) 189 { 190 generateCustomResource(nextKid); 191 } 192 else if (nodeName.equalsIgnoreCase(EXT_JNDI_RESOURCE)) 193 { 194 generateJNDIResource(nextKid); 195 } 196 else if (nodeName.equalsIgnoreCase(JDBC_RESOURCE)) 197 { 198 generateJDBCResource(nextKid); 199 } 200 else if (nodeName.equalsIgnoreCase(JDBC_CONN_POOL)) 201 { 202 generateJDBCConnectionPoolResource(nextKid); 203 } 204 else if (nodeName.equalsIgnoreCase(MAIL_RESOURCE)) 205 { 206 generateMailResource(nextKid); 207 } 208 else if (nodeName.equalsIgnoreCase(PERSISTENCE_RESOURCE)) 209 { 210 generatePersistenceResource(nextKid); 211 } 212 else if (nodeName.equalsIgnoreCase(JMS_RESOURCE)) 213 { 214 generateJMSResource(nextKid); 215 } 216 217 } 218 } 219 } 220 221 224 private void generateCustomResource(Node nextKid) throws Exception 225 { 226 NamedNodeMap attributes = nextKid.getAttributes(); 227 if (attributes == null) 228 return; 229 230 Node jndiNameNode = attributes.getNamedItem(JNDI_NAME); 231 String jndiName = jndiNameNode.getNodeValue(); 232 Node resTypeNode = attributes.getNamedItem(RES_TYPE); 233 String resType = resTypeNode.getNodeValue(); 234 Node factoryClassNode = 235 attributes.getNamedItem(FACTORY_CLASS); 236 String factoryClass = factoryClassNode.getNodeValue(); 237 Node enabledNode = attributes.getNamedItem(ENABLED); 238 239 Resource customResource = new Resource(Resource.CUSTOM_RESOURCE); 240 customResource.setAttribute(JNDI_NAME, jndiName); 241 customResource.setAttribute(RES_TYPE, resType); 242 customResource.setAttribute(FACTORY_CLASS, factoryClass); 243 if (enabledNode != null) { 244 String sEnabled = enabledNode.getNodeValue(); 245 customResource.setAttribute(ENABLED, sEnabled); 246 } 247 248 NodeList children = nextKid.getChildNodes(); 249 generatePropertyElement(customResource, children); 250 resources.add(customResource); 251 252 printResourceElements(customResource); 254 } 255 256 259 private void generateJNDIResource(Node nextKid) throws Exception 260 { 261 NamedNodeMap attributes = nextKid.getAttributes(); 262 if (attributes == null) 263 return; 264 265 Node jndiNameNode = attributes.getNamedItem(JNDI_NAME); 266 String jndiName = jndiNameNode.getNodeValue(); 267 Node jndiLookupNode = attributes.getNamedItem(JNDI_LOOKUP); 268 String jndiLookup = jndiLookupNode.getNodeValue(); 269 Node resTypeNode = attributes.getNamedItem(RES_TYPE); 270 String resType = resTypeNode.getNodeValue(); 271 Node factoryClassNode = attributes.getNamedItem(FACTORY_CLASS); 272 String factoryClass = factoryClassNode.getNodeValue(); 273 Node enabledNode = attributes.getNamedItem(ENABLED); 274 275 Resource jndiResource = new Resource(Resource.EXT_JNDI_RESOURCE); 276 jndiResource.setAttribute(JNDI_NAME, jndiName); 277 jndiResource.setAttribute(JNDI_LOOKUP, jndiLookup); 278 jndiResource.setAttribute(RES_TYPE, resType); 279 jndiResource.setAttribute(FACTORY_CLASS, factoryClass); 280 if (enabledNode != null) { 281 String sEnabled = enabledNode.getNodeValue(); 282 jndiResource.setAttribute(ENABLED, sEnabled); 283 } 284 285 NodeList children = nextKid.getChildNodes(); 286 generatePropertyElement(jndiResource, children); 287 resources.add(jndiResource); 288 289 printResourceElements(jndiResource); 291 } 292 293 296 private void generateJDBCResource(Node nextKid) throws Exception 297 { 298 NamedNodeMap attributes = nextKid.getAttributes(); 299 if (attributes == null) 300 return; 301 302 Node jndiNameNode = attributes.getNamedItem(JNDI_NAME); 303 String jndiName = jndiNameNode.getNodeValue(); 304 Node poolNameNode = attributes.getNamedItem(POOL_NAME); 305 String poolName = poolNameNode.getNodeValue(); 306 Node enabledNode = attributes.getNamedItem(ENABLED); 307 308 Resource jdbcResource = new Resource(Resource.JDBC_RESOURCE); 309 jdbcResource.setAttribute(JNDI_NAME, jndiName); 310 jdbcResource.setAttribute(POOL_NAME, poolName); 311 if (enabledNode != null) { 312 String enabledName = enabledNode.getNodeValue(); 313 jdbcResource.setAttribute(ENABLED, enabledName); 314 } 315 316 NodeList children = nextKid.getChildNodes(); 317 if (children != null) 319 { 320 for (int ii=0; ii<children.getLength(); ii++) 321 { 322 if (children.item(ii).getNodeName().equals("description")) 323 jdbcResource.setDescription( 324 children.item(ii).getFirstChild().getNodeValue()); 325 } 326 } 327 328 resources.add(jdbcResource); 329 330 printResourceElements(jdbcResource); 332 } 333 334 337 private void generateJDBCConnectionPoolResource(Node nextKid) throws Exception 338 { 339 NamedNodeMap attributes = nextKid.getAttributes(); 340 if (attributes == null) 341 return; 342 343 Node nameNode = attributes.getNamedItem(CONNECTION_POOL_NAME); 344 String name = nameNode.getNodeValue(); 345 Node nSteadyPoolSizeNode = attributes.getNamedItem(STEADY_POOL_SIZE); 346 Node nMaxPoolSizeNode = attributes.getNamedItem(MAX_POOL_SIZE); 347 Node nMaxWaitTimeInMillisNode = 348 attributes.getNamedItem(MAX_WAIT_TIME_IN_MILLIS); 349 Node nPoolSizeQuantityNode = 350 attributes.getNamedItem(POOL_SIZE_QUANTITY); 351 Node nIdleTimeoutInSecNode = 352 attributes.getNamedItem(IDLE_TIME_OUT_IN_SECONDS); 353 Node nIsConnectionValidationRequiredNode = 354 attributes.getNamedItem(IS_CONNECTION_VALIDATION_REQUIRED); 355 Node nConnectionValidationMethodNode = 356 attributes.getNamedItem(CONNECTION_VALIDATION_METHOD); 357 Node nFailAllConnectionsNode = 358 attributes.getNamedItem(FAIL_ALL_CONNECTIONS); 359 Node nValidationTableNameNode = 360 attributes.getNamedItem(VALIDATION_TABLE_NAME); 361 Node nResType = attributes.getNamedItem(RES_TYPE); 362 Node nTransIsolationLevel = 363 attributes.getNamedItem(TRANS_ISOLATION_LEVEL); 364 Node nIsIsolationLevelQuaranteed = 365 attributes.getNamedItem(IS_ISOLATION_LEVEL_GUARANTEED); 366 Node datasourceNode = attributes.getNamedItem(DATASOURCE_CLASS); 367 String datasource = datasourceNode.getNodeValue(); 368 369 Resource jdbcResource = new Resource(Resource.JDBC_CONN_POOL); 370 jdbcResource.setAttribute(CONNECTION_POOL_NAME, name); 371 jdbcResource.setAttribute(DATASOURCE_CLASS, datasource); 372 if (nSteadyPoolSizeNode != null) { 373 String sSteadyPoolSize = nSteadyPoolSizeNode.getNodeValue(); 374 jdbcResource.setAttribute(STEADY_POOL_SIZE, sSteadyPoolSize); 375 } 376 if (nMaxPoolSizeNode != null) { 377 String sMaxPoolSize = nMaxPoolSizeNode.getNodeValue(); 378 jdbcResource.setAttribute(MAX_POOL_SIZE, sMaxPoolSize); 379 } 380 if (nMaxWaitTimeInMillisNode != null) { 381 String sMaxWaitTimeInMillis = nMaxWaitTimeInMillisNode.getNodeValue(); 382 jdbcResource.setAttribute(MAX_WAIT_TIME_IN_MILLIS, sMaxWaitTimeInMillis); 383 } 384 if (nPoolSizeQuantityNode != null) { 385 String sPoolSizeQuantity = nPoolSizeQuantityNode.getNodeValue(); 386 jdbcResource.setAttribute(POOL_SIZE_QUANTITY, sPoolSizeQuantity); 387 } 388 if (nIdleTimeoutInSecNode != null) { 389 String sIdleTimeoutInSec = nIdleTimeoutInSecNode.getNodeValue(); 390 jdbcResource.setAttribute(IDLE_TIME_OUT_IN_SECONDS, sIdleTimeoutInSec); 391 } 392 if (nIsConnectionValidationRequiredNode != null) { 393 String sIsConnectionValidationRequired = nIsConnectionValidationRequiredNode.getNodeValue(); 394 jdbcResource.setAttribute(IS_CONNECTION_VALIDATION_REQUIRED, sIsConnectionValidationRequired); 395 } 396 if (nConnectionValidationMethodNode != null) { 397 String sConnectionValidationMethod = nConnectionValidationMethodNode.getNodeValue(); 398 jdbcResource.setAttribute(CONNECTION_VALIDATION_METHOD, sConnectionValidationMethod); 399 } 400 if (nFailAllConnectionsNode != null) { 401 String sFailAllConnection = nFailAllConnectionsNode.getNodeValue(); 402 jdbcResource.setAttribute(FAIL_ALL_CONNECTIONS, sFailAllConnection); 403 } 404 if (nValidationTableNameNode != null) { 405 String sValidationTableName = nValidationTableNameNode.getNodeValue(); 406 jdbcResource.setAttribute(VALIDATION_TABLE_NAME, sValidationTableName); 407 } 408 if (nResType != null) { 409 String sResType = nResType.getNodeValue(); 410 jdbcResource.setAttribute(RES_TYPE, sResType); 411 } 412 if (nTransIsolationLevel != null) { 413 String sTransIsolationLevel = nTransIsolationLevel.getNodeValue(); 414 jdbcResource.setAttribute(TRANS_ISOLATION_LEVEL, sTransIsolationLevel); 415 } 416 if (nIsIsolationLevelQuaranteed != null) { 417 String sIsIsolationLevelQuaranteed = 418 nIsIsolationLevelQuaranteed.getNodeValue(); 419 jdbcResource.setAttribute(IS_ISOLATION_LEVEL_GUARANTEED, 420 sIsIsolationLevelQuaranteed); 421 } 422 423 NodeList children = nextKid.getChildNodes(); 424 generatePropertyElement(jdbcResource, children); 425 resources.add(jdbcResource); 426 427 printResourceElements(jdbcResource); 429 } 430 431 434 private void generateMailResource(Node nextKid) throws Exception 435 { 436 NamedNodeMap attributes = nextKid.getAttributes(); 437 if (attributes == null) 438 return; 439 440 Node jndiNameNode = attributes.getNamedItem(JNDI_NAME); 441 Node hostNode = attributes.getNamedItem(MAIL_HOST); 442 Node userNode = attributes.getNamedItem(MAIL_USER); 443 Node fromAddressNode = attributes.getNamedItem(MAIL_FROM_ADDRESS); 444 Node storeProtoNode = attributes.getNamedItem(MAIL_STORE_PROTO); 445 Node storeProtoClassNode = attributes.getNamedItem(MAIL_STORE_PROTO_CLASS); 446 Node transProtoNode = attributes.getNamedItem(MAIL_TRANS_PROTO); 447 Node transProtoClassNode = attributes.getNamedItem(MAIL_TRANS_PROTO_CLASS); 448 Node debugNode = attributes.getNamedItem(MAIL_DEBUG); 449 Node enabledNode = attributes.getNamedItem(ENABLED); 450 451 String jndiName = jndiNameNode.getNodeValue(); 452 String host = hostNode.getNodeValue(); 453 String user = userNode.getNodeValue(); 454 String fromAddress = fromAddressNode.getNodeValue(); 455 456 Resource mailResource = new Resource(Resource.MAIL_RESOURCE); 457 458 mailResource.setAttribute(JNDI_NAME, jndiName); 459 mailResource.setAttribute(MAIL_HOST, host); 460 mailResource.setAttribute(MAIL_USER, user); 461 mailResource.setAttribute(MAIL_FROM_ADDRESS, fromAddress); 462 if (storeProtoNode != null) { 463 String sStoreProto = storeProtoNode.getNodeValue(); 464 mailResource.setAttribute(MAIL_STORE_PROTO, sStoreProto); 465 } 466 if (storeProtoClassNode != null) { 467 String sStoreProtoClass = storeProtoClassNode.getNodeValue(); 468 mailResource.setAttribute(MAIL_STORE_PROTO_CLASS, sStoreProtoClass); 469 } 470 if (transProtoNode != null) { 471 String sTransProto = transProtoNode.getNodeValue(); 472 mailResource.setAttribute(MAIL_TRANS_PROTO, sTransProto); 473 } 474 if (transProtoClassNode != null) { 475 String sTransProtoClass = transProtoClassNode.getNodeValue(); 476 mailResource.setAttribute(MAIL_TRANS_PROTO_CLASS, sTransProtoClass); 477 } 478 if (debugNode != null) { 479 String sDebug = debugNode.getNodeValue(); 480 mailResource.setAttribute(MAIL_DEBUG, sDebug); 481 } 482 if (enabledNode != null) { 483 String sEnabled = enabledNode.getNodeValue(); 484 mailResource.setAttribute(ENABLED, sEnabled); 485 } 486 487 NodeList children = nextKid.getChildNodes(); 488 generatePropertyElement(mailResource, children); 489 resources.add(mailResource); 490 491 printResourceElements(mailResource); 493 } 494 495 498 private void generatePersistenceResource(Node nextKid) throws Exception 499 { 500 NamedNodeMap attributes = nextKid.getAttributes(); 501 if (attributes == null) 502 return; 503 504 Node jndiNameNode = attributes.getNamedItem(JNDI_NAME); 505 String jndiName = jndiNameNode.getNodeValue(); 506 Node factoryClassNode = attributes.getNamedItem(FACTORY_CLASS); 507 Node poolNameNode = attributes.getNamedItem(JDBC_RESOURCE_JNDI_NAME); 508 Node enabledNode = attributes.getNamedItem(ENABLED); 509 510 Resource persistenceResource = 511 new Resource(Resource.PERSISTENCE_RESOURCE); 512 persistenceResource.setAttribute(JNDI_NAME, jndiName); 513 if (factoryClassNode != null) { 514 String factoryClass = factoryClassNode.getNodeValue(); 515 persistenceResource.setAttribute(FACTORY_CLASS, factoryClass); 516 } 517 if (poolNameNode != null) { 518 String poolName = poolNameNode.getNodeValue(); 519 persistenceResource.setAttribute(JDBC_RESOURCE_JNDI_NAME, poolName); 520 } 521 if (enabledNode != null) { 522 String sEnabled = enabledNode.getNodeValue(); 523 persistenceResource.setAttribute(ENABLED, sEnabled); 524 } 525 526 NodeList children = nextKid.getChildNodes(); 527 generatePropertyElement(persistenceResource, children); 528 resources.add(persistenceResource); 529 530 printResourceElements(persistenceResource); 532 } 533 534 537 private void generateJMSResource(Node nextKid) throws Exception 538 { 539 NamedNodeMap attributes = nextKid.getAttributes(); 540 if (attributes == null) 541 return; 542 543 Node jndiNameNode = attributes.getNamedItem(JNDI_NAME); 544 String jndiName = jndiNameNode.getNodeValue(); 545 Node resTypeNode = attributes.getNamedItem(RES_TYPE); 546 String resType = resTypeNode.getNodeValue(); 547 Node enabledNode = attributes.getNamedItem(ENABLED); 548 549 Resource jmsResource = new Resource(Resource.JMS_RESOURCE); 550 jmsResource.setAttribute(JNDI_NAME, jndiName); 551 jmsResource.setAttribute(RES_TYPE, resType); 552 if (enabledNode != null) { 553 String sEnabled = enabledNode.getNodeValue(); 554 jmsResource.setAttribute(ENABLED, sEnabled); 555 } 556 557 NodeList children = nextKid.getChildNodes(); 558 generatePropertyElement(jmsResource, children); 559 resources.add(jmsResource); 560 561 printResourceElements(jmsResource); 563 } 564 565 private void generatePropertyElement(Resource rs, NodeList children) throws Exception 566 { 567 if (children != null) { 568 for (int ii=0; ii<children.getLength(); ii++) { 569 if (children.item(ii).getNodeName().equals("property")) { 570 NamedNodeMap attNodeMap = children.item(ii).getAttributes(); 571 Node nameNode = attNodeMap.getNamedItem("name"); 572 Node valueNode = attNodeMap.getNamedItem("value"); 573 if (nameNode != null && valueNode != null) { 574 boolean bDescFound = false; 575 String sName = nameNode.getNodeValue(); 576 String sValue = valueNode.getNodeValue(); 577 Node descNode = children.item(ii).getFirstChild(); 579 while (descNode != null && !bDescFound) { 580 if (descNode.getNodeName().equalsIgnoreCase("description")) { 581 try { 582 rs.setElementProperty(sName, sValue, descNode.getFirstChild().getNodeValue()); 583 bDescFound = true; 584 } 585 catch (DOMException dome) { 586 throw new Exception (dome.getLocalizedMessage()); 588 } 589 } 590 descNode = descNode.getNextSibling(); 591 } 592 if (!bDescFound) { 593 rs.setElementProperty(sName, sValue); 594 } 595 } 596 } 597 if (children.item(ii).getNodeName().equals("description")) { 598 rs.setDescription(children.item(ii).getFirstChild().getNodeValue()); 599 } 600 } 601 } 602 } 603 604 public Iterator getResources() 605 { 606 return resources.iterator(); 607 } 608 609 612 private void printResourceElements(Resource resource) 613 { 614 Properties attributes = resource.getAttributes(); 615 Enumeration properties = attributes.propertyNames(); 616 while (properties.hasMoreElements()) 617 { 618 String name = (String ) properties.nextElement(); 619 Logger logger = Logger.getLogger(AdminConstants.kLoggerName); 620 logger.log(Level.FINE, "general.print_attr_name", name); 621 } 622 } 623 } 624 625 | Popular Tags |