1 8 9 package com.sleepycat.je.dbi; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.util.Hashtable ; 14 import java.util.Map ; 15 16 import com.sleepycat.je.DatabaseException; 17 import com.sleepycat.je.EnvironmentConfig; 18 19 22 public class DbEnvPool { 23 24 private static DbEnvPool pool = new DbEnvPool(); 25 26 30 private Map envs; 31 32 35 private DbEnvPool() { 36 envs = new Hashtable (); 37 } 38 39 42 public static DbEnvPool getInstance() { 43 return pool; 44 } 45 46 49 public EnvironmentImplInfo getEnvironment(File envHome, 50 EnvironmentConfig config ) 51 throws DatabaseException { 52 53 return getEnvironment(envHome, config, true); 54 } 55 56 59 public EnvironmentImplInfo getExistingEnvironment(File envHome) 60 throws DatabaseException { 61 62 return getEnvironment(envHome, null, false); 63 } 64 65 69 private synchronized 70 EnvironmentImplInfo getEnvironment(File envHome, 71 EnvironmentConfig config, 72 boolean openIfNeeded) 73 throws DatabaseException { 74 75 boolean found; 76 boolean firstHandle = false; 77 78 EnvironmentImpl environmentImpl = null; 79 String environmentKey = getEnvironmentMapKey(envHome); 80 81 if (envs.containsKey(environmentKey)) { 82 83 environmentImpl = (EnvironmentImpl) envs.get(environmentKey); 84 if (!environmentImpl.isOpen()) { 85 if (openIfNeeded) { 86 environmentImpl.open(); 87 found = true; 88 } else { 89 found = false; 90 } 91 } else { 92 found = true; 93 } 94 } else { 95 if (openIfNeeded) { 96 97 101 environmentImpl = new EnvironmentImpl(envHome, config); 102 envs.put(environmentKey, environmentImpl); 103 firstHandle = true; 104 found = true; 105 } else { 106 found = false; 107 } 108 } 109 110 if (found) { 111 return new EnvironmentImplInfo(environmentImpl, firstHandle); 112 } else { 113 return new EnvironmentImplInfo(null, false); 114 } 115 } 116 117 118 121 void remove(File envHome) 122 throws DatabaseException { 123 envs.remove(getEnvironmentMapKey(envHome)); 124 } 125 126 public void clear() { 127 envs.clear(); 128 } 129 130 133 public static class EnvironmentImplInfo { 134 public EnvironmentImpl envImpl; 135 public boolean firstHandle = false; 136 137 EnvironmentImplInfo(EnvironmentImpl envImpl, boolean firstHandle) { 138 this.envImpl = envImpl; 139 this.firstHandle = firstHandle; 140 } 141 } 142 143 144 private String getEnvironmentMapKey(File file) 145 throws DatabaseException { 146 try { 147 return file.getCanonicalPath(); 148 } catch (IOException e) { 149 throw new DatabaseException(e); 150 } 151 } 152 } 153 | Popular Tags |