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