KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > control > DBController


1 /**
2  * com.mckoi.dbcontrol.DBController 26 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.control;
26
27 import com.mckoi.database.Database;
28 import com.mckoi.database.DatabaseSystem;
29 import com.mckoi.database.DatabaseException;
30 import com.mckoi.debug.*;
31 import com.mckoi.util.LogWriter;
32
33 import java.io.File JavaDoc;
34 import java.io.Writer JavaDoc;
35 import java.io.PrintWriter JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.util.Date JavaDoc;
38
39 /**
40  * An object that provides methods for creating and controlling database
41  * systems in the current JVM.
42  *
43  * @author Tobias Downer
44  */

45
46 public final class DBController {
47
48   /**
49    * This object can not be constructed outside of this package.
50    */

51   DBController() {
52   }
53
54   /**
55    * Returns true if a Mckoi database exists in the given directory of the
56    * file system, otherwise returns false if the path doesn't contain a
57    * database.
58    * <p>
59    * The path string must be formatted using Unix '/' deliminators as
60    * directory separators.
61    *
62    * @param config the configuration of the database to check the existence
63    * of.
64    * @return true if a database exists at the given path, false otherwise.
65    */

66   public boolean databaseExists(DBConfig config) {
67     Database database = createDatabase(config);
68     boolean b = database.exists();
69     database.getSystem().dispose();
70     return b;
71   }
72
73   /**
74    * Creates a database in the local JVM (and filesystem) given the
75    * configuration in DBConfig and returns a DBSystem object. When this
76    * method returns, the database created will be up and running providing
77    * there was no failure during the database creation process.
78    * <p>
79    * A failure might happen because the database path does not exist.
80    *
81    * @param admin_user the username of the administrator for the new database.
82    * @param admin_pass the password of the administrator for the new database.
83    * @param config the configuration of the database to create and start in the
84    * local JVM.
85    * @return the DBSystem object used to access the database created.
86    */

87   public DBSystem createDatabase(DBConfig config,
88                                  String JavaDoc admin_user, String JavaDoc admin_pass) {
89
90     // Create the Database object with this configuration.
91
Database database = createDatabase(config);
92     DatabaseSystem system = database.getSystem();
93
94     // Create the database.
95
try {
96       database.create(admin_user, admin_pass);
97       database.init();
98     }
99     catch (DatabaseException e) {
100       system.Debug().write(Lvl.ERROR, this, "Database create failed");
101       system.Debug().writeException(e);
102       throw new RuntimeException JavaDoc(e.getMessage());
103     }
104
105     // Return the DBSystem object for the newly created database.
106
return new DBSystem(this, config, database);
107
108   }
109
110   /**
111    * Starts a database in the local JVM given the configuration in DBConfig
112    * and returns a DBSystem object. When this method returns, the database
113    * will be up and running providing there was no failure to initialize the
114    * database.
115    * <p>
116    * A failure might happen if the database does not exist in the path given
117    * in the configuration.
118    *
119    * @param config the configuration of the database to start in the local
120    * JVM.
121    * @return the DBSystem object used to access the database started.
122    */

123   public DBSystem startDatabase(DBConfig config) {
124
125     // Create the Database object with this configuration.
126
Database database = createDatabase(config);
127     DatabaseSystem system = database.getSystem();
128
129     // First initialise the database
130
try {
131       database.init();
132     }
133     catch (DatabaseException e) {
134       system.Debug().write(Lvl.ERROR, this, "Database init failed");
135       system.Debug().writeException(e);
136       throw new RuntimeException JavaDoc(e.getMessage());
137     }
138
139     // Return the DBSystem object for the newly created database.
140
return new DBSystem(this, config, database);
141
142   }
143
144
145   // ---------- Static methods ----------
146

147   /**
148    * Creates a Database object for the given DBConfig configuration.
149    */

150   private static Database createDatabase(DBConfig config) {
151
152     DatabaseSystem system = new DatabaseSystem();
153
154     // Initialize the DatabaseSystem first,
155
// ------------------------------------
156

157     // This will throw an Error exception if the database system has already
158
// been initialized.
159
system.init(config);
160
161     // Start the database class
162
// ------------------------
163

164     // Note, currently we only register one database, and it is named
165
// 'DefaultDatabase'.
166
Database database = new Database(system, "DefaultDatabase");
167
168     // Start up message
169
system.Debug().write(Lvl.MESSAGE, DBController.class,
170                          "Starting Database Server");
171
172     return database;
173   }
174
175   /**
176    * Returns the static controller for this JVM.
177    */

178   public static DBController getDefault() {
179     return VM_DB_CONTROLLER;
180   }
181
182   /**
183    * The static DBController object.
184    */

185   private final static DBController VM_DB_CONTROLLER = new DBController();
186
187 }
188
Popular Tags