KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > hsql > HsqlDBService


1 package org.jfox.hsql;
2
3 import java.io.File JavaDoc;
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 /**
11  * @author <a HREF="mailto:yy.young@gmail.com">Young Yang</a>
12  */

13
14 public class HsqlDBService extends AbstractService implements ActiveComponent, ManagableComponent {
15
16     /**
17      * db strage directory
18      */

19     private String JavaDoc storage = "db";
20
21     /**
22      * db name
23      */

24     private String JavaDoc name = "default";
25
26     /**
27      * Default port.
28      */

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 JavaDoc {
34         File JavaDoc dbpath = new File JavaDoc(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 JavaDoc db = new File JavaDoc(dbpath, name);
44         server.setLogWriter(null);
45 // server.setErrWriter(null);
46
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 JavaDoc {
55         server.start();
56     }
57
58     protected void doStop() throws Exception JavaDoc {
59         server.stop();
60     }
61
62     protected void doDestroy() throws Exception JavaDoc {
63
64     }
65
66     public void setDatabase(final String JavaDoc name) {
67         this.name = name;
68     }
69
70     public String JavaDoc 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 JavaDoc getDatabasePath() {
83         return new File JavaDoc(context.getModuleDir(), storage).toString();
84     }
85
86     public void run() {
87
88     }
89
90     /**
91      * start of DatabaseManager accesible from the http://localhost:8082
92      */

93     public void startDatabaseManager() {
94         // Start DBManager in new thread
95
new Thread JavaDoc() {
96             public void run() {
97                 try {
98                     String JavaDoc driver = "org.hsqldb.jdbcDriver";
99                     String JavaDoc[] args;
100                     args = new String JavaDoc[]{
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 JavaDoc e) {
111                     logger.error("Failed to start database manager", e);
112                 }
113             }
114         }.start();
115     }
116
117
118     public static void main(String JavaDoc[] args) {
119
120     }
121 }
122
123
Popular Tags