KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > dao > file > berkeleydb > FileDbEnv


1 /*
2  * $Id: FileDbEnv.java,v 1.3 2005/06/07 12:32:34 bel70 Exp $
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitriy Belov <bel@jresearch.org>
23  * .
24  * * ***** END LICENSE BLOCK ***** */

25 /*
26  * Created on 08.08.2004
27  *
28  */

29 package org.jresearch.gossip.dao.file.berkeleydb;
30
31 import java.io.File JavaDoc;
32
33 import org.jresearch.gossip.IConst;
34 import org.jresearch.gossip.configuration.Configurator;
35 import org.jresearch.gossip.exception.SystemException;
36
37 import com.sleepycat.je.Database;
38 import com.sleepycat.je.DatabaseConfig;
39 import com.sleepycat.je.DatabaseException;
40 import com.sleepycat.je.Environment;
41 import com.sleepycat.je.EnvironmentConfig;
42
43 /**
44  * @author Dmitry Belov
45  *
46  */

47 public class FileDbEnv {
48
49     private Environment env;
50
51     private DatabaseConfig dbConfig;
52
53     private FileDbEnv() {
54
55     }
56
57     private static FileDbEnv instance;
58
59     /**
60      * @return
61      * @throws SystemException
62      */

63     public static synchronized FileDbEnv getInstance() throws SystemException {
64         if (instance == null) {
65             load();
66         }
67         return instance;
68     }
69
70     /**
71      * @throws SystemException
72      */

73     public static void load() throws SystemException {
74         try {
75             if (instance != null) {
76                 instance.stop();
77             }
78             instance = new FileDbEnv();
79             File JavaDoc path;
80
81             path = new File JavaDoc(Configurator.getInstance().get(
82                     IConst.CONFIG.ATTACH_STORE_PATH));
83
84             instance.setup(path, false);
85         } catch (DatabaseException e) {
86             throw new SystemException(e);
87         }
88     }
89
90     /**
91      * @throws SystemException
92      */

93     public static void close() throws SystemException {
94
95         if (instance != null) {
96             instance.stop();
97         }
98
99     }
100
101     /**
102      * @param envHome
103      * @param readOnly
104      * @throws DatabaseException
105      */

106     private void setup(File JavaDoc envHome, boolean readOnly) throws DatabaseException {
107
108         EnvironmentConfig myEnvConfig = new EnvironmentConfig();
109         dbConfig = new DatabaseConfig();
110
111         // If the environment is read-only, then
112
// make the databases read-only too.
113
myEnvConfig.setReadOnly(readOnly);
114         dbConfig.setReadOnly(readOnly);
115
116         // If the environment is opened for write, then we want to be
117
// able to create the environment and databases if
118
// they do not exist.
119
myEnvConfig.setAllowCreate(!readOnly);
120         dbConfig.setAllowCreate(!readOnly);
121
122         // Allow transactions if we are writing to the database
123
myEnvConfig.setTransactional(!readOnly);
124         dbConfig.setTransactional(!readOnly);
125         // Open the environment
126
env = new Environment(envHome, myEnvConfig);
127     }
128
129     // Close the environment
130
/**
131      * @throws SystemException
132      */

133     private void stop() throws SystemException {
134         if (env != null) {
135             try {
136                 // Finally, close the environment.
137
env.close();
138
139             } catch (DatabaseException dbe) {
140                 throw new SystemException("Error closing environment: ", dbe);
141             }
142         }
143     }
144
145     /**
146      * @return Returns the env.
147      */

148     public Environment getEnv() {
149         return env;
150     }
151
152     /**
153      * @param dbname
154      * @return Returns the fileDb.
155      * @throws SystemException
156      */

157     public Database getFileDb(String JavaDoc dbname) throws SystemException {
158         try {
159             return env.openDatabase(null, dbname, dbConfig);
160         } catch (DatabaseException dbe) {
161             throw new SystemException("Error opening " + dbname + ": ", dbe);
162         }
163     }
164
165 }
Popular Tags