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 dbConfig.setReadOnly(this.getConfig().getReadOnly());
67 try {
68 classCatalogDB = openDatabase(null, "classCatalog", dbConfig);
69 classCatalog = new StoredClassCatalog(classCatalogDB);
70 } catch (DatabaseException e) {
71
72 throw new RuntimeException(e);
73 }
74 }
75 return classCatalog;
76 }
77
78 @Override
79 public synchronized void close() throws DatabaseException {
80 if(classCatalogDB!=null) {
81 classCatalogDB.close();
82 }
83 super.close();
84 }
85
86
87
88 }