1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.archive.util.bdbje;
24
25 import java.io.File;
26
27 import com.sleepycat.bind.serial.StoredClassCatalog;
28 import com.sleepycat.je.Database;
29 import com.sleepycat.je.DatabaseConfig;
30 import com.sleepycat.je.DatabaseException;
31 import com.sleepycat.je.Environment;
32 import com.sleepycat.je.EnvironmentConfig;
33
34 /***
35 * Version of BDB_JE Environment with additional convenience features, such as
36 * a shared, cached StoredClassCatalog. (Additional convenience caching of
37 * Databases and StoredCollections may be added later.)
38 *
39 * @author gojomo
40 */
41 public class EnhancedEnvironment extends Environment {
42 StoredClassCatalog classCatalog;
43 Database classCatalogDB;
44
45 /***
46 * Constructor
47 *
48 * @param envHome directory in which to open environment
49 * @param envConfig config options
50 * @throws DatabaseException
51 */
52 public EnhancedEnvironment(File envHome, EnvironmentConfig envConfig) throws DatabaseException {
53 super(envHome, envConfig);
54 }
55
56 /***
57 * Return a StoredClassCatalog backed by a Database in this environment,
58 * either pre-existing or created (and cached) if necessary.
59 *
60 * @return the cached class catalog
61 */
62 public StoredClassCatalog getClassCatalog() {
63 if(classCatalog == null) {
64 DatabaseConfig dbConfig = new DatabaseConfig();
65 dbConfig.setAllowCreate(true);
66 try {
67 classCatalogDB = openDatabase(null, "classCatalog", dbConfig);
68 classCatalog = new StoredClassCatalog(classCatalogDB);
69 } catch (DatabaseException e) {
70
71 throw new RuntimeException(e);
72 }
73 }
74 return classCatalog;
75 }
76
77 @Override
78 public synchronized void close() throws DatabaseException {
79 if(classCatalogDB!=null) {
80 classCatalogDB.close();
81 }
82 super.close();
83 }
84
85
86
87 }