1 25 29 package org.jresearch.gossip.dao.file.berkeleydb; 30 31 import java.io.File ; 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 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 63 public static synchronized FileDbEnv getInstance() throws SystemException { 64 if (instance == null) { 65 load(); 66 } 67 return instance; 68 } 69 70 73 public static void load() throws SystemException { 74 try { 75 if (instance != null) { 76 instance.stop(); 77 } 78 instance = new FileDbEnv(); 79 File path; 80 81 path = new File (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 93 public static void close() throws SystemException { 94 95 if (instance != null) { 96 instance.stop(); 97 } 98 99 } 100 101 106 private void setup(File envHome, boolean readOnly) throws DatabaseException { 107 108 EnvironmentConfig myEnvConfig = new EnvironmentConfig(); 109 dbConfig = new DatabaseConfig(); 110 111 myEnvConfig.setReadOnly(readOnly); 114 dbConfig.setReadOnly(readOnly); 115 116 myEnvConfig.setAllowCreate(!readOnly); 120 dbConfig.setAllowCreate(!readOnly); 121 122 myEnvConfig.setTransactional(!readOnly); 124 dbConfig.setTransactional(!readOnly); 125 env = new Environment(envHome, myEnvConfig); 127 } 128 129 133 private void stop() throws SystemException { 134 if (env != null) { 135 try { 136 env.close(); 138 139 } catch (DatabaseException dbe) { 140 throw new SystemException("Error closing environment: ", dbe); 141 } 142 } 143 } 144 145 148 public Environment getEnv() { 149 return env; 150 } 151 152 157 public Database getFileDb(String 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 |