KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > LocalDatabase


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: LocalDatabase.java,v 1.8 2002/09/18 06:54:13 per_nyfelt Exp $
8

9 package org.ozoneDB;
10
11 import org.ozoneDB.core.DbRemote.DbClient;
12 import org.ozoneDB.core.DbRemote.DbLocalClient;
13 import org.ozoneDB.core.Env;
14 import org.ozoneDB.tools.Install;
15 import org.ozoneDB.util.OzoneDebugLevel;
16
17 import java.io.PrintWriter JavaDoc;
18 import java.io.StringWriter JavaDoc;
19 import java.util.Hashtable JavaDoc;
20
21
22 /**
23  * This class represents a local database server that runs inside the same
24  * JVM as the client. For a detailed method description see OzoneInterface.
25  *
26  *
27  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
28  * @author <a HREF="http://www.medium.net/">SMB</a>
29  * @version $Revision: 1.8 $Date: 2002/09/18 06:54:13 $
30  * @see OzoneInterface
31  */

32 public final class LocalDatabase extends ExternalDatabase {
33
34     public Env env;
35
36     public String JavaDoc userName;
37
38
39     public LocalDatabase() {
40     }
41
42
43     /**
44      * Create a new database in the given directory using default values
45      * for all Setup entries.
46      *
47      *
48      * @param dirName The directory to create the database in
49      * @return a StringWriter that holds the status messages
50      * @exception java.lang.Exception
51      * @see Setup
52      */

53     public synchronized String JavaDoc create( String JavaDoc dirName ) throws Exception JavaDoc {
54         //use default values
55
Setup setup = new Setup( env );
56         return create( dirName, setup );
57     }
58
59
60     /**
61      * Create a new database in the given directory using the values
62      * of the given Setup.
63      *
64      * @param dirName
65      * @return a StringWriter that holds the status messages
66      * @exception java.lang.Exception
67      * @see Setup
68      */

69     public synchronized String JavaDoc create( String JavaDoc dirName, Setup setup ) throws Exception JavaDoc {
70         StringWriter JavaDoc writer = new StringWriter JavaDoc();
71         PrintWriter JavaDoc logFile = new PrintWriter JavaDoc( writer, true );
72         Install.createDB( dirName, setup, logFile );
73         return writer.toString();
74     }
75
76     /** opens the database.
77      * @param dirName is the path name of the database directory root
78     */

79     public void open( String JavaDoc dirName) throws Exception JavaDoc {
80         String JavaDoc username = System.getProperty( "user.name" );
81         open( dirName, null, username, username );
82     }
83
84     /**
85      * For backwards compatibility
86      *
87      * @deprecated but still needed by legacy software.
88      * use open(String _dirName, String _debugLevel ) instead.
89      * @param dirName is the path name of the database directory root
90      * @param debugLevel should match one of the constants in OzoneDebugLevel.
91      * Overrrides the property ozoneDB.logLevel in config.properties.
92      */

93     public void open(String JavaDoc dirName, int debugLevel) throws Exception JavaDoc {
94         String JavaDoc level = OzoneDebugLevel.toLevel(debugLevel, OzoneDebugLevel.INFO).toString();
95         open(dirName, level);
96     }
97
98     /**
99      * Opens the database.
100      * @param dirName is the path name of the database directory root
101      * @param debugLevel derived from OzoneDebugLevel.
102      * Overrrides the property ozoneDB.logLevel in config.properties. If set to null, the config.properties
103      * setting will be used.
104      */

105     public void open( String JavaDoc dirName, String JavaDoc debugLevel ) throws Exception JavaDoc {
106         String JavaDoc username = System.getProperty( "user.name" );
107         open( dirName, debugLevel, username, username );
108     }
109
110     /**
111      * Opens the database.
112      * @param dirName
113      * @param debugLevel one of the String constants in OzoneDebugLevel (e.g. INFO_STR).
114      * Overrrides the property ozoneDB.logLevel in config.properties. If set to null, the config.properties
115      * setting will be used.
116      * @param userName
117      * @param password
118      */

119     public void open( String JavaDoc dirName, String JavaDoc debugLevel, String JavaDoc userName, String JavaDoc password ) throws Exception JavaDoc {
120         Hashtable JavaDoc props = new Hashtable JavaDoc();
121         props.put( PROP_DIR, dirName );
122         props.put( PROP_USER, userName );
123         props.put( PROP_PASSWD, password );
124         if (debugLevel != null) {
125             props.put( PROP_DEBUG, debugLevel );
126         }
127
128         open( props );
129     }
130
131
132     protected synchronized void open( Hashtable JavaDoc props ) throws Exception JavaDoc {
133         try {
134             super.open( props );
135
136             String JavaDoc dirName = (String JavaDoc)props.get( PROP_DIR );
137             userName = (String JavaDoc)props.get( PROP_USER );
138             //String passwd = (String)props.get( PROP_PASSWD );
139
String JavaDoc debugLevel = ((String JavaDoc)props.get( PROP_DEBUG ));
140
141             env = new Env( dirName, debugLevel);
142
143             // create the user if it doesn't exist yet; in local mode we cannot use
144
// the admin tool to do so
145
if (env.userManager.userForName( userName ) == null) {
146                 env.userManager.newUser( userName, userName.hashCode() );
147             }
148             env.startDeadlockRecognition();
149         }
150         catch (Exception JavaDoc e) {
151             close();
152             throw e;
153         }
154     }
155
156
157     public boolean isOpen() throws Exception JavaDoc {
158         return env != null;
159     }
160
161
162     public synchronized void close() throws Exception JavaDoc {
163         super.close();
164         if (env != null) {
165             try {
166                 env.shutdown();
167             }
168             finally {
169                 env = null;
170             }
171         }
172     }
173
174
175     protected DbClient newConnection() throws org.ozoneDB.core.UserManagerException,org.ozoneDB.PermissionDeniedException {
176         return new DbLocalClient(this,env,userName);
177     }
178
179
180     public String JavaDoc toString() {
181         return "LocalDatabase";
182     }
183
184
185     protected void finalize() throws Exception JavaDoc {
186         close();
187     }
188
189     /* Tells whether a database has been installed in the dbDir directory or not
190      * useful for erver when starting up the ExternalDatabase and makes it possible to
191      * determine whether to create or open a LocalDatabase
192      */

193     public boolean exists(String JavaDoc dbDir) {
194         return Install.dbExists(dbDir);
195     }
196
197 }
198
Popular Tags