1 24 25 package org.objectweb.cjdbc.controller.xml; 26 27 import java.io.BufferedReader ; 28 import java.io.File ; 29 import java.io.FileReader ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.io.StringReader ; 33 import java.net.InetAddress ; 34 import java.net.URL ; 35 import java.net.URLDecoder ; 36 import java.util.ArrayList ; 37 import java.util.Locale ; 38 39 import org.objectweb.cjdbc.common.i18n.Translate; 40 import org.objectweb.cjdbc.common.jmx.JmxConstants; 41 import org.objectweb.cjdbc.common.jmx.JmxException; 42 import org.objectweb.cjdbc.common.log.Trace; 43 import org.objectweb.cjdbc.common.net.SSLConfiguration; 44 import org.objectweb.cjdbc.common.xml.ControllerXmlTags; 45 import org.objectweb.cjdbc.common.xml.XmlValidator; 46 import org.objectweb.cjdbc.controller.core.Controller; 47 import org.objectweb.cjdbc.controller.core.ControllerConstants; 48 import org.objectweb.cjdbc.controller.core.ControllerFactory; 49 import org.objectweb.cjdbc.controller.core.ReportManager; 50 import org.objectweb.cjdbc.controller.core.security.ControllerSecurityManager; 51 import org.xml.sax.Attributes ; 52 import org.xml.sax.InputSource ; 53 import org.xml.sax.SAXException ; 54 import org.xml.sax.SAXParseException ; 55 import org.xml.sax.XMLReader ; 56 import org.xml.sax.helpers.DefaultHandler ; 57 import org.xml.sax.helpers.XMLReaderFactory ; 58 59 67 public class ControllerParser extends DefaultHandler 68 { 69 70 static Trace logger = Trace 71 .getLogger(ControllerParser.class 72 .getName()); 73 74 75 private XMLReader parser; 76 77 78 private ControllerFactory config; 79 private ControllerSecurityManager security; 80 private Controller controller; 81 private boolean parseAccept = false; 82 private SSLConfiguration ssl; 83 private String controllerIP; 84 private ReportManager manager; 85 86 94 public ControllerParser(ControllerFactory configure) throws Exception 95 { 96 this.config = configure; 97 this.controller = configure.getController(); 98 99 parser = XMLReaderFactory.createXMLReader(); 101 102 parser.setFeature("http://xml.org/sax/features/validation", true); 104 105 parser.setErrorHandler(this); 107 108 parser.setContentHandler(this); 110 111 parser.setEntityResolver(this); 113 } 114 115 122 public void readXML(String xml) throws IOException , SAXException 123 { 124 if (xml != null) 125 { 126 InputSource input = new InputSource (new StringReader (xml)); 127 parser.parse(input); 128 } 129 else 130 throw new IOException ("Input was null in input source."); 131 } 132 133 141 public void readXML(String xml, boolean validateBeforeParsing) 142 throws IOException , SAXException 143 { 144 if (validateBeforeParsing) 145 { 146 XmlValidator validator = new XmlValidator( 147 ControllerConstants.C_JDBC_CONTROLLER_DTD_FILE, xml.toString()); 148 if (logger.isDebugEnabled()) 149 { 150 if (validator.isDtdValid()) 151 logger.debug(Translate.get("controller.xml.dtd.validated")); 152 if (validator.isXmlValid()) 153 logger.debug(Translate.get("controller.xml.document.validated")); 154 } 155 156 if (validator.getWarnings().size() > 0) 157 { 158 ArrayList warnings = validator.getWarnings(); 159 for (int i = 0; i < warnings.size(); i++) 160 logger.warn(Translate.get("virtualdatabase.xml.parsing.warning", 161 warnings.get(i))); 162 } 163 164 if (!validator.isDtdValid()) 165 logger.error(Translate.get("controller.xml.dtd.not.validated")); 166 if (!validator.isXmlValid()) 167 logger.error(Translate.get("controller.xml.document.not.validated")); 168 169 ArrayList errors = validator.getExceptions(); 170 for (int i = 0; i < errors.size(); i++) 171 logger.error(((Exception ) errors.get(i)).getMessage()); 172 173 if (!validator.isValid()) 174 throw new SAXException (Translate 175 .get("controller.xml.document.not.valid")); 176 } 177 readXML(xml); 178 } 179 180 188 public void readXML(FileReader fileReader, boolean validateBeforeParsing) 189 throws IOException , SAXException 190 { 191 if (fileReader != null) 192 { 193 194 BufferedReader in = new BufferedReader (fileReader); 196 StringBuffer xml = new StringBuffer (); 197 String line; 198 do 199 { 200 line = in.readLine(); 201 if (line != null) 202 xml.append(line.trim()); 203 } 204 while (line != null); 205 206 readXML(xml.toString(), validateBeforeParsing); 207 } 208 else 209 { 210 throw new IOException ("Input was null in input source."); 211 } 212 } 213 214 221 public void fatalError(SAXParseException e) throws SAXException 222 { 223 logger.error(Translate.get("controller.xml.parsing.fatal", new String []{ 224 e.getPublicId(), String.valueOf(e.getLineNumber()), 225 String.valueOf(e.getColumnNumber()), e.getMessage()})); 226 throw e; 227 } 228 229 236 public void error(SAXParseException e) throws SAXException 237 { 238 logger.error(Translate.get("controller.xml.parsing.error", new String []{ 239 e.getPublicId(), String.valueOf(e.getLineNumber()), 240 String.valueOf(e.getColumnNumber()), e.getMessage()})); 241 throw e; 242 } 243 244 252 public InputSource resolveEntity(String publicId, String systemId) 253 throws SAXException 254 { 255 logger.debug(Translate.get("controller.xml.dtd.using", 256 ControllerConstants.C_JDBC_CONTROLLER_DTD_FILE)); 257 InputStream stream = ControllerParser.class.getResourceAsStream("/" 258 + ControllerConstants.C_JDBC_CONTROLLER_DTD_FILE); 259 if (stream == null) 260 { 261 throw new SAXException (Translate.get("controller.xml.dtd.not.found", 262 ControllerConstants.C_JDBC_CONTROLLER_DTD_FILE)); 263 } 264 265 return new InputSource (stream); 266 } 267 268 273 public void startDocument() throws SAXException 274 { 275 logger.debug(Translate.get("controller.xml.parsing.document")); 276 } 277 278 283 public void endDocument() throws SAXException 284 { 285 logger.info(Translate.get("controller.xml.done")); 286 } 287 288 297 public void startElement(String uri, String localName, String name, 298 Attributes atts) throws SAXException 299 { 300 logger.debug(Translate.get("controller.xml.parsing.start", name)); 301 if (name.equalsIgnoreCase(ControllerXmlTags.ELT_CONTROLLER)) 302 configureController(atts); 303 else if (name.equalsIgnoreCase(ControllerXmlTags.ELT_INTERNATIONALIZATION)) 304 { 305 Locale.setDefault(new Locale (atts 306 .getValue(ControllerXmlTags.ATT_LANGUAGE), "")); 307 } 308 else if (name.equalsIgnoreCase(ControllerXmlTags.ELT_REPORT)) 309 configureReport(atts); 310 else if (name.equalsIgnoreCase(ControllerXmlTags.ELT_JMX)) 311 { 312 config.put(ControllerFactory.JMX_ENABLE, "true"); 313 } 314 else if (name.equalsIgnoreCase(ControllerXmlTags.ELT_HTTP_JMX_ADAPTOR)) 315 configureHttpJmxAdaptor(atts); 316 else if (name.equalsIgnoreCase(ControllerXmlTags.ELT_RMI_JMX_ADAPTOR)) 317 configureRmiJmxAdaptor(atts); 318 else if (name.equalsIgnoreCase(ControllerXmlTags.ELT_SSL)) 319 configureSSL(atts); 320 else if (name.equalsIgnoreCase(ControllerXmlTags.ELT_VIRTUAL_DATABASE)) 321 configureVirtualDatabase(atts); 322 else if (name.equalsIgnoreCase(ControllerXmlTags.ELT_SECURITY)) 323 { 324 security = new ControllerSecurityManager(); 325 boolean connect = new Boolean (atts 326 .getValue(ControllerXmlTags.ATT_DEFAULT_CONNECT)).booleanValue(); 327 security.setDefaultConnect(connect); 328 } 329 else if (name.equalsIgnoreCase(ControllerXmlTags.ELT_JAR)) 330 { 331 boolean allow = new Boolean (atts 332 .getValue(ControllerXmlTags.ATT_JAR_ALLOW_DRIVER)).booleanValue(); 333 security.setAllowAdditionalDriver(allow); 334 } 335 else if (name.equalsIgnoreCase(ControllerXmlTags.ELT_CLIENT)) 336 configureClient(atts); 337 else if (name.equalsIgnoreCase(ControllerXmlTags.ELT_CONSOLE)) 338 configureConsole(atts); 339 else if (name.equalsIgnoreCase(ControllerXmlTags.ELT_ACCEPT)) 340 parseAccept = true; 341 else if (name.equalsIgnoreCase(ControllerXmlTags.ELT_BLOCK)) 342 parseAccept = false; 343 else if (name.equalsIgnoreCase(ControllerXmlTags.ELT_HOSTNAME)) 344 security.addHostToSecureList(atts.getValue(ControllerXmlTags.ATT_VALUE), 345 parseAccept); 346 else if (name.equalsIgnoreCase(ControllerXmlTags.ELT_IPADDRESS)) 347 security.addHostToSecureList(atts.getValue(ControllerXmlTags.ATT_VALUE), 348 parseAccept); 349 else if (name.equalsIgnoreCase(ControllerXmlTags.ELT_IPRANGE)) 350 configureIpRange(atts); 351 } 352 353 361 public void endElement(String uri, String localName, String name) 362 throws SAXException 363 { 364 if (name.equalsIgnoreCase(ControllerXmlTags.ELT_JMX)) 366 { 367 try 368 { 369 config.setUpJmx(); 370 } 371 catch (JmxException jmxEx) 372 { 373 logger.error(Translate.get("controller.xml.jmx.setup.failed", jmxEx 374 .getMessage()), jmxEx); 375 } 376 } 377 if (name.equalsIgnoreCase(ControllerXmlTags.ELT_SECURITY)) 378 { 379 security.setSslConfig(ssl); 380 ssl = null; 381 config.setUpSecurity(security); 382 } 383 if (name.equalsIgnoreCase(ControllerXmlTags.ELT_RMI_JMX_ADAPTOR)) 384 { 385 if (ssl != null) 386 { 387 config.put(JmxConstants.CONNECTOR_RMI_SSL, ssl); 388 ssl = null; 389 } 390 } 391 if (name.equalsIgnoreCase(ControllerXmlTags.ELT_CONTROLLER)) 392 { 393 if (manager == null) 395 { 396 manager = new ReportManager(controller); 397 manager.setSettings(null); 398 controller.setReport(manager); 399 } 400 } 401 logger.debug(Translate.get("controller.xml.parsing.end", name)); 402 } 403 404 409 private void configureClient(Attributes atts) 410 { 411 boolean localhost = new Boolean (atts 412 .getValue(ControllerXmlTags.ATT_ONLY_LOCALHOST)).booleanValue(); 413 boolean allow = new Boolean (atts.getValue(ControllerXmlTags.ATT_ALLOW)) 414 .booleanValue(); 415 security.setAllowClientShutdown(allow); 416 security.setAllowLocalClientOnly(localhost); 417 } 418 419 424 private void configureConsole(Attributes atts) 425 { 426 boolean localhost = new Boolean (atts 427 .getValue(ControllerXmlTags.ATT_ONLY_LOCALHOST)).booleanValue(); 428 boolean allow = new Boolean (atts.getValue(ControllerXmlTags.ATT_ALLOW)) 429 .booleanValue(); 430 security.setAllowConsoleShutdown(allow); 431 security.setAllowLocalConsoleOnly(localhost); 432 } 433 434 440 private void configureController(Attributes atts) throws SAXException 441 { 442 try 443 { 444 String controllerPort = atts 445 .getValue(ControllerXmlTags.ATT_CONTROLLER_PORT); 446 if (controllerPort == null) 447 config.put(ControllerFactory.CONTROLLER_PORT, String 448 .valueOf(ControllerConstants.DEFAULT_PORT)); 449 else 450 config.put(ControllerFactory.CONTROLLER_PORT, controllerPort); 451 config.getController().setPortNumber( 452 Integer.parseInt((String ) config 453 .get(ControllerFactory.CONTROLLER_PORT))); 454 455 controllerIP = atts.getValue(ControllerXmlTags.ATT_CONTROLLER_IP); 456 if (controllerIP == null) 457 { 458 try 459 { 460 468 String localIP = InetAddress.getLocalHost().getHostAddress(); 469 config.put(ControllerFactory.CONTROLLER_IP, localIP); 470 } 471 catch (RuntimeException e1) 472 { 473 logger 474 .warn("Unable to obtain IP address of controller, setting default address: " 475 + ControllerConstants.DEFAULT_IP); 476 config.put(ControllerFactory.CONTROLLER_IP, 477 ControllerConstants.DEFAULT_IP); 478 } 479 } 480 else 481 config.put(ControllerFactory.CONTROLLER_IP, controllerIP); 482 config.getController().setIPAddress( 483 (String ) config.get(ControllerFactory.CONTROLLER_IP)); 484 485 String controllerBacklog = atts 486 .getValue(ControllerXmlTags.ATT_backlogSize); 487 if (controllerBacklog == null) 488 config.put(ControllerFactory.CONTROLLER_BACKLOG, String 489 .valueOf(ControllerConstants.DEFAULT_BACKLOG_SIZE)); 490 else 491 config.put(ControllerFactory.CONTROLLER_BACKLOG, controllerBacklog); 492 config.getController().setBacklogSize( 493 Integer.parseInt((String ) config 494 .get(ControllerFactory.CONTROLLER_BACKLOG))); 495 } 496 catch (Exception e) 497 { 498 throw new SAXException (e.getMessage()); 499 } 500 } 501 502 507 private void configureHttpJmxAdaptor(Attributes atts) 508 { 509 String adaptorPort = atts.getValue(ControllerXmlTags.ATT_JMX_ADAPTOR_PORT); 510 if (config.get(JmxConstants.ADAPTOR_TYPE_HTTP) == null) 511 config.put(JmxConstants.ADAPTOR_TYPE_HTTP, String.valueOf(adaptorPort)); 512 } 513 514 519 private void configureIpRange(Attributes atts) 520 { 521 String iprange = atts.getValue(ControllerXmlTags.ATT_VALUE); 522 try 523 { 524 security.addToSecureList(iprange, parseAccept); 525 } 526 catch (Exception e) 527 { 528 logger.warn(Translate 529 .get("controller.configure.invalid.iprange", iprange)); 530 } 531 } 532 533 538 private void configureReport(Attributes atts) 539 { 540 config.put(ControllerXmlTags.ATT_REPORT_ENABLED, "true"); 541 config.put(ControllerXmlTags.ATT_REPORT_HIDE_SENSITIVE_DATA, atts 542 .getValue(ControllerXmlTags.ATT_REPORT_HIDE_SENSITIVE_DATA)); 543 config.put(ControllerXmlTags.ATT_REPORT_GENERATE_ON_SHUTDOWN, atts 544 .getValue(ControllerXmlTags.ATT_REPORT_GENERATE_ON_SHUTDOWN)); 545 config.put(ControllerXmlTags.ATT_REPORT_GENERATE_ON_FATAL, atts 546 .getValue(ControllerXmlTags.ATT_REPORT_GENERATE_ON_FATAL)); 547 config.put(ControllerXmlTags.ATT_REPORT_DELETE_ON_SHUTDOWN, atts 548 .getValue(ControllerXmlTags.ATT_REPORT_DELETE_ON_SHUTDOWN)); 549 String reportLocation = atts 550 .getValue(ControllerXmlTags.ATT_REPORT_REPORT_LOCATION); 551 if ((reportLocation == null) || reportLocation.equals("")) 553 { 554 String defaultDir = System.getProperty("cjdbc.home"); 555 if (defaultDir == null) 556 { 557 reportLocation = "."; 558 } 559 else 560 { 561 reportLocation = defaultDir + File.separator 562 + ControllerConstants.DEFAULT_LOG_DIR_NAME; 563 } 564 } 565 config.put(ControllerXmlTags.ATT_REPORT_REPORT_LOCATION, reportLocation); 566 567 config.put(ControllerXmlTags.ATT_REPORT_ENABLE_FILE_LOGGING, atts 568 .getValue(ControllerXmlTags.ATT_REPORT_ENABLE_FILE_LOGGING)); 569 manager = new ReportManager(controller); 570 manager.setSettings(config); 571 controller.setReport(manager); 572 } 573 574 579 private void configureRmiJmxAdaptor(Attributes atts) 580 { 581 String adaptorPort = atts.getValue(ControllerXmlTags.ATT_JMX_ADAPTOR_PORT); 582 if (config.get(JmxConstants.ADAPTOR_TYPE_RMI) == null) 583 config.put(JmxConstants.ADAPTOR_TYPE_RMI, String.valueOf(adaptorPort)); 584 585 String username = atts 586 .getValue(ControllerXmlTags.ATT_JMX_CONNECTOR_USERNAME); 587 String password = atts 588 .getValue(ControllerXmlTags.ATT_JMX_CONNECTOR_PASSWORD); 589 if (username != null) 590 config.put(JmxConstants.CONNECTOR_AUTH_USERNAME, username); 591 if (password != null) 592 config.put(JmxConstants.CONNECTOR_AUTH_PASSWORD, password); 593 } 594 595 600 private void configureSSL(Attributes atts) 601 { 602 ssl = new SSLConfiguration(); 603 ssl 604 .setKeyStore(new File (atts.getValue(ControllerXmlTags.ATT_SSL_KEYSTORE))); 605 ssl.setKeyStorePassword(atts 606 .getValue(ControllerXmlTags.ATT_SSL_KEYSTORE_PASSWORD)); 607 ssl.setKeyStoreKeyPassword(atts 608 .getValue(ControllerXmlTags.ATT_SSL_KEYSTORE_KEYPASSWORD)); 609 ssl.setClientAuthenticationRequired("true".equals(atts 610 .getValue(ControllerXmlTags.ATT_SSL_NEED_CLIENT_AUTH))); 611 ssl.setTrustStore(new File (atts 612 .getValue(ControllerXmlTags.ATT_SSL_TRUSTSTORE))); 613 ssl.setTrustStorePassword(atts 614 .getValue(ControllerXmlTags.ATT_SSL_TRUSTSTORE_PASSWORD)); 615 } 616 617 623 private void configureVirtualDatabase(Attributes atts) throws SAXException 624 { 625 String checkPoint = atts 626 .getValue(ControllerXmlTags.ATT_VIRTUAL_DATABASE_CHECKPOINT); 627 String virtualName = atts 628 .getValue(ControllerXmlTags.ATT_VIRTUAL_DATABASE_NAME); 629 String file = atts.getValue(ControllerXmlTags.ATT_VIRTUAL_DATABASE_FILE); 630 631 if (file.indexOf(File.separator) == -1) 634 { 635 try 636 { 637 URL url = this.getClass().getResource("/" + file); 638 file = url.getFile(); 639 logger.info(Translate.get("controller.configure.using", file)); 640 } 641 catch (Exception e) 642 { 643 throw new SAXException (Translate.get( 644 "controller.configure.file.not.found", file)); 645 } 646 } 647 648 file = URLDecoder.decode(file); 649 650 File checkExist = new File (file); 651 if (checkExist.exists() == false) 652 throw new SAXException (Translate.get( 653 "controller.configure.file.not.found", file)); 654 655 int autoLoad = -1; 656 String autoLoadString = atts 657 .getValue(ControllerXmlTags.ATT_VIRTUAL_DATABASE_AUTO_ENABLE); 658 659 if (autoLoadString.equalsIgnoreCase(ControllerXmlTags.VAL_true)) 660 autoLoad = ControllerConstants.AUTO_ENABLE_TRUE; 661 else if (autoLoadString.equalsIgnoreCase(ControllerXmlTags.VAL_force)) 662 autoLoad = ControllerConstants.AUTO_ENABLE_FORCE; 663 else 664 autoLoad = ControllerConstants.AUTO_ENABLE_FALSE; 665 666 logger.info(Translate.get("controller.configure.setup", new String []{ 667 virtualName, String.valueOf(autoLoad), checkPoint})); 668 config.setUpVirtualDatabase(file, virtualName, autoLoad, checkPoint); 669 } 670 671 } | Popular Tags |