KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > hsql > HsqldbJob


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.hsql;
5
6 import java.util.HashMap JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import org.hsqldb.Server;
11 import org.hsqldb.ServerConstants;
12 import org.hsqldb.persist.HsqlProperties;
13
14 /**
15  * @oddjob.description Start an instance of the HSQL
16  * Database engine. For more information on the properties please see
17  * <a HREF="http://hsqldb.org">http://hsqldb.org</a>
18  *
19  * @oddjob.example
20  *
21  * Please see the {@link org.oddjob.sql.SqlJob} examples.
22  *
23  * @author Rob Gordon.
24  */

25 public class HsqldbJob {
26
27     class ServerWithStateNotify extends Server {
28         synchronized protected void setState(int state) {
29             super.setState(state);
30                 notifyAll();
31         }
32     }
33     
34     /**
35      * @oddjob.property
36      * @oddjob.description A name, can be any text.
37      * @oddjob.required No.
38      */

39     private String JavaDoc name;
40     
41     /**
42      * @oddjob.property database
43      * @oddjob.description A list of name/value pairs as a
44      * {@link org.oddjob.values.types.MapType}
45      * type object which are the database name and the database path.
46      * Hsqldb supports up to 10 databases per server instance.
47      * @oddjob.required No.
48      */

49     private Map JavaDoc databases = new HashMap JavaDoc();
50     
51     
52     /**
53      * @oddjob.property
54      * @oddjob.description The server hsql properties. For more information please
55      * see the hsqldb documentation.
56      * @oddjob.required No.
57      */

58     private HsqlProperties properties;
59     
60     /** The server */
61     private Server server;
62     
63     /**
64      * Get the name.
65      *
66      * @return The name.
67      */

68     public String JavaDoc getName() {
69         return name;
70     }
71     
72     /**
73      * Set the name.
74      *
75      * @param name The name.
76      */

77     public void setName(String JavaDoc name) {
78         this.name = name;
79     }
80     
81     /**
82      * Get the database path for a give name.
83      *
84      * @param name The database path.
85      * @return The database path.
86      */

87     public String JavaDoc getDatabase(String JavaDoc name) {
88         return (String JavaDoc) databases.get(name);
89     }
90     
91     /**
92      * Set the database name.
93      *
94      * @param name The database name.
95      */

96     public void setDatabase(String JavaDoc name, String JavaDoc path) {
97         if (name == null) {
98             throw new NullPointerException JavaDoc("Database name can not be null");
99         }
100         this.databases.put(name, path);
101     }
102     
103     /**
104      * Set a property.
105      *
106      * @param name The property name.
107      * @param value The property value.
108      */

109     public void setProperty(String JavaDoc name, String JavaDoc value) {
110         if (properties == null) {
111             properties = new HsqlProperties();
112         }
113         properties.setProperty(name, value);
114     }
115     
116     /**
117      * Get a property.
118      *
119      * @param name The property name.
120      */

121     public String JavaDoc getProperty(String JavaDoc name) {
122         if (properties == null) {
123             return null;
124         }
125         return properties.getProperty(name);
126     }
127     
128     /**
129      * Start the server.
130      *
131      */

132     public void start() {
133
134         server = new ServerWithStateNotify();
135         int dbCount = 0;
136         for (Iterator JavaDoc it = databases.entrySet().iterator(); it.hasNext(); ++dbCount) {
137             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
138             server.setDatabaseName(dbCount, (String JavaDoc) entry.getKey());
139             server.setDatabasePath(dbCount, (String JavaDoc) entry.getValue());
140         }
141         
142         if (properties != null) {
143             server.setProperties(properties);
144         }
145         server.start();
146         synchronized (server) {
147             while (server.getState() != ServerConstants.SERVER_STATE_ONLINE) {
148                 try {
149                     server.wait();
150                 } catch (InterruptedException JavaDoc e) {
151                     break;
152                 }
153             }
154         }
155     }
156     
157     /**
158      * Stop the server.
159      *
160      */

161     public void stop() {
162         server.stop();
163         synchronized (server) {
164             while (server.getState() != ServerConstants.SERVER_STATE_SHUTDOWN) {
165                 try {
166                     server.wait();
167                 } catch (InterruptedException JavaDoc e) {
168                     break;
169                 }
170             }
171         }
172         server= null;
173         properties = null;
174     }
175     
176     /*
177      * (non-Javadoc)
178      * @see java.lang.Object#toString()
179      */

180     public String JavaDoc toString() {
181         if (name == null) {
182             return "Hsqldb server";
183         }
184         return name;
185     }
186 }
187
Popular Tags