Source Code Cross Referenced for Sample.java in  » JMX » je » collections » ship » entity » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » JMX » je » collections.ship.entity 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.18.2.2 2008/01/07 15:14:00 cwl Exp $
007:         */
008:
009:        package collections.ship.entity;
010:
011:        import java.io.FileNotFoundException;
012:        import java.util.Iterator;
013:        import java.util.Set;
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.entity.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:             * and the indices are used to print the entities for certain keys.
134:             *
135:             * <p> Note the use of special iterator() methods.  These are used here
136:             * with indices to find the shipments for certain keys.</p>
137:             */
138:            private class PrintDatabase implements  TransactionWorker {
139:
140:                public void doWork() throws Exception {
141:                    printValues("Parts", views.getPartSet().iterator());
142:                    printValues("Suppliers", views.getSupplierSet().iterator());
143:                    printValues("Suppliers for City Paris", views
144:                            .getSupplierByCityMap().duplicates("Paris")
145:                            .iterator());
146:                    printValues("Shipments", views.getShipmentSet().iterator());
147:                    printValues("Shipments for Part P1", views
148:                            .getShipmentByPartMap().duplicates(
149:                                    new PartKey("P1")).iterator());
150:                    printValues("Shipments for Supplier S1", views
151:                            .getShipmentBySupplierMap().duplicates(
152:                                    new SupplierKey("S1")).iterator());
153:                }
154:            }
155:
156:            /**
157:             * Populate the part entities in the database.  If the part set is not
158:             * empty, assume that this has already been done.
159:             */
160:            private void addParts() {
161:
162:                Set parts = views.getPartSet();
163:                if (parts.isEmpty()) {
164:                    System.out.println("Adding Parts");
165:                    parts.add(new Part("P1", "Nut", "Red", new Weight(12.0,
166:                            Weight.GRAMS), "London"));
167:                    parts.add(new Part("P2", "Bolt", "Green", new Weight(17.0,
168:                            Weight.GRAMS), "Paris"));
169:                    parts.add(new Part("P3", "Screw", "Blue", new Weight(17.0,
170:                            Weight.GRAMS), "Rome"));
171:                    parts.add(new Part("P4", "Screw", "Red", new Weight(14.0,
172:                            Weight.GRAMS), "London"));
173:                    parts.add(new Part("P5", "Cam", "Blue", new Weight(12.0,
174:                            Weight.GRAMS), "Paris"));
175:                    parts.add(new Part("P6", "Cog", "Red", new Weight(19.0,
176:                            Weight.GRAMS), "London"));
177:                }
178:            }
179:
180:            /**
181:             * Populate the supplier entities in the database.  If the supplier set is
182:             * not empty, assume that this has already been done.
183:             */
184:            private void addSuppliers() {
185:
186:                Set suppliers = views.getSupplierSet();
187:                if (suppliers.isEmpty()) {
188:                    System.out.println("Adding Suppliers");
189:                    suppliers.add(new Supplier("S1", "Smith", 20, "London"));
190:                    suppliers.add(new Supplier("S2", "Jones", 10, "Paris"));
191:                    suppliers.add(new Supplier("S3", "Blake", 30, "Paris"));
192:                    suppliers.add(new Supplier("S4", "Clark", 20, "London"));
193:                    suppliers.add(new Supplier("S5", "Adams", 30, "Athens"));
194:                }
195:            }
196:
197:            /**
198:             * Populate the shipment entities in the database.  If the shipment set
199:             * is not empty, assume that this has already been done.
200:             */
201:            private void addShipments() {
202:
203:                Set shipments = views.getShipmentSet();
204:                if (shipments.isEmpty()) {
205:                    System.out.println("Adding Shipments");
206:                    shipments.add(new Shipment("P1", "S1", 300));
207:                    shipments.add(new Shipment("P2", "S1", 200));
208:                    shipments.add(new Shipment("P3", "S1", 400));
209:                    shipments.add(new Shipment("P4", "S1", 200));
210:                    shipments.add(new Shipment("P5", "S1", 100));
211:                    shipments.add(new Shipment("P6", "S1", 100));
212:                    shipments.add(new Shipment("P1", "S2", 300));
213:                    shipments.add(new Shipment("P2", "S2", 400));
214:                    shipments.add(new Shipment("P2", "S3", 200));
215:                    shipments.add(new Shipment("P2", "S4", 200));
216:                    shipments.add(new Shipment("P4", "S4", 300));
217:                    shipments.add(new Shipment("P5", "S4", 400));
218:                }
219:            }
220:
221:            /**
222:             * Print the objects returned by an iterator of entity value objects.
223:             */
224:            private void printValues(String label, Iterator iterator) {
225:
226:                System.out.println("\n--- " + label + " ---");
227:                while (iterator.hasNext()) {
228:                    System.out.println(iterator.next().toString());
229:                }
230:            }
231:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.