KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > collections > ship > basic > SampleDatabase


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: SampleDatabase.java,v 1.24 2006/10/30 21:13:55 bostic Exp $
7  */

8
9 package collections.ship.basic;
10
11 import java.io.File JavaDoc;
12 import java.io.FileNotFoundException JavaDoc;
13
14 import com.sleepycat.bind.serial.StoredClassCatalog;
15 import com.sleepycat.je.Database;
16 import com.sleepycat.je.DatabaseConfig;
17 import com.sleepycat.je.DatabaseException;
18 import com.sleepycat.je.Environment;
19 import com.sleepycat.je.EnvironmentConfig;
20
21 /**
22  * SampleDatabase defines the storage containers, indices and foreign keys
23  * for the sample database.
24  *
25  * @author Mark Hayes
26  */

27 public class SampleDatabase {
28
29     private static final String JavaDoc CLASS_CATALOG = "java_class_catalog";
30     private static final String JavaDoc SUPPLIER_STORE = "supplier_store";
31     private static final String JavaDoc PART_STORE = "part_store";
32     private static final String JavaDoc SHIPMENT_STORE = "shipment_store";
33
34     private Environment env;
35     private Database partDb;
36     private Database supplierDb;
37     private Database shipmentDb;
38     private StoredClassCatalog javaCatalog;
39
40     /**
41      * Open all storage containers, indices, and catalogs.
42      */

43     public SampleDatabase(String JavaDoc homeDirectory)
44         throws DatabaseException, FileNotFoundException JavaDoc {
45
46         // Open the Berkeley DB environment in transactional mode.
47
//
48
System.out.println("Opening environment in: " + homeDirectory);
49         EnvironmentConfig envConfig = new EnvironmentConfig();
50         envConfig.setTransactional(true);
51         envConfig.setAllowCreate(true);
52         env = new Environment(new File JavaDoc(homeDirectory), envConfig);
53
54         // Set the Berkeley DB config for opening all stores.
55
//
56
DatabaseConfig dbConfig = new DatabaseConfig();
57         dbConfig.setTransactional(true);
58         dbConfig.setAllowCreate(true);
59
60         // Create the Serial class catalog. This holds the serialized class
61
// format for all database records of serial format.
62
//
63
Database catalogDb = env.openDatabase(null, CLASS_CATALOG, dbConfig);
64         javaCatalog = new StoredClassCatalog(catalogDb);
65
66         // Open the Berkeley DB database for the part, supplier and shipment
67
// stores. The stores are opened with no duplicate keys allowed.
68
//
69
partDb = env.openDatabase(null, PART_STORE, dbConfig);
70
71         supplierDb = env.openDatabase(null, SUPPLIER_STORE, dbConfig);
72
73         shipmentDb = env.openDatabase(null, SHIPMENT_STORE, dbConfig);
74     }
75
76     /**
77      * Return the storage environment for the database.
78      */

79     public final Environment getEnvironment() {
80
81         return env;
82     }
83
84     /**
85      * Return the class catalog.
86      */

87     public final StoredClassCatalog getClassCatalog() {
88
89         return javaCatalog;
90     }
91
92     /**
93      * Return the part storage container.
94      */

95     public final Database getPartDatabase() {
96
97         return partDb;
98     }
99
100     /**
101      * Return the supplier storage container.
102      */

103     public final Database getSupplierDatabase() {
104
105         return supplierDb;
106     }
107
108     /**
109      * Return the shipment storage container.
110      */

111     public final Database getShipmentDatabase() {
112
113         return shipmentDb;
114     }
115
116     /**
117      * Close all databases and the environment.
118      */

119     public void close()
120         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 }
130
Popular Tags