KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: Sample.java,v 1.17 2006/10/31 19:56:10 mark Exp $
7  */

8
9 package collections.ship.basic;
10
11 import java.io.FileNotFoundException JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import com.sleepycat.collections.TransactionRunner;
16 import com.sleepycat.collections.TransactionWorker;
17 import com.sleepycat.je.DatabaseException;
18
19 /**
20  * Sample is the main entry point for the sample program and may be run as
21  * follows:
22  *
23  * <pre>
24  * java collections.ship.basic.Sample
25  * [-h <home-directory> ]
26  * </pre>
27  *
28  * <p> The default for the home directory is ./tmp -- the tmp subdirectory of
29  * the current directory where the sample is run. The home directory must exist
30  * before running the sample. To recreate the sample database from scratch,
31  * delete all files in the home directory before running the sample. </p>
32  *
33  * @author Mark Hayes
34  */

35 public class Sample {
36
37     private SampleDatabase db;
38     private SampleViews views;
39
40     /**
41      * Run the sample program.
42      */

43     public static void main(String JavaDoc[] args) {
44
45         System.out.println("\nRunning sample: " + Sample.class);
46
47         // Parse the command line arguments.
48
//
49
String JavaDoc homeDir = "./tmp";
50         for (int i = 0; i < args.length; i += 1) {
51             if (args[i].equals("-h") && i < args.length - 1) {
52                 i += 1;
53                 homeDir = args[i];
54             } else {
55                 System.err.println("Usage:\n java " + Sample.class.getName() +
56                    "\n [-h <home-directory>]");
57                 System.exit(2);
58             }
59         }
60
61         // Run the sample.
62
//
63
Sample sample = null;
64         try {
65             sample = new Sample(homeDir);
66             sample.run();
67         } catch (Exception JavaDoc e) {
68             // If an exception reaches this point, the last transaction did not
69
// complete. If the exception is RunRecoveryException, follow
70
// the Berkeley DB recovery procedures before running again.
71
e.printStackTrace();
72         } finally {
73             if (sample != null) {
74                 try {
75                     // Always attempt to close the database cleanly.
76
sample.close();
77                 } catch (Exception JavaDoc e) {
78                     System.err.println("Exception during database close:");
79                     e.printStackTrace();
80                 }
81             }
82         }
83     }
84
85     /**
86      * Open the database and views.
87      */

88     private Sample(String JavaDoc homeDir)
89         throws DatabaseException, FileNotFoundException JavaDoc {
90
91         db = new SampleDatabase(homeDir);
92         views = new SampleViews(db);
93     }
94
95     /**
96      * Close the database cleanly.
97      */

98     private void close()
99         throws DatabaseException {
100
101         db.close();
102     }
103
104     /**
105      * Run two transactions to populate and print the database. A
106      * TransactionRunner is used to ensure consistent handling of transactions,
107      * including deadlock retries. But the best transaction handling mechanism
108      * to use depends on the application.
109      */

110     private void run()
111         throws Exception JavaDoc {
112
113         TransactionRunner runner = new TransactionRunner(db.getEnvironment());
114         runner.run(new PopulateDatabase());
115         runner.run(new PrintDatabase());
116     }
117
118     /**
119      * Populate the database in a single transaction.
120      */

121     private class PopulateDatabase implements TransactionWorker {
122
123         public void doWork()
124             throws Exception JavaDoc {
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()
137             throws Exception JavaDoc {
138             printEntries("Parts",
139              views.getPartEntrySet().iterator());
140             printEntries("Suppliers",
141              views.getSupplierEntrySet().iterator());
142             printEntries("Shipments",
143              views.getShipmentEntrySet().iterator());
144         }
145     }
146
147     /**
148      * Populate the part entities in the database. If the part map is not
149      * empty, assume that this has already been done.
150      */

151     private void addParts() {
152
153         Map JavaDoc parts = views.getPartMap();
154         if (parts.isEmpty()) {
155             System.out.println("Adding Parts");
156             parts.put(new PartKey("P1"),
157                       new PartData("Nut", "Red",
158                                     new Weight(12.0, Weight.GRAMS),
159                                     "London"));
160             parts.put(new PartKey("P2"),
161                       new PartData("Bolt", "Green",
162                                     new Weight(17.0, Weight.GRAMS),
163                                     "Paris"));
164             parts.put(new PartKey("P3"),
165                       new PartData("Screw", "Blue",
166                                     new Weight(17.0, Weight.GRAMS),
167                                     "Rome"));
168             parts.put(new PartKey("P4"),
169                       new PartData("Screw", "Red",
170                                     new Weight(14.0, Weight.GRAMS),
171                                     "London"));
172             parts.put(new PartKey("P5"),
173                       new PartData("Cam", "Blue",
174                                     new Weight(12.0, Weight.GRAMS),
175                                     "Paris"));
176             parts.put(new PartKey("P6"),
177                       new PartData("Cog", "Red",
178                                     new Weight(19.0, Weight.GRAMS),
179                                     "London"));
180         }
181     }
182
183     /**
184      * Populate the supplier entities in the database. If the supplier map is
185      * not empty, assume that this has already been done.
186      */

187     private void addSuppliers() {
188
189         Map JavaDoc suppliers = views.getSupplierMap();
190         if (suppliers.isEmpty()) {
191             System.out.println("Adding Suppliers");
192             suppliers.put(new SupplierKey("S1"),
193                           new SupplierData("Smith", 20, "London"));
194             suppliers.put(new SupplierKey("S2"),
195                           new SupplierData("Jones", 10, "Paris"));
196             suppliers.put(new SupplierKey("S3"),
197                           new SupplierData("Blake", 30, "Paris"));
198             suppliers.put(new SupplierKey("S4"),
199                           new SupplierData("Clark", 20, "London"));
200             suppliers.put(new SupplierKey("S5"),
201                           new SupplierData("Adams", 30, "Athens"));
202         }
203     }
204
205     /**
206      * Populate the shipment entities in the database. If the shipment map
207      * is not empty, assume that this has already been done.
208      */

209     private void addShipments() {
210
211         Map JavaDoc shipments = views.getShipmentMap();
212         if (shipments.isEmpty()) {
213             System.out.println("Adding Shipments");
214             shipments.put(new ShipmentKey("P1", "S1"),
215                           new ShipmentData(300));
216             shipments.put(new ShipmentKey("P2", "S1"),
217                           new ShipmentData(200));
218             shipments.put(new ShipmentKey("P3", "S1"),
219                           new ShipmentData(400));
220             shipments.put(new ShipmentKey("P4", "S1"),
221                           new ShipmentData(200));
222             shipments.put(new ShipmentKey("P5", "S1"),
223                           new ShipmentData(100));
224             shipments.put(new ShipmentKey("P6", "S1"),
225                           new ShipmentData(100));
226             shipments.put(new ShipmentKey("P1", "S2"),
227                           new ShipmentData(300));
228             shipments.put(new ShipmentKey("P2", "S2"),
229                           new ShipmentData(400));
230             shipments.put(new ShipmentKey("P2", "S3"),
231                           new ShipmentData(200));
232             shipments.put(new ShipmentKey("P2", "S4"),
233                           new ShipmentData(200));
234             shipments.put(new ShipmentKey("P4", "S4"),
235                           new ShipmentData(300));
236             shipments.put(new ShipmentKey("P5", "S4"),
237                           new ShipmentData(400));
238         }
239     }
240
241     /**
242      * Print the key/value objects returned by an iterator of Map.Entry
243      * objects.
244      */

245     private void printEntries(String JavaDoc label, Iterator JavaDoc iterator) {
246
247         System.out.println("\n--- " + label + " ---");
248         while (iterator.hasNext()) {
249             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
250             System.out.println(entry.getKey().toString());
251             System.out.println(entry.getValue().toString());
252         }
253     }
254 }
255
Popular Tags