KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > jdbcserver > DefaultLocalBootable


1 /**
2  * com.mckoi.database.jdbcserver.DefaultLocalBootable 28 Mar 2002
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.jdbcserver;
26
27 //import com.mckoi.runtime.BootMain;
28
import com.mckoi.database.control.DBConfig;
29 import com.mckoi.database.control.DBSystem;
30 import com.mckoi.database.control.DBController;
31 import com.mckoi.database.control.DefaultDBConfig;
32 import com.mckoi.database.DatabaseSystem;
33 import com.mckoi.database.Database;
34 import com.mckoi.database.DatabaseException;
35 import com.mckoi.database.jdbc.LocalBootable;
36 import com.mckoi.database.jdbc.DatabaseCallBack;
37 import com.mckoi.database.jdbc.DatabaseInterface;
38 import com.mckoi.database.jdbc.QueryResponse;
39 import com.mckoi.database.jdbc.ResultPart;
40 import com.mckoi.database.jdbc.SQLQuery;
41
42 import java.io.File JavaDoc;
43 import java.sql.SQLException JavaDoc;
44
45 /**
46  * A bootable object that filters through to a JDBCDatabaseInterface but
47  * is thread-safe and multi-threaded. This is to be used when you have a
48  * local JDBC Client accessing a stand-alone database.
49  *
50  * @author Tobias Downer
51  */

52
53 public class DefaultLocalBootable implements LocalBootable {
54
55   /**
56    * Set to true if the database is booted.
57    */

58   private boolean booted = false;
59
60   /**
61    * Set to true when this interface is active.
62    */

63   private boolean active = false;
64
65   /**
66    * The local DBSystem database object.
67    */

68   private DBSystem dbsys;
69
70   /**
71    * The connection id. This is incremented by 1 each time an
72    * interface connects to the local JVM.
73    */

74   private int connect_id = 0;
75
76   /**
77    * The number of connections that are current open.
78    */

79   private int open_connections = 0;
80
81   /**
82    * The connection lock object.
83    */

84   private Object JavaDoc connection_lock = new Object JavaDoc();
85
86   /**
87    * Creates and boots a local database with the given configuration. This
88    * is implemented from LocalBootable.
89    *
90    * @param config the configuration variables.
91    */

92   public DatabaseInterface create(String JavaDoc username, String JavaDoc password,
93                                   DBConfig config) throws SQLException JavaDoc {
94
95     if (username.equals("") || password.equals("")) {
96       throw new SQLException JavaDoc("Username and Password must both be set.");
97     }
98
99     if (!booted) {
100       // Local connections are formatted as;
101
// 'Local/[type]/[connect_id]'
102
String JavaDoc host_string = "Local/Create/";
103
104       // Create the DBSystem and bind it to a DatabaseInterface.
105
DBController controller = DBController.getDefault();
106       dbsys = controller.createDatabase(config, username, password);
107       DatabaseInterface db_interface =
108          new LocalJDBCDatabaseInterface(dbsys.getDatabase(), host_string);
109
110       booted = true;
111       ++open_connections;
112       active = true;
113
114       return db_interface;
115     }
116
117     throw new SQLException JavaDoc("Database is already created.");
118
119   }
120
121   /**
122    * Boots the local database with the given configuration. This is
123    * implemented from LocalBootable.
124    *
125    * @param config the configuration variables.
126    */

127   public DatabaseInterface boot(DBConfig config) throws SQLException JavaDoc {
128     if (!booted) {
129       // Local connections are formatted as;
130
// 'Local/[type]/[connect_id]'
131
String JavaDoc host_string = "Local/Boot/";
132
133       // Start the DBSystem and bind it to a DatabaseInterface.
134
DBController controller = DBController.getDefault();
135       dbsys = controller.startDatabase(config);
136       DatabaseInterface db_interface =
137          new LocalJDBCDatabaseInterface(dbsys.getDatabase(), host_string);
138
139       booted = true;
140       ++open_connections;
141       active = true;
142
143       return db_interface;
144
145     }
146     else {
147       throw new SQLException JavaDoc("Database was booted more than once.");
148     }
149   }
150
151   /**
152    * Attempts to test if the database exists or not. Returns true if the
153    * database exists.
154    *
155    * @param config the configuration variables.
156    */

157   public boolean checkExists(DBConfig config) throws SQLException JavaDoc {
158     if (!booted) {
159       DBController controller = DBController.getDefault();
160       return controller.databaseExists(config);
161     }
162     else {
163       throw new SQLException JavaDoc("The database is already booted.");
164     }
165   }
166
167   /**
168    * Returns true if a database has successfully been booted in this JVM. If
169    * a database hasn't been botted then it returns false.
170    */

171   public boolean isBooted() throws SQLException JavaDoc {
172     return booted;
173   }
174
175   /**
176    * Creates a new LocalDatabaseInterface that is connected to the database
177    * currently running in this VM. Calling this method must guarentee that
178    * either 'boot' or 'create' has been called in this VM beforehand.
179    */

180   public DatabaseInterface connectToJVM() throws SQLException JavaDoc {
181     if (booted) {
182
183       // Local connections are formatted as;
184
// 'Local/[type]/[connect_id]'
185
String JavaDoc host_string = "Local/Connection/" + connect_id;
186
187       // Create a DatabaseInterface,
188
DatabaseInterface db_interface =
189           new LocalJDBCDatabaseInterface(dbsys.getDatabase(), host_string);
190
191       ++connect_id;
192       ++open_connections;
193       active = true;
194
195       return db_interface;
196
197     }
198     else {
199       throw new SQLException JavaDoc("The database is not started.");
200     }
201
202   }
203
204   // ---------- Inner classes ----------
205

206   /**
207    * A local implementation of JDBCDatabaseInterface that will dispose the
208    * parent LocalBootable object when the last open connection is disposed.
209    */

210   private class LocalJDBCDatabaseInterface extends JDBCDatabaseInterface {
211
212     boolean closed = false;
213
214     public LocalJDBCDatabaseInterface(Database database, String JavaDoc host_string) {
215       super(database, host_string);
216     }
217
218     // ---------- Overwritten from JDBCDatabaseInterface ----------
219

220     public void dispose() throws SQLException JavaDoc {
221       if (!closed) {
222         super.dispose();
223
224         --open_connections;
225
226         // When all connections are closed, shut down...
227
if (open_connections <= 0) {
228           // When the local database interface is disposed, we must shut down
229
// the database system.
230
dbsys.close();
231           active = false;
232           booted = false;
233           dbsys = null;
234         }
235         closed = true;
236       }
237
238     }
239
240     // ---------- Clean up ----------
241

242     public void finalize() throws Throwable JavaDoc {
243       super.finalize();
244       dispose();
245     }
246
247   }
248
249 }
250
Popular Tags