01: /* EnhancedEnvironment.java
02: *
03: * Created on February 18. 2007
04: *
05: * Copyright (C) 2007 Internet Archive.
06: *
07: * This file is part of the Heritrix web crawler (crawler.archive.org).
08: *
09: * Heritrix is free software; you can redistribute it and/or modify
10: * it under the terms of the GNU Lesser Public License as published by
11: * the Free Software Foundation; either version 2.1 of the License, or
12: * any later version.
13: *
14: * Heritrix is distributed in the hope that it will be useful,
15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: * GNU Lesser Public License for more details.
18: *
19: * You should have received a copy of the GNU Lesser Public License
20: * along with Heritrix; if not, write to the Free Software
21: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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)
53: throws DatabaseException {
54: super (envHome, envConfig);
55: }
56:
57: /**
58: * Return a StoredClassCatalog backed by a Database in this environment,
59: * either pre-existing or created (and cached) if necessary.
60: *
61: * @return the cached class catalog
62: */
63: public StoredClassCatalog getClassCatalog() {
64: if (classCatalog == null) {
65: DatabaseConfig dbConfig = new DatabaseConfig();
66: dbConfig.setAllowCreate(true);
67: try {
68: classCatalogDB = openDatabase(null, "classCatalog",
69: dbConfig);
70: classCatalog = new StoredClassCatalog(classCatalogDB);
71: } catch (DatabaseException e) {
72: // TODO Auto-generated catch block
73: throw new RuntimeException(e);
74: }
75: }
76: return classCatalog;
77: }
78:
79: @Override
80: public synchronized void close() throws DatabaseException {
81: if (classCatalogDB != null) {
82: classCatalogDB.close();
83: }
84: super.close();
85: }
86:
87: }
|