KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > daemon > hsqldb > HsqlDBEngineImpl


1 /*
2  * HsqlDbEngineDaemon: The hsql db engine daemon
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * HsqlDBEngineImpl.java
20  *
21  * The implementation of the HsqlDBEngine daemon.
22  *
23  * $Revision: 1.2 $
24  */

25
26 // package path
27
package com.rift.coad.daemon.hsqldb;
28
29 // java imports
30
import java.rmi.Remote JavaDoc;
31 import java.rmi.RemoteException JavaDoc;
32 import java.net.InetAddress JavaDoc;
33
34 // logging import
35
import org.apache.log4j.Logger;
36
37 // hsqldb imports
38
import org.hsqldb.Server;
39 import org.hsqldb.ServerConstants;
40 import org.hsqldb.DatabaseManager;
41 import org.hsqldb.persist.HsqlProperties;
42
43 // coadunation imports
44
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 /**
50  * The implementation of the HsqlDBEngine daemon.
51  *
52  * @author Brett Chaldecott
53  */

54 public class HsqlDBEngineImpl implements HsqlDBEngine, BeanRunnable {
55     
56     // index value
57
private final static int LOCK_INDEX = 0;
58     private final static String JavaDoc DB_HOSTNAME = "db_hostname";
59     private final static String JavaDoc DB_PORT = "db_port";
60     private final static String JavaDoc DB_PATH = "db_path";
61     private final static String JavaDoc DB_NAME = "db_name";
62     private final static String JavaDoc DEFAULT_DB_NAME = "coadunation";
63     private final static String JavaDoc DB_SILENT = "db_silent";
64     private final static boolean DEFAULT_DB_SILENT = true;
65     private final static String JavaDoc DB_TRACE = "db_trace";
66     private final static boolean DEFAULT_DB_TRACE = false;
67     private final static String JavaDoc DB_TLS = "db_tls";
68     private final static boolean DEFAULT_DB_TLS = false;
69     
70     // private member variables
71
private Logger log = Logger.getLogger(HsqlDBEngineImpl.class.getName());
72     private Server server = null;
73     private ThreadStateMonitor state = new ThreadStateMonitor();
74     
75     /**
76      * Creates a new instance of HsqlDBEngineImpl
77      *
78      * @exception HsqlDBEngineException
79      */

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             // set the properties
89
props.setProperty(ServerConstants.SC_KEY_ADDRESS, config.getString(
90                     DB_HOSTNAME,InetAddress.getLocalHost().
91                     getCanonicalHostName()));
92             props.setProperty(ServerConstants.SC_KEY_PORT, new Long JavaDoc(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 JavaDoc ex) {
107             throw new HsqlDBEngineException(
108                     "Failed to init the HsqlDB Engine : " + ex.getMessage(),ex);
109         }
110     }
111     
112     
113     /**
114      * Retrieves, in string form, this server's host address.
115      *
116      * @return this server's host address
117      *
118      * @jmx.managed-attribute
119      * access="read-write"
120      * description="Host InetAddress"
121      * @exception RemoteException
122      * @exception HsqlDBEngineException
123      */

124     public String JavaDoc getAddress() throws RemoteException JavaDoc, HsqlDBEngineException {
125         try {
126             return server.getAddress();
127         } catch (Exception JavaDoc ex) {
128             throw new HsqlDBEngineException("Failed to retrieve the address " +
129                     "information : " + ex.getMessage(),ex);
130         }
131     }
132     
133     
134     /**
135      * Retrieves the url alias (network name) of the i'th database
136      * that this Server hosts.
137      *
138      * @param index the index of the url alias upon which to report
139      * @param asconfigured if true, report the configured value, else
140      * the live value
141      * @return the url alias component of the i'th database
142      * that this Server hosts, or null if no such name exists.
143      *
144      * @jmx.managed-operation
145      * impact="INFO"
146      * description="url alias component of the i'th hosted Database"
147      *
148      * @jmx.managed-operation-parameter
149      * name="index"
150      * type="int"
151      * position="0"
152      * description="This Server's index for the hosted Database"
153      *
154      * @jmx.managed-operation-parameter
155      * name="asconfigured"
156      * type="boolean"
157      * position="1"
158      * description="if true, the configured value, else the live value"
159      *
160      * @exception RemoteException
161      * @exception HsqlDBEngineException
162      */

163     public String JavaDoc getDatabaseName(int index, boolean asconfigured) throws
164             RemoteException JavaDoc, HsqlDBEngineException {
165         try {
166             return server.getDatabaseName(LOCK_INDEX,asconfigured);
167         } catch (Exception JavaDoc ex) {
168             throw new HsqlDBEngineException("Failed to retrieve the address " +
169                     "information : " + ex.getMessage(),ex);
170         }
171     }
172     
173     
174     /**
175      * Retrieves the HSQLDB path descriptor (uri) of the i'th
176      * Database that this Server hosts.
177      *
178      * @param index the index of the uri upon which to report
179      * @param asconfigured if true, report the configured value, else
180      * the live value
181      * @return the HSQLDB database path descriptor of the i'th database
182      * that this Server hosts, or null if no such path descriptor
183      * exists
184      *
185      * @jmx.managed-operation
186      * impact="INFO"
187      * description="For i'th hosted database"
188      *
189      * @jmx.managed-operation-parameter
190      * name="index"
191      * type="int"
192      * position="0"
193      * description="This Server's index for the hosted Database"
194      *
195      * @jmx.managed-operation-parameter
196      * name="asconfigured"
197      * type="boolean"
198      * position="1"
199      * description="if true, the configured value, else the live value"
200      *
201      * @exception RemoteException
202      * @exception HsqlDBEngineException
203      */

204     public String JavaDoc getDatabasePath(int index, boolean asconfigured) throws
205             RemoteException JavaDoc, HsqlDBEngineException {
206         try {
207             return server.getDatabasePath(LOCK_INDEX,asconfigured);
208         } catch (Exception JavaDoc ex) {
209             throw new HsqlDBEngineException("Failed to retrieve the database " +
210                     "path : " + ex.getMessage(),ex);
211         }
212     }
213     
214     
215     /**
216      * This method returns the HsqlDB type
217      *
218      * @return A string containing the type of this db.
219      * @param index The index of this type.
220      * @exception RemoteException
221      * @exception HsqlDBEngineException
222      */

223     public String JavaDoc getDatabaseType(int index) throws RemoteException JavaDoc,
224             HsqlDBEngineException {
225         try {
226             return server.getDatabaseType(LOCK_INDEX);
227         } catch (Exception JavaDoc ex) {
228             throw new HsqlDBEngineException("Failed to retrieve the database " +
229                     "type : " + ex.getMessage(),ex);
230         }
231     }
232     
233     
234     /**
235      * Retrieves this server's host port.
236      *
237      * @return this server's host port
238      *
239      * @jmx.managed-attribute
240      * access="read-write"
241      * description="At which ServerSocket listens for connections"
242      *
243      * @exception RemoteException
244      * @exception HsqlDBEngineException
245      */

246     public int getPort() throws RemoteException JavaDoc, HsqlDBEngineException {
247         try {
248             return server.getPort();
249         } catch (Exception JavaDoc ex) {
250             throw new HsqlDBEngineException("Failed to retrieve the database " +
251                     "port : " + ex.getMessage(),ex);
252         }
253     }
254     
255     
256     /**
257      * Retrieves this server's product name. <p>
258      *
259      * Typically, this will be something like: "HSQLDB xxx server".
260      *
261      * @return the product name of this server
262      *
263      * @jmx.managed-attribute
264      * access="read-only"
265      * description="Of Server"
266      *
267      * @exception RemoteException
268      * @exception HsqlDBEngineException
269      */

270     public String JavaDoc getProductName() throws RemoteException JavaDoc,
271             HsqlDBEngineException {
272         try {
273             return server.getProductName();
274         } catch (Exception JavaDoc ex) {
275             throw new HsqlDBEngineException("Failed to retrieve the database " +
276                     "product name : " + ex.getMessage(),ex);
277         }
278     }
279     
280     
281     /**
282      * Retrieves the server's product version, as a String. <p>
283      *
284      * Typically, this will be something like: "1.x.x" or "2.x.x" and so on.
285      *
286      * @return the product version of the server
287      *
288      * @jmx.managed-attribute
289      * access="read-only"
290      * description="Of Server"
291      *
292      * @exception RemoteException
293      * @exception HsqlDBEngineException
294      */

295     public String JavaDoc getProductVersion() throws RemoteException JavaDoc,
296             HsqlDBEngineException {
297         try {
298             return server.getProductVersion();
299         } catch (Exception JavaDoc ex) {
300             throw new HsqlDBEngineException("Failed to retrieve the database " +
301                     "product version : " + ex.getMessage(),ex);
302         }
303     }
304     
305     
306     /**
307      * Retrieves a string respresentaion of the network protocol
308      * this server offers, typically one of 'HTTP', HTTPS', 'HSQL' or 'HSQLS'.
309      *
310      * @return string respresentation of this server's protocol
311      *
312      * @jmx.managed-attribute
313      * access="read-only"
314      * description="Used to handle connections"
315      *
316      * @exception RemoteException
317      * @exception HsqlDBEngineException
318      */

319     public String JavaDoc getProtocol() throws RemoteException JavaDoc, HsqlDBEngineException {
320         try {
321             return server.getProtocol();
322         } catch (Exception JavaDoc ex) {
323             throw new HsqlDBEngineException("Failed to retrieve the database " +
324                     "protocol : " + ex.getMessage(),ex);
325         }
326     }
327     
328     
329     /**
330      * Retrieves a String identifying this Server object.
331      *
332      * @return a String identifying this Server object
333      *
334      * @jmx.managed-attribute
335      * access="read-only"
336      * description="Identifying Server"
337      *
338      * @exception RemoteException
339      * @exception HsqlDBEngineException
340      */

341     public String JavaDoc getServerId() throws RemoteException JavaDoc, HsqlDBEngineException {
342         try {
343             return server.getServerId();
344         } catch (Exception JavaDoc ex) {
345             throw new HsqlDBEngineException("Failed to retrieve the server " +
346                     "id : " + ex.getMessage(),ex);
347         }
348     }
349     
350     
351     /**
352      * Retrieves current state of this server in numerically coded form. <p>
353      *
354      * Typically, this will be one of: <p>
355      *
356      * <ol>
357      * <li>ServerProperties.SERVER_STATE_ONLINE (1)
358      * <li>ServerProperties.SERVER_STATE_OPENING (4)
359      * <li>ServerProperties.SERVER_STATE_CLOSING (8)
360      * <li>ServerProperties.SERVER_STATE_SHUTDOWN (16)
361      * </ol>
362      *
363      * @return this server's state code.
364      *
365      * @jmx.managed-attribute
366      * access="read-only"
367      * description="1:ONLINE 4:OPENING 8:CLOSING, 16:SHUTDOWN"
368      *
369      * @exception RemoteException
370      * @exception HsqlDBEngineException
371      */

372     public int getState() throws RemoteException JavaDoc, HsqlDBEngineException {
373         try {
374             return server.getState();
375         } catch (Exception JavaDoc ex) {
376             throw new HsqlDBEngineException("Failed to retrieve the state : "
377                     + ex.getMessage(),ex);
378         }
379     }
380     
381     
382     /**
383      * Retrieves a character sequence describing this server's current state,
384      * including the message of the last exception, if there is one and it
385      * is still in context.
386      *
387      * @return this server's state represented as a character sequence.
388      *
389      * @jmx.managed-attribute
390      * access="read-only"
391      * description="State as string"
392      *
393      * @exception RemoteException
394      * @exception HsqlDBEngineException
395      */

396     public String JavaDoc getStateDescriptor() throws RemoteException JavaDoc,
397             HsqlDBEngineException {
398         try {
399             return server.getStateDescriptor();
400         } catch (Exception JavaDoc ex) {
401             throw new HsqlDBEngineException("Failed to retrieve the state " +
402                     "description : " + ex.getMessage(),ex);
403         }
404     }
405     
406     
407     /**
408      * Retrieves whether the use of secure sockets was requested in the
409      * server properties.
410      *
411      * @return if true, secure sockets are requested, else not
412      *
413      * @jmx.managed-attribute
414      * access="read-write"
415      * description="Use TLS/SSL sockets?"
416      *
417      * @exception RemoteException
418      * @exception HsqlDBEngineException
419      */

420     public boolean isTls() throws RemoteException JavaDoc, HsqlDBEngineException {
421         try {
422             return server.isTls();
423         } catch (Exception JavaDoc ex) {
424             throw new HsqlDBEngineException("Failed to retrieve the value of " +
425                     "the TLs flag : " + ex.getMessage(),ex);
426         }
427     }
428     
429     /**
430      * Attempts to put properties from the file
431      * with the specified path. The file
432      * extension '.properties' is implicit and should not
433      * be included in the path specification.
434      *
435      * @param path the path of the desired properties file, without the
436      * '.properties' file extension
437      * @throws RuntimeException if this server is running
438      * @return true if the indicated file was read sucessfully, else false
439      *
440      * @jmx.managed-operation
441      * impact="ACTION"
442      * description="Reads in properties"
443      *
444      * @jmx.managed-operation-parameter
445      * name="path"
446      * type="java.lang.String"
447      * position="0"
448      * description="(optional) returns false if path is empty"
449      *
450      * @exception RemoteException
451      * @exception HsqlDBEngineException
452      */

453     public boolean putPropertiesFromFile(String JavaDoc path) throws RemoteException JavaDoc,
454             HsqlDBEngineException{
455         try {
456             return server.putPropertiesFromFile(path);
457         } catch (Exception JavaDoc ex) {
458             throw new HsqlDBEngineException("Failed to set the properties " +
459                     "from the specified file : " + ex.getMessage(),ex);
460         }
461     }
462     
463     
464     /**
465      * Puts properties from the supplied string argument. The relevant
466      * key value pairs are the same as those for the (web)server.properties
467      * file format, except that the 'server.' prefix should not be specified.
468      *
469      * @param s semicolon-delimited key=value pair string,
470      * e.g. k1=v1;k2=v2;k3=v3...
471      * @throws RuntimeException if this server is running
472      *
473      * @jmx.managed-operation
474      * impact="ACTION"
475      * description="'server.' key prefix automatically supplied"
476      *
477      * @jmx.managed-operation-parameter
478      * name="s"
479      * type="java.lang.String"
480      * position="0"
481      * description="semicolon-delimited key=value pairs"
482      *
483      * @exception RemoteException
484      * @exception HsqlDBEngineException
485      */

486     public void putPropertiesFromString(String JavaDoc s) throws RemoteException JavaDoc,
487             HsqlDBEngineException {
488         try {
489             server.putPropertiesFromString(s);
490         } catch (Exception JavaDoc ex) {
491             throw new HsqlDBEngineException("Failed to set the properties " +
492                     "from the string : " + ex.getMessage(),ex);
493         }
494     }
495     
496     
497     /**
498      * This method will be called to perform the processing. This method
499      * replaces the traditional run method.
500      */

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 JavaDoc ex) {
509             log.error("Failed to start the server : " + ex.getMessage(), ex);
510         }
511     }
512     
513     
514     /**
515      * This method is called to soft terminate the processing thread.
516      */

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 JavaDoc ex) {
524             log.error("Failed to stop the logger : " + ex.getMessage(), ex);
525         }
526     }
527 }
528
Popular Tags