KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > dbi > DbEnvPool


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2000,2006 Oracle. All rights reserved.
5  *
6  * $Id: DbEnvPool.java,v 1.38 2006/10/30 21:14:15 bostic Exp $
7  */

8
9 package com.sleepycat.je.dbi;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.util.Hashtable JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import com.sleepycat.je.DatabaseException;
17 import com.sleepycat.je.EnvironmentConfig;
18
19 /**
20  * Singleton collection of database environments.
21  */

22 public class DbEnvPool {
23     /* singleton instance. */
24     private static DbEnvPool pool = new DbEnvPool();
25
26     /*
27      * Collection of environment handles, mapped by canonical directory
28      * name->EnvironmentImpl object.
29      */

30     private Map JavaDoc envs;
31
32     /**
33      * Enforce singleton behavior.
34      */

35     private DbEnvPool() {
36         envs = new Hashtable JavaDoc();
37     }
38
39     /**
40      * Access the singleton instance.
41      */

42     public static DbEnvPool getInstance() {
43         return pool;
44     }
45
46     /**
47      * If the environment is not open, open it.
48      */

49     public EnvironmentImplInfo getEnvironment(File JavaDoc envHome,
50                                                EnvironmentConfig config )
51         throws DatabaseException {
52
53         return getEnvironment(envHome, config, true);
54     }
55
56     /*
57      * Only return an environment if it's already bee open in this process.
58      */

59     public EnvironmentImplInfo getExistingEnvironment(File JavaDoc envHome)
60         throws DatabaseException {
61
62         return getEnvironment(envHome, null, false);
63     }
64
65     /**
66      * Find a single environment, used by Environment handles and by command
67      * line utilities.
68      */

69     private synchronized
70         EnvironmentImplInfo getEnvironment(File JavaDoc 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 JavaDoc environmentKey = getEnvironmentMapKey(envHome);
80
81         if (envs.containsKey(environmentKey)) {
82             /* Environment is resident */
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                 /*
98                  * Environment must be instantiated. If it can be created,
99                  * the configuration must have allowCreate set.
100                  */

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     /**
119      * Remove a EnvironmentImpl from the pool because it's been closed.
120      */

121     void remove(File JavaDoc envHome)
122         throws DatabaseException {
123         envs.remove(getEnvironmentMapKey(envHome));
124     }
125
126     public void clear() {
127         envs.clear();
128     }
129
130     /*
131      * Struct for returning two values.
132      */

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     /* Use the canonical path name for a normalized environment key. */
144     private String JavaDoc getEnvironmentMapKey(File JavaDoc file)
145         throws DatabaseException {
146         try {
147             return file.getCanonicalPath();
148         } catch (IOException JavaDoc e) {
149             throw new DatabaseException(e);
150         }
151     }
152 }
153
Popular Tags