1 25 26 package com.rift.coad.daemon.hsqldb; 28 29 import java.rmi.Remote ; 31 import java.rmi.RemoteException ; 32 import java.net.InetAddress ; 33 34 import org.apache.log4j.Logger; 36 37 import org.hsqldb.Server; 39 import org.hsqldb.ServerConstants; 40 import org.hsqldb.DatabaseManager; 41 import org.hsqldb.persist.HsqlProperties; 42 43 import com.rift.coad.lib.bean.BeanRunnable; 45 import com.rift.coad.lib.configuration.Configuration; 46 import com.rift.coad.lib.configuration.ConfigurationFactory; 47 import com.rift.coad.lib.thread.ThreadStateMonitor; 48 49 54 public class HsqlDBEngineImpl implements HsqlDBEngine, BeanRunnable { 55 56 private final static int LOCK_INDEX = 0; 58 private final static String DB_HOSTNAME = "db_hostname"; 59 private final static String DB_PORT = "db_port"; 60 private final static String DB_PATH = "db_path"; 61 private final static String DB_NAME = "db_name"; 62 private final static String DEFAULT_DB_NAME = "coadunation"; 63 private final static String DB_SILENT = "db_silent"; 64 private final static boolean DEFAULT_DB_SILENT = true; 65 private final static String DB_TRACE = "db_trace"; 66 private final static boolean DEFAULT_DB_TRACE = false; 67 private final static String DB_TLS = "db_tls"; 68 private final static boolean DEFAULT_DB_TLS = false; 69 70 private Logger log = Logger.getLogger(HsqlDBEngineImpl.class.getName()); 72 private Server server = null; 73 private ThreadStateMonitor state = new ThreadStateMonitor(); 74 75 80 public HsqlDBEngineImpl() throws HsqlDBEngineException { 81 try { 82 server = new Server(); 83 HsqlProperties props = new HsqlProperties(); 84 85 Configuration config = ConfigurationFactory.getInstance(). 86 getConfig(HsqlDBEngineImpl.class); 87 88 props.setProperty(ServerConstants.SC_KEY_ADDRESS, config.getString( 90 DB_HOSTNAME,InetAddress.getLocalHost(). 91 getCanonicalHostName())); 92 props.setProperty(ServerConstants.SC_KEY_PORT, new Long (config.getLong( 93 DB_PORT,ServerConstants.SC_DEFAULT_HSQL_SERVER_PORT)). 94 toString()); 95 props.setProperty(ServerConstants.SC_KEY_DATABASE + "." + LOCK_INDEX, 96 config.getString(DB_PATH)); 97 props.setProperty(ServerConstants.SC_KEY_DBNAME + "." + LOCK_INDEX, 98 config.getString(DB_NAME,DEFAULT_DB_NAME)); 99 100 server.setProperties(props); 101 server.setSilent(config.getBoolean(DB_SILENT,DEFAULT_DB_SILENT)); 102 server.setTrace(config.getBoolean(DB_TRACE,DEFAULT_DB_TRACE)); 103 server.setTls(config.getBoolean(DB_TLS,DEFAULT_DB_TLS)); 104 server.setNoSystemExit(true); 105 server.setRestartOnShutdown(true); 106 } catch (Exception ex) { 107 throw new HsqlDBEngineException( 108 "Failed to init the HsqlDB Engine : " + ex.getMessage(),ex); 109 } 110 } 111 112 113 124 public String getAddress() throws RemoteException , HsqlDBEngineException { 125 try { 126 return server.getAddress(); 127 } catch (Exception ex) { 128 throw new HsqlDBEngineException("Failed to retrieve the address " + 129 "information : " + ex.getMessage(),ex); 130 } 131 } 132 133 134 163 public String getDatabaseName(int index, boolean asconfigured) throws 164 RemoteException , HsqlDBEngineException { 165 try { 166 return server.getDatabaseName(LOCK_INDEX,asconfigured); 167 } catch (Exception ex) { 168 throw new HsqlDBEngineException("Failed to retrieve the address " + 169 "information : " + ex.getMessage(),ex); 170 } 171 } 172 173 174 204 public String getDatabasePath(int index, boolean asconfigured) throws 205 RemoteException , HsqlDBEngineException { 206 try { 207 return server.getDatabasePath(LOCK_INDEX,asconfigured); 208 } catch (Exception ex) { 209 throw new HsqlDBEngineException("Failed to retrieve the database " + 210 "path : " + ex.getMessage(),ex); 211 } 212 } 213 214 215 223 public String getDatabaseType(int index) throws RemoteException , 224 HsqlDBEngineException { 225 try { 226 return server.getDatabaseType(LOCK_INDEX); 227 } catch (Exception ex) { 228 throw new HsqlDBEngineException("Failed to retrieve the database " + 229 "type : " + ex.getMessage(),ex); 230 } 231 } 232 233 234 246 public int getPort() throws RemoteException , HsqlDBEngineException { 247 try { 248 return server.getPort(); 249 } catch (Exception ex) { 250 throw new HsqlDBEngineException("Failed to retrieve the database " + 251 "port : " + ex.getMessage(),ex); 252 } 253 } 254 255 256 270 public String getProductName() throws RemoteException , 271 HsqlDBEngineException { 272 try { 273 return server.getProductName(); 274 } catch (Exception ex) { 275 throw new HsqlDBEngineException("Failed to retrieve the database " + 276 "product name : " + ex.getMessage(),ex); 277 } 278 } 279 280 281 295 public String getProductVersion() throws RemoteException , 296 HsqlDBEngineException { 297 try { 298 return server.getProductVersion(); 299 } catch (Exception ex) { 300 throw new HsqlDBEngineException("Failed to retrieve the database " + 301 "product version : " + ex.getMessage(),ex); 302 } 303 } 304 305 306 319 public String getProtocol() throws RemoteException , HsqlDBEngineException { 320 try { 321 return server.getProtocol(); 322 } catch (Exception ex) { 323 throw new HsqlDBEngineException("Failed to retrieve the database " + 324 "protocol : " + ex.getMessage(),ex); 325 } 326 } 327 328 329 341 public String getServerId() throws RemoteException , HsqlDBEngineException { 342 try { 343 return server.getServerId(); 344 } catch (Exception ex) { 345 throw new HsqlDBEngineException("Failed to retrieve the server " + 346 "id : " + ex.getMessage(),ex); 347 } 348 } 349 350 351 372 public int getState() throws RemoteException , HsqlDBEngineException { 373 try { 374 return server.getState(); 375 } catch (Exception ex) { 376 throw new HsqlDBEngineException("Failed to retrieve the state : " 377 + ex.getMessage(),ex); 378 } 379 } 380 381 382 396 public String getStateDescriptor() throws RemoteException , 397 HsqlDBEngineException { 398 try { 399 return server.getStateDescriptor(); 400 } catch (Exception ex) { 401 throw new HsqlDBEngineException("Failed to retrieve the state " + 402 "description : " + ex.getMessage(),ex); 403 } 404 } 405 406 407 420 public boolean isTls() throws RemoteException , HsqlDBEngineException { 421 try { 422 return server.isTls(); 423 } catch (Exception ex) { 424 throw new HsqlDBEngineException("Failed to retrieve the value of " + 425 "the TLs flag : " + ex.getMessage(),ex); 426 } 427 } 428 429 453 public boolean putPropertiesFromFile(String path) throws RemoteException , 454 HsqlDBEngineException{ 455 try { 456 return server.putPropertiesFromFile(path); 457 } catch (Exception ex) { 458 throw new HsqlDBEngineException("Failed to set the properties " + 459 "from the specified file : " + ex.getMessage(),ex); 460 } 461 } 462 463 464 486 public void putPropertiesFromString(String s) throws RemoteException , 487 HsqlDBEngineException { 488 try { 489 server.putPropertiesFromString(s); 490 } catch (Exception ex) { 491 throw new HsqlDBEngineException("Failed to set the properties " + 492 "from the string : " + ex.getMessage(),ex); 493 } 494 } 495 496 497 501 public void process() { 502 try { 503 server.start(); 504 while(!state.isTerminated()) { 505 state.monitor(); 506 } 507 log.info("process method exiting"); 508 } catch (Exception ex) { 509 log.error("Failed to start the server : " + ex.getMessage(), ex); 510 } 511 } 512 513 514 517 public void terminate() { 518 try { 519 server.shutdown(); 520 DatabaseManager.closeDatabases(0); 521 state.terminate(true); 522 log.info("Hsql has been shut down exiting"); 523 } catch (Exception ex) { 524 log.error("Failed to stop the logger : " + ex.getMessage(), ex); 525 } 526 } 527 } 528 | Popular Tags |