001: /**
002: * com.mckoi.dbcontrol.DBController 26 Mar 2002
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.database.control;
024:
025: import com.mckoi.database.Database;
026: import com.mckoi.database.DatabaseSystem;
027: import com.mckoi.database.DatabaseException;
028: import com.mckoi.debug.*;
029: import com.mckoi.util.LogWriter;
030:
031: import java.io.File;
032: import java.io.Writer;
033: import java.io.PrintWriter;
034: import java.io.IOException;
035: import java.util.Date;
036:
037: /**
038: * An object that provides methods for creating and controlling database
039: * systems in the current JVM.
040: *
041: * @author Tobias Downer
042: */
043:
044: public final class DBController {
045:
046: /**
047: * This object can not be constructed outside of this package.
048: */
049: DBController() {
050: }
051:
052: /**
053: * Returns true if a Mckoi database exists in the given directory of the
054: * file system, otherwise returns false if the path doesn't contain a
055: * database.
056: * <p>
057: * The path string must be formatted using Unix '/' deliminators as
058: * directory separators.
059: *
060: * @param config the configuration of the database to check the existence
061: * of.
062: * @return true if a database exists at the given path, false otherwise.
063: */
064: public boolean databaseExists(DBConfig config) {
065: Database database = createDatabase(config);
066: boolean b = database.exists();
067: database.getSystem().dispose();
068: return b;
069: }
070:
071: /**
072: * Creates a database in the local JVM (and filesystem) given the
073: * configuration in DBConfig and returns a DBSystem object. When this
074: * method returns, the database created will be up and running providing
075: * there was no failure during the database creation process.
076: * <p>
077: * A failure might happen because the database path does not exist.
078: *
079: * @param admin_user the username of the administrator for the new database.
080: * @param admin_pass the password of the administrator for the new database.
081: * @param config the configuration of the database to create and start in the
082: * local JVM.
083: * @return the DBSystem object used to access the database created.
084: */
085: public DBSystem createDatabase(DBConfig config, String admin_user,
086: String admin_pass) {
087:
088: // Create the Database object with this configuration.
089: Database database = createDatabase(config);
090: DatabaseSystem system = database.getSystem();
091:
092: // Create the database.
093: try {
094: database.create(admin_user, admin_pass);
095: database.init();
096: } catch (DatabaseException e) {
097: system.Debug().write(Lvl.ERROR, this ,
098: "Database create failed");
099: system.Debug().writeException(e);
100: throw new RuntimeException(e.getMessage());
101: }
102:
103: // Return the DBSystem object for the newly created database.
104: return new DBSystem(this , config, database);
105:
106: }
107:
108: /**
109: * Starts a database in the local JVM given the configuration in DBConfig
110: * and returns a DBSystem object. When this method returns, the database
111: * will be up and running providing there was no failure to initialize the
112: * database.
113: * <p>
114: * A failure might happen if the database does not exist in the path given
115: * in the configuration.
116: *
117: * @param config the configuration of the database to start in the local
118: * JVM.
119: * @return the DBSystem object used to access the database started.
120: */
121: public DBSystem startDatabase(DBConfig config) {
122:
123: // Create the Database object with this configuration.
124: Database database = createDatabase(config);
125: DatabaseSystem system = database.getSystem();
126:
127: // First initialise the database
128: try {
129: database.init();
130: } catch (DatabaseException e) {
131: system.Debug().write(Lvl.ERROR, this ,
132: "Database init failed");
133: system.Debug().writeException(e);
134: throw new RuntimeException(e.getMessage());
135: }
136:
137: // Return the DBSystem object for the newly created database.
138: return new DBSystem(this , config, database);
139:
140: }
141:
142: // ---------- Static methods ----------
143:
144: /**
145: * Creates a Database object for the given DBConfig configuration.
146: */
147: private static Database createDatabase(DBConfig config) {
148:
149: DatabaseSystem system = new DatabaseSystem();
150:
151: // Initialize the DatabaseSystem first,
152: // ------------------------------------
153:
154: // This will throw an Error exception if the database system has already
155: // been initialized.
156: system.init(config);
157:
158: // Start the database class
159: // ------------------------
160:
161: // Note, currently we only register one database, and it is named
162: // 'DefaultDatabase'.
163: Database database = new Database(system, "DefaultDatabase");
164:
165: // Start up message
166: system.Debug().write(Lvl.MESSAGE, DBController.class,
167: "Starting Database Server");
168:
169: return database;
170: }
171:
172: /**
173: * Returns the static controller for this JVM.
174: */
175: public static DBController getDefault() {
176: return VM_DB_CONTROLLER;
177: }
178:
179: /**
180: * The static DBController object.
181: */
182: private final static DBController VM_DB_CONTROLLER = new DBController();
183:
184: }
|