1 package org.jfox.hsql; 2 3 import java.io.File ; 4 5 import org.jfox.ioc.common.AbstractService; 6 import org.jfox.ioc.ext.ActiveComponent; 7 import org.jfox.ioc.ext.ManagableComponent; 8 9 10 13 14 public class HsqlDBService extends AbstractService implements ActiveComponent, ManagableComponent { 15 16 19 private String storage = "db"; 20 21 24 private String name = "default"; 25 26 29 private int port = org.hsqldb.ServerConstants.SC_DEFAULT_HSQL_SERVER_PORT; 30 31 org.hsqldb.Server server = new org.hsqldb.Server(); 32 33 protected void doInit() throws Exception { 34 File dbpath = new File (context.getModuleDir(), storage); 35 if(!dbpath.exists()) { 36 dbpath.mkdirs(); 37 } 38 39 if(!dbpath.isDirectory()) { 40 logger.error("Failed to create db directory: " + dbpath); 41 return; 42 } 43 File db = new File (dbpath, name); 44 server.setLogWriter(null); 45 server.setSilent(true); 47 server.setNoSystemExit(true); 48 server.setPort(port); 49 50 51 server.setDatabasePath(0, db.toURL().toString()); 52 } 53 54 protected void doStart() throws Exception { 55 server.start(); 56 } 57 58 protected void doStop() throws Exception { 59 server.stop(); 60 } 61 62 protected void doDestroy() throws Exception { 63 64 } 65 66 public void setDatabase(final String name) { 67 this.name = name; 68 } 69 70 public String getDatabase() { 71 return name; 72 } 73 74 public void setPort(final int port) { 75 this.port = port; 76 } 77 78 public int getPort() { 79 return port; 80 } 81 82 public String getDatabasePath() { 83 return new File (context.getModuleDir(), storage).toString(); 84 } 85 86 public void run() { 87 88 } 89 90 93 public void startDatabaseManager() { 94 new Thread () { 96 public void run() { 97 try { 98 String driver = "org.hsqldb.jdbcDriver"; 99 String [] args; 100 args = new String []{ 101 "-driver", driver, 102 "-url", "jdbc:hsqldb:hsql://localhost:" + port, 103 "-user", "sa", 104 "-password", "", 105 "-dir", getDatabasePath(), 106 "-noexit" 107 }; 108 org.hsqldb.util.DatabaseManagerSwing.main(args); 109 } 110 catch(Exception e) { 111 logger.error("Failed to start database manager", e); 112 } 113 } 114 }.start(); 115 } 116 117 118 public static void main(String [] args) { 119 120 } 121 } 122 123 | Popular Tags |