001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: SampleDatabase.java,v 1.24.2.2 2008/01/07 15:14:00 cwl Exp $
007: */
008:
009: package collections.ship.basic;
010:
011: import java.io.File;
012: import java.io.FileNotFoundException;
013:
014: import com.sleepycat.bind.serial.StoredClassCatalog;
015: import com.sleepycat.je.Database;
016: import com.sleepycat.je.DatabaseConfig;
017: import com.sleepycat.je.DatabaseException;
018: import com.sleepycat.je.Environment;
019: import com.sleepycat.je.EnvironmentConfig;
020:
021: /**
022: * SampleDatabase defines the storage containers, indices and foreign keys
023: * for the sample database.
024: *
025: * @author Mark Hayes
026: */
027: public class SampleDatabase {
028:
029: private static final String CLASS_CATALOG = "java_class_catalog";
030: private static final String SUPPLIER_STORE = "supplier_store";
031: private static final String PART_STORE = "part_store";
032: private static final String SHIPMENT_STORE = "shipment_store";
033:
034: private Environment env;
035: private Database partDb;
036: private Database supplierDb;
037: private Database shipmentDb;
038: private StoredClassCatalog javaCatalog;
039:
040: /**
041: * Open all storage containers, indices, and catalogs.
042: */
043: public SampleDatabase(String homeDirectory)
044: throws DatabaseException, FileNotFoundException {
045:
046: // Open the Berkeley DB environment in transactional mode.
047: //
048: System.out.println("Opening environment in: " + homeDirectory);
049: EnvironmentConfig envConfig = new EnvironmentConfig();
050: envConfig.setTransactional(true);
051: envConfig.setAllowCreate(true);
052: env = new Environment(new File(homeDirectory), envConfig);
053:
054: // Set the Berkeley DB config for opening all stores.
055: //
056: DatabaseConfig dbConfig = new DatabaseConfig();
057: dbConfig.setTransactional(true);
058: dbConfig.setAllowCreate(true);
059:
060: // Create the Serial class catalog. This holds the serialized class
061: // format for all database records of serial format.
062: //
063: Database catalogDb = env.openDatabase(null, CLASS_CATALOG,
064: dbConfig);
065: javaCatalog = new StoredClassCatalog(catalogDb);
066:
067: // Open the Berkeley DB database for the part, supplier and shipment
068: // stores. The stores are opened with no duplicate keys allowed.
069: //
070: partDb = env.openDatabase(null, PART_STORE, dbConfig);
071:
072: supplierDb = env.openDatabase(null, SUPPLIER_STORE, dbConfig);
073:
074: shipmentDb = env.openDatabase(null, SHIPMENT_STORE, dbConfig);
075: }
076:
077: /**
078: * Return the storage environment for the database.
079: */
080: public final Environment getEnvironment() {
081:
082: return env;
083: }
084:
085: /**
086: * Return the class catalog.
087: */
088: public final StoredClassCatalog getClassCatalog() {
089:
090: return javaCatalog;
091: }
092:
093: /**
094: * Return the part storage container.
095: */
096: public final Database getPartDatabase() {
097:
098: return partDb;
099: }
100:
101: /**
102: * Return the supplier storage container.
103: */
104: public final Database getSupplierDatabase() {
105:
106: return supplierDb;
107: }
108:
109: /**
110: * Return the shipment storage container.
111: */
112: public final Database getShipmentDatabase() {
113:
114: return shipmentDb;
115: }
116:
117: /**
118: * Close all databases and the environment.
119: */
120: public void close() throws DatabaseException {
121:
122: partDb.close();
123: supplierDb.close();
124: shipmentDb.close();
125: // And don't forget to close the catalog and the environment.
126: javaCatalog.close();
127: env.close();
128: }
129: }
|