001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: Sample.java,v 1.17.2.2 2008/01/07 15:14:00 cwl Exp $
007: */
008:
009: package collections.ship.basic;
010:
011: import java.io.FileNotFoundException;
012: import java.util.Iterator;
013: import java.util.Map;
014:
015: import com.sleepycat.collections.TransactionRunner;
016: import com.sleepycat.collections.TransactionWorker;
017: import com.sleepycat.je.DatabaseException;
018:
019: /**
020: * Sample is the main entry point for the sample program and may be run as
021: * follows:
022: *
023: * <pre>
024: * java collections.ship.basic.Sample
025: * [-h <home-directory> ]
026: * </pre>
027: *
028: * <p> The default for the home directory is ./tmp -- the tmp subdirectory of
029: * the current directory where the sample is run. The home directory must exist
030: * before running the sample. To recreate the sample database from scratch,
031: * delete all files in the home directory before running the sample. </p>
032: *
033: * @author Mark Hayes
034: */
035: public class Sample {
036:
037: private SampleDatabase db;
038: private SampleViews views;
039:
040: /**
041: * Run the sample program.
042: */
043: public static void main(String[] args) {
044:
045: System.out.println("\nRunning sample: " + Sample.class);
046:
047: // Parse the command line arguments.
048: //
049: String homeDir = "./tmp";
050: for (int i = 0; i < args.length; i += 1) {
051: if (args[i].equals("-h") && i < args.length - 1) {
052: i += 1;
053: homeDir = args[i];
054: } else {
055: System.err.println("Usage:\n java "
056: + Sample.class.getName()
057: + "\n [-h <home-directory>]");
058: System.exit(2);
059: }
060: }
061:
062: // Run the sample.
063: //
064: Sample sample = null;
065: try {
066: sample = new Sample(homeDir);
067: sample.run();
068: } catch (Exception e) {
069: // If an exception reaches this point, the last transaction did not
070: // complete. If the exception is RunRecoveryException, follow
071: // the Berkeley DB recovery procedures before running again.
072: e.printStackTrace();
073: } finally {
074: if (sample != null) {
075: try {
076: // Always attempt to close the database cleanly.
077: sample.close();
078: } catch (Exception e) {
079: System.err
080: .println("Exception during database close:");
081: e.printStackTrace();
082: }
083: }
084: }
085: }
086:
087: /**
088: * Open the database and views.
089: */
090: private Sample(String homeDir) throws DatabaseException,
091: FileNotFoundException {
092:
093: db = new SampleDatabase(homeDir);
094: views = new SampleViews(db);
095: }
096:
097: /**
098: * Close the database cleanly.
099: */
100: private void close() throws DatabaseException {
101:
102: db.close();
103: }
104:
105: /**
106: * Run two transactions to populate and print the database. A
107: * TransactionRunner is used to ensure consistent handling of transactions,
108: * including deadlock retries. But the best transaction handling mechanism
109: * to use depends on the application.
110: */
111: private void run() throws Exception {
112:
113: TransactionRunner runner = new TransactionRunner(db
114: .getEnvironment());
115: runner.run(new PopulateDatabase());
116: runner.run(new PrintDatabase());
117: }
118:
119: /**
120: * Populate the database in a single transaction.
121: */
122: private class PopulateDatabase implements TransactionWorker {
123:
124: public void doWork() throws Exception {
125: addSuppliers();
126: addParts();
127: addShipments();
128: }
129: }
130:
131: /**
132: * Print the database in a single transaction. All entities are printed.
133: */
134: private class PrintDatabase implements TransactionWorker {
135:
136: public void doWork() throws Exception {
137: printEntries("Parts", views.getPartEntrySet().iterator());
138: printEntries("Suppliers", views.getSupplierEntrySet()
139: .iterator());
140: printEntries("Shipments", views.getShipmentEntrySet()
141: .iterator());
142: }
143: }
144:
145: /**
146: * Populate the part entities in the database. If the part map is not
147: * empty, assume that this has already been done.
148: */
149: private void addParts() {
150:
151: Map parts = views.getPartMap();
152: if (parts.isEmpty()) {
153: System.out.println("Adding Parts");
154: parts.put(new PartKey("P1"), new PartData("Nut", "Red",
155: new Weight(12.0, Weight.GRAMS), "London"));
156: parts.put(new PartKey("P2"), new PartData("Bolt", "Green",
157: new Weight(17.0, Weight.GRAMS), "Paris"));
158: parts.put(new PartKey("P3"), new PartData("Screw", "Blue",
159: new Weight(17.0, Weight.GRAMS), "Rome"));
160: parts.put(new PartKey("P4"), new PartData("Screw", "Red",
161: new Weight(14.0, Weight.GRAMS), "London"));
162: parts.put(new PartKey("P5"), new PartData("Cam", "Blue",
163: new Weight(12.0, Weight.GRAMS), "Paris"));
164: parts.put(new PartKey("P6"), new PartData("Cog", "Red",
165: new Weight(19.0, Weight.GRAMS), "London"));
166: }
167: }
168:
169: /**
170: * Populate the supplier entities in the database. If the supplier map is
171: * not empty, assume that this has already been done.
172: */
173: private void addSuppliers() {
174:
175: Map suppliers = views.getSupplierMap();
176: if (suppliers.isEmpty()) {
177: System.out.println("Adding Suppliers");
178: suppliers.put(new SupplierKey("S1"), new SupplierData(
179: "Smith", 20, "London"));
180: suppliers.put(new SupplierKey("S2"), new SupplierData(
181: "Jones", 10, "Paris"));
182: suppliers.put(new SupplierKey("S3"), new SupplierData(
183: "Blake", 30, "Paris"));
184: suppliers.put(new SupplierKey("S4"), new SupplierData(
185: "Clark", 20, "London"));
186: suppliers.put(new SupplierKey("S5"), new SupplierData(
187: "Adams", 30, "Athens"));
188: }
189: }
190:
191: /**
192: * Populate the shipment entities in the database. If the shipment map
193: * is not empty, assume that this has already been done.
194: */
195: private void addShipments() {
196:
197: Map shipments = views.getShipmentMap();
198: if (shipments.isEmpty()) {
199: System.out.println("Adding Shipments");
200: shipments.put(new ShipmentKey("P1", "S1"),
201: new ShipmentData(300));
202: shipments.put(new ShipmentKey("P2", "S1"),
203: new ShipmentData(200));
204: shipments.put(new ShipmentKey("P3", "S1"),
205: new ShipmentData(400));
206: shipments.put(new ShipmentKey("P4", "S1"),
207: new ShipmentData(200));
208: shipments.put(new ShipmentKey("P5", "S1"),
209: new ShipmentData(100));
210: shipments.put(new ShipmentKey("P6", "S1"),
211: new ShipmentData(100));
212: shipments.put(new ShipmentKey("P1", "S2"),
213: new ShipmentData(300));
214: shipments.put(new ShipmentKey("P2", "S2"),
215: new ShipmentData(400));
216: shipments.put(new ShipmentKey("P2", "S3"),
217: new ShipmentData(200));
218: shipments.put(new ShipmentKey("P2", "S4"),
219: new ShipmentData(200));
220: shipments.put(new ShipmentKey("P4", "S4"),
221: new ShipmentData(300));
222: shipments.put(new ShipmentKey("P5", "S4"),
223: new ShipmentData(400));
224: }
225: }
226:
227: /**
228: * Print the key/value objects returned by an iterator of Map.Entry
229: * objects.
230: */
231: private void printEntries(String label, Iterator iterator) {
232:
233: System.out.println("\n--- " + label + " ---");
234: while (iterator.hasNext()) {
235: Map.Entry entry = (Map.Entry) iterator.next();
236: System.out.println(entry.getKey().toString());
237: System.out.println(entry.getValue().toString());
238: }
239: }
240: }
|