001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: LocalDatabase.java,v 1.5 2002/07/29 11:30:23 per_nyfelt Exp $
008:
009: package org.ozoneDB;
010:
011: import java.io.*;
012: import java.util.*;
013: import org.ozoneDB.core.*;
014: import org.ozoneDB.util.*;
015: import org.ozoneDB.core.DbRemote.*;
016: import org.ozoneDB.tools.*;
017: import org.ozoneDB.DxLib.*;
018:
019: /**
020: * This class represents a local database server that runs inside the same
021: * JVM as the client. For a detailed method description see OzoneInterface.
022: *
023: *
024: * @author <a href="http://www.softwarebuero.de/">SMB</a>
025: * @author <a href="http://www.medium.net/">SMB</a>
026: * @version $Revision: 1.5 $Date: 2002/07/29 11:30:23 $
027: * @see OzoneInterface
028: */
029: public final class LocalDatabase extends ExternalDatabase {
030:
031: public Env theEnv;
032:
033: public String userName;
034:
035: public LocalDatabase() {
036: }
037:
038: /**
039: * Create a new database in the given directory using default values
040: * for all Setup entries.
041: *
042: *
043: * @param _dirName
044: * @return a StringWriter that holds the status messages
045: * @exception java.lang.Exception
046: * @see Setup
047: */
048: public synchronized String create(String dirName) throws Exception {
049: //use default values
050: Setup setup = new Setup(theEnv);
051: return create(dirName, setup);
052: }
053:
054: /**
055: * Create a new database in the given directory using the values
056: * of the given Setup.
057: *
058: * @param dirName
059: * @return a StringWriter that holds the status messages
060: * @exception java.lang.Exception
061: * @see Setup
062: */
063: public synchronized String create(String dirName, Setup setup)
064: throws Exception {
065: StringWriter writer = new StringWriter();
066: PrintWriter logFile = new PrintWriter(writer, true);
067: Install.createDB(dirName, setup, logFile);
068: return writer.toString();
069: }
070:
071: /** opens the database.
072: * @param dirName is the path name of the database directory root
073: */
074: public void open(String dirName) throws Exception {
075: String username = System.getProperty("user.name");
076: open(dirName, null, username, username);
077: }
078:
079: /**
080: * For backwards compatibility
081: *
082: * @deprecated but still needed by legacy software.
083: * use open(String _dirName, String _debugLevel ) instead.
084: * @param dirName is the path name of the database directory root
085: * @param debugLevel should match one of the constants in OzoneDebugLevel.
086: * Overrrides the property ozoneDB.logLevel in config.properties.
087: */
088: public void open(String dirName, int debugLevel) throws Exception {
089: String level = OzoneDebugLevel.toLevel(debugLevel,
090: OzoneDebugLevel.INFO).toString();
091: open(dirName, level);
092: }
093:
094: /**
095: * Opens the database.
096: * @param dirName is the path name of the database directory root
097: * @param debugLevel derived from OzoneDebugLevel.
098: * Overrrides the property ozoneDB.logLevel in config.properties. If set to null, the config.properties
099: * setting will be used.
100: */
101: public void open(String dirName, String debugLevel)
102: throws Exception {
103: String username = System.getProperty("user.name");
104: open(dirName, debugLevel, username, username);
105: }
106:
107: /**
108: * Opens the database.
109: * @param dirName
110: * @param debugLevel, one of the String constants in OzoneDebugLevel (e.g. INFO_STR).
111: * Overrrides the property ozoneDB.logLevel in config.properties. If set to null, the config.properties
112: * setting will be used.
113: * @param userName
114: * @param password
115: */
116: public void open(String dirName, String debugLevel,
117: String userName, String password) throws Exception {
118: Hashtable props = new Hashtable();
119: props.put(PROP_DIR, dirName);
120: props.put(PROP_USER, userName);
121: props.put(PROP_PASSWD, password);
122: if (debugLevel != null) {
123: props.put(PROP_DEBUG, debugLevel);
124: }
125:
126: open(props);
127: }
128:
129: protected synchronized void open(Hashtable props) throws Exception {
130: try {
131: super .open(props);
132:
133: String dirName = (String) props.get(PROP_DIR);
134: userName = (String) props.get(PROP_USER);
135: String passwd = (String) props.get(PROP_PASSWD);
136: String debugLevel = ((String) props.get(PROP_DEBUG));
137:
138: theEnv = new Env(dirName, debugLevel);
139:
140: // create the user if it doesn't exist yet; in local mode we cannot use
141: // the admin tool to do so
142: if (theEnv.userManager.userForName(userName) == null) {
143: theEnv.userManager.newUser(userName, userName
144: .hashCode());
145: }
146: theEnv.startDeadlockRecognition();
147: } catch (Exception e) {
148: close();
149: throw e;
150: }
151: }
152:
153: public boolean isOpen() throws Exception {
154: return theEnv != null;
155: }
156:
157: public synchronized void close() throws Exception {
158: super .close();
159: if (theEnv != null) {
160: try {
161: theEnv.shutdown();
162: } finally {
163: theEnv = null;
164: }
165: }
166: }
167:
168: protected DbClient newConnection() throws Exception {
169: return new DbLocalClient(this , theEnv, userName);
170: }
171:
172: public String toString() {
173: return "LocalDatabase";
174: }
175:
176: protected void finalize() throws Exception {
177: close();
178: }
179:
180: /* Tells whether a database has been installed in the dbDir directory or not
181: * useful for erver when starting up the ExternalDatabase and makes it possible to
182: * determine whether to create or open a LocalDatabase
183: */
184: public boolean exists(String dbDir) {
185: return Install.dbExists(dbDir);
186: }
187:
188: }
|