1 22 package org.jboss.jdbc; 23 24 import java.awt.HeadlessException ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.lang.reflect.Method ; 28 import java.sql.Connection ; 29 import java.sql.DriverManager ; 30 import java.sql.Statement ; 31 32 import org.jboss.system.ServiceMBeanSupport; 33 import org.jboss.system.server.ServerConfigLocator; 34 35 46 public class HypersonicDatabase extends ServiceMBeanSupport 47 implements HypersonicDatabaseMBean 48 { 49 50 private static final String DEFAULT_PASSWORD = ""; 51 52 53 private static final String DEFAULT_USER = "sa"; 54 55 56 private static final String JDBC_DRIVER_CLASS = "org.hsqldb.jdbcDriver"; 57 58 59 private static final String JDBC_URL_PREFIX = "jdbc:hsqldb:"; 60 61 62 private static final String DEFAULT_REMOTE_SHUTDOWN_COMMAND = "SHUTDOWN COMPACT"; 63 64 65 private static final String DEFAULT_IN_PROCESS_SHUTDOWN_COMMAND = "SHUTDOWN COMPACT"; 66 67 68 private static final String DEFAULT_IN_MEMORY_SHUTDOWN_COMMAND = "SHUTDOWN IMMEDIATELY"; 69 70 71 private static final String HYPERSONIC_DATA_DIR = "hypersonic"; 72 73 74 private static final int DEFAULT_PORT = 1701; 75 76 77 private static final String DEFAULT_ADDRESS = "0.0.0.0"; 78 79 80 private static final String DEFAULT_DATABASE_NAME = "default"; 81 82 83 private static final String IN_MEMORY_DATABASE = "."; 84 85 86 private static final String DEFAULT_DATABASE_MANAGER_CLASS = "org.hsqldb.util.DatabaseManagerSwing"; 87 88 89 private static final String DEFAULT_SERVER_CLASS = "org.hsqldb.Server"; 90 91 93 94 private File dbPath; 95 96 97 private String name = DEFAULT_DATABASE_NAME; 98 99 100 private int port = DEFAULT_PORT; 101 102 103 private String address = DEFAULT_ADDRESS; 104 105 106 private boolean silent = true; 107 108 109 private boolean trace = false; 110 111 112 private boolean no_system_exit = true; 113 114 115 private boolean persist = true; 116 117 118 private String shutdownCommand; 119 120 121 private boolean inProcessMode = false; 122 123 124 private String user = DEFAULT_USER; 125 126 127 private String password = DEFAULT_PASSWORD; 128 129 130 private String databaseManagerClass = DEFAULT_DATABASE_MANAGER_CLASS; 131 132 133 private String serverClass = DEFAULT_SERVER_CLASS; 134 135 136 private Thread serverThread; 137 138 139 private Connection connection; 140 141 private String dbDataDir; 142 143 145 148 public HypersonicDatabase() 149 { 150 } 152 153 155 public String getDbDataDir() 156 { 157 return dbDataDir; 158 } 159 160 public void setDbDataDir(String dbDataDir) 161 { 162 this.dbDataDir = dbDataDir; 163 } 164 165 170 public void setDatabase(String name) 171 { 172 if (name == null) 173 { 174 name = DEFAULT_DATABASE_NAME; 175 } 176 this.name = name; 177 } 178 179 184 public String getDatabase() 185 { 186 return name; 187 } 188 189 194 public void setPort(final int port) 195 { 196 this.port = port; 197 } 198 199 204 public int getPort() 205 { 206 return port; 207 } 208 209 214 public void setBindAddress(final String address) 215 { 216 this.address = address; 217 } 218 219 224 public String getBindAddress() 225 { 226 return address; 227 } 228 229 234 public void setSilent(final boolean silent) 235 { 236 this.silent = silent; 237 } 238 239 244 public boolean getSilent() 245 { 246 return silent; 247 } 248 249 254 public void setTrace(final boolean trace) 255 { 256 this.trace = trace; 257 } 258 259 264 public boolean getTrace() 265 { 266 return trace; 267 } 268 269 274 public void setNo_system_exit(final boolean no_system_exit) 275 { 276 this.no_system_exit = no_system_exit; 277 } 278 279 284 public boolean getNo_system_exit() 285 { 286 return no_system_exit; 287 } 288 289 296 public void setPersist(final boolean persist) 297 { 298 this.persist = persist; 299 } 300 301 308 public boolean getPersist() 309 { 310 return persist; 311 } 312 313 318 public String getDatabasePath() 319 { 320 if (dbPath != null) 321 { 322 return dbPath.toString(); 323 } 324 else 325 { 326 return null; 327 } 328 } 329 330 335 public boolean isInProcessMode() 336 { 337 return inProcessMode; 338 } 339 340 345 public String getShutdownCommand() 346 { 347 return shutdownCommand; 348 } 349 350 357 public void setInProcessMode(boolean b) 358 { 359 inProcessMode = b; 360 } 361 362 367 public void setShutdownCommand(String string) 368 { 369 shutdownCommand = string; 370 } 371 372 377 public String getPassword() 378 { 379 return password; 380 } 381 382 387 public String getUser() 388 { 389 return user; 390 } 391 392 397 public void setPassword(String password) 398 { 399 if (password == null) 400 { 401 password = DEFAULT_PASSWORD; 402 } 403 this.password = password; 404 } 405 406 411 public void setUser(String user) 412 { 413 if (user == null) 414 { 415 user = DEFAULT_USER; 416 } 417 this.user = user; 418 } 419 420 425 public String getDatabaseManagerClass() 426 { 427 return databaseManagerClass; 428 } 429 430 437 public void setDatabaseManagerClass(String databaseManagerClass) 438 { 439 if (databaseManagerClass == null) 440 { 441 databaseManagerClass = DEFAULT_DATABASE_MANAGER_CLASS; 442 } 443 this.databaseManagerClass = databaseManagerClass; 444 } 445 446 449 public String getServerClass() 450 { 451 return serverClass; 452 } 453 454 459 public void setServerClass(String serverClass) 460 { 461 if (serverClass == null) 462 { 463 serverClass = DEFAULT_SERVER_CLASS; 464 } 465 this.serverClass = serverClass; 466 } 467 468 470 475 public void startDatabaseManager() 476 { 477 new Thread () 479 { 480 public void run() 481 { 482 try 483 { 484 String connectHost = DEFAULT_ADDRESS.equals(address) ? "localhost" : address; 486 String driver = JDBC_DRIVER_CLASS; 487 String [] args; 488 if (!inProcessMode) 489 { 490 args = 491 new String [] { 492 "-noexit", 493 "-driver", driver, 494 "-url", JDBC_URL_PREFIX + "hsql://" + connectHost + ":" + port, 495 "-user", user, 496 "-password", password, 497 "-dir", getDatabasePath() 498 }; 499 } 500 else if (IN_MEMORY_DATABASE.equals(name)) 501 { 502 args = 503 new String [] { 504 "-noexit", 505 "-driver", driver, 506 "-url", JDBC_URL_PREFIX + IN_MEMORY_DATABASE, 507 "-user", user, 508 "-password", password 509 }; 510 } 511 else 512 { 513 args = 514 new String [] { 515 "-noexit", 516 "-driver", driver, 517 "-url", JDBC_URL_PREFIX + getDatabasePath(), 518 "-user", user, 519 "-password", password, 520 "-dir", getDatabasePath() 521 }; 522 } 523 524 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 526 Class clazz = Class.forName(databaseManagerClass, true, cl); 527 Method main = clazz.getMethod("main", new Class [] { args.getClass() }); 528 main.invoke(null, new Object [] { args }); 529 } 530 catch (HeadlessException e) 531 { 532 log.error("Failed to start database manager because this is an headless configuration (no display, mouse or keyword)"); 533 } 534 catch (Exception e) 535 { 536 log.error("Failed to start database manager", e); 537 } 538 } 539 } 540 .start(); 541 } 542 543 545 548 protected void startService() throws Exception 549 { 550 if (!persist) 552 { 553 inProcessMode = true; 554 name = IN_MEMORY_DATABASE; 555 } 556 557 if (!inProcessMode) 559 { 560 startRemoteDatabase(); 561 } 562 else if (IN_MEMORY_DATABASE.equals(name)) 563 { 564 startInMemoryDatabase(); 565 } 566 else 567 { 568 startStandaloneDatabase(); 569 } 570 } 571 572 577 protected void stopService() throws Exception 578 { 579 if (!inProcessMode) 581 { 582 stopRemoteDatabase(); 583 } 584 else if (IN_MEMORY_DATABASE.equals(name)) 585 { 586 stopInMemoryDatabase(); 587 } 588 else 589 { 590 stopStandaloneDatabase(); 591 } 592 } 593 594 596 599 private void startStandaloneDatabase() throws Exception 600 { 601 File dataDir = null; 603 604 if (dbDataDir == null) dataDir = ServerConfigLocator.locate().getServerDataDir(); 605 else dataDir = new File (dbDataDir); 606 607 File hypersoniDir = new File (dataDir, HYPERSONIC_DATA_DIR); 609 610 if (!hypersoniDir.exists()) 611 { 612 hypersoniDir.mkdirs(); 613 } 614 615 if (!hypersoniDir.isDirectory()) 616 { 617 throw new IOException ("Failed to create directory: " + hypersoniDir); 618 } 619 620 dbPath = new File (hypersoniDir, name); 621 622 String dbURL = JDBC_URL_PREFIX + getDatabasePath(); 623 624 connection = getConnection(dbURL); 626 } 627 628 631 private void startInMemoryDatabase() throws Exception 632 { 633 String dbURL = JDBC_URL_PREFIX + IN_MEMORY_DATABASE; 634 635 connection = getConnection(dbURL); 637 } 638 639 642 private void startRemoteDatabase() throws Exception 643 { 644 File dataDir = null; 646 if (dbDataDir == null) dataDir = ServerConfigLocator.locate().getServerDataDir(); 647 else dataDir = new File (dbDataDir); 648 649 File hypersoniDir = new File (dataDir, HYPERSONIC_DATA_DIR); 651 652 if (!hypersoniDir.exists()) 653 { 654 hypersoniDir.mkdirs(); 655 } 656 657 if (!hypersoniDir.isDirectory()) 658 { 659 throw new IOException ("Failed to create directory: " + hypersoniDir); 660 } 661 662 dbPath = new File (hypersoniDir, name); 663 664 serverThread = new Thread ("hypersonic-" + name) 666 { 667 public void run() 668 { 669 try 670 { 671 String [] args = 673 new String [] { 674 "-database", dbPath.toString(), 675 "-port", String.valueOf(port), 676 "-address", address, 677 "-silent", String.valueOf(silent), 678 "-trace", String.valueOf(trace), 679 "-no_system_exit", String.valueOf(no_system_exit), 680 }; 681 682 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 684 Class clazz = Class.forName(serverClass, true, cl); 685 Method main = clazz.getMethod("main", new Class [] { args.getClass() }); 686 main.invoke(null, new Object [] { args } ); 687 } 688 catch (Exception e) 689 { 690 log.error("Failed to start database", e); 691 } 692 } 693 }; 694 serverThread.start(); 695 } 696 697 700 private void stopStandaloneDatabase() throws Exception 701 { 702 String dbURL = JDBC_URL_PREFIX + getDatabasePath(); 703 704 Connection connection = getConnection(dbURL); 705 Statement statement = connection.createStatement(); 706 707 String shutdownCommand = this.shutdownCommand; 708 if (shutdownCommand == null) 709 { 710 shutdownCommand = DEFAULT_IN_PROCESS_SHUTDOWN_COMMAND; 711 } 712 713 statement.executeQuery(shutdownCommand); 714 this.connection = null; 715 log.info("Database standalone closed clean"); 716 } 717 718 721 private void stopInMemoryDatabase() throws Exception 722 { 723 String dbURL = JDBC_URL_PREFIX + IN_MEMORY_DATABASE; 724 725 Connection connection = getConnection(dbURL); 726 Statement statement = connection.createStatement(); 727 728 String shutdownCommand = this.shutdownCommand; 729 if (shutdownCommand == null) 730 { 731 shutdownCommand = DEFAULT_IN_MEMORY_SHUTDOWN_COMMAND; 732 } 733 734 statement.executeQuery(shutdownCommand); 735 this.connection = null; 736 log.info("Database in memory closed clean"); 737 } 738 739 742 private void stopRemoteDatabase() throws Exception 743 { 744 String connectHost = DEFAULT_ADDRESS.equals(address) ? "localhost" : address; 746 String dbURL = JDBC_URL_PREFIX + "hsql://" + connectHost + ":" + port; 747 748 Connection connection = getConnection(dbURL); 749 Statement statement = connection.createStatement(); 750 751 String shutdownCommand = this.shutdownCommand; 752 if (shutdownCommand == null) 753 { 754 shutdownCommand = DEFAULT_REMOTE_SHUTDOWN_COMMAND; 755 } 756 757 statement.executeQuery(shutdownCommand); 758 serverThread = null; 760 this.connection = null; 761 log.info("Database remote closed clean"); 762 } 763 764 771 private synchronized Connection getConnection(String dbURL) throws Exception 772 { 773 if (connection == null) 774 { 775 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 776 Class.forName(JDBC_DRIVER_CLASS, true, cl).newInstance(); 777 connection = DriverManager.getConnection(dbURL, user, password); 778 } 779 return connection; 780 } 781 782 } 783 | Popular Tags |