Source Code Cross Referenced for HelloDatabaseWorld.java in  » JMX » je » collections » hello » 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.hello 
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: HelloDatabaseWorld.java,v 1.23.2.2 2008/01/07 15:13:59 cwl Exp $
007:         */
008:
009:        package collections.hello;
010:
011:        import java.io.File;
012:        import java.util.Iterator;
013:        import java.util.Map;
014:        import java.util.SortedMap;
015:
016:        import com.sleepycat.bind.serial.ClassCatalog;
017:        import com.sleepycat.bind.serial.SerialBinding;
018:        import com.sleepycat.bind.serial.StoredClassCatalog;
019:        import com.sleepycat.bind.tuple.TupleBinding;
020:        import com.sleepycat.collections.StoredSortedMap;
021:        import com.sleepycat.collections.TransactionRunner;
022:        import com.sleepycat.collections.TransactionWorker;
023:        import com.sleepycat.je.Database;
024:        import com.sleepycat.je.DatabaseConfig;
025:        import com.sleepycat.je.Environment;
026:        import com.sleepycat.je.EnvironmentConfig;
027:
028:        /**
029:         * @author Mark Hayes
030:         */
031:        public class HelloDatabaseWorld implements  TransactionWorker {
032:
033:            private static final String[] INT_NAMES = { "Hello", "Database",
034:                    "World", };
035:            private static boolean create = true;
036:
037:            private Environment env;
038:            private ClassCatalog catalog;
039:            private Database db;
040:            private SortedMap map;
041:
042:            /** Creates the environment and runs a transaction */
043:            public static void main(String[] argv) throws Exception {
044:
045:                String dir = "./tmp";
046:
047:                // environment is transactional
048:                EnvironmentConfig envConfig = new EnvironmentConfig();
049:                envConfig.setTransactional(true);
050:                if (create) {
051:                    envConfig.setAllowCreate(true);
052:                }
053:                Environment env = new Environment(new File(dir), envConfig);
054:
055:                // create the application and run a transaction
056:                HelloDatabaseWorld worker = new HelloDatabaseWorld(env);
057:                TransactionRunner runner = new TransactionRunner(env);
058:                try {
059:                    // open and access the database within a transaction
060:                    runner.run(worker);
061:                } finally {
062:                    // close the database outside the transaction
063:                    worker.close();
064:                }
065:            }
066:
067:            /** Creates the database for this application */
068:            private HelloDatabaseWorld(Environment env) throws Exception {
069:
070:                this .env = env;
071:                open();
072:            }
073:
074:            /** Performs work within a transaction. */
075:            public void doWork() throws Exception {
076:
077:                writeAndRead();
078:            }
079:
080:            /** Opens the database and creates the Map. */
081:            private void open() throws Exception {
082:
083:                // use a generic database configuration
084:                DatabaseConfig dbConfig = new DatabaseConfig();
085:                dbConfig.setTransactional(true);
086:                if (create) {
087:                    dbConfig.setAllowCreate(true);
088:                }
089:
090:                // catalog is needed for serial bindings (java serialization)
091:                Database catalogDb = env
092:                        .openDatabase(null, "catalog", dbConfig);
093:                catalog = new StoredClassCatalog(catalogDb);
094:
095:                // use Integer tuple binding for key entries
096:                TupleBinding keyBinding = TupleBinding
097:                        .getPrimitiveBinding(Integer.class);
098:
099:                // use String serial binding for data entries
100:                SerialBinding dataBinding = new SerialBinding(catalog,
101:                        String.class);
102:
103:                this .db = env.openDatabase(null, "helloworld", dbConfig);
104:
105:                // create a map view of the database
106:                this .map = new StoredSortedMap(db, keyBinding, dataBinding,
107:                        true);
108:            }
109:
110:            /** Closes the database. */
111:            private void close() throws Exception {
112:
113:                if (catalog != null) {
114:                    catalog.close();
115:                    catalog = null;
116:                }
117:                if (db != null) {
118:                    db.close();
119:                    db = null;
120:                }
121:                if (env != null) {
122:                    env.close();
123:                    env = null;
124:                }
125:            }
126:
127:            /** Writes and reads the database via the Map. */
128:            private void writeAndRead() {
129:
130:                // check for existing data
131:                Integer key = new Integer(0);
132:                String val = (String) map.get(key);
133:                if (val == null) {
134:                    System.out.println("Writing data");
135:                    // write in reverse order to show that keys are sorted
136:                    for (int i = INT_NAMES.length - 1; i >= 0; i -= 1) {
137:                        map.put(new Integer(i), INT_NAMES[i]);
138:                    }
139:                }
140:                // get iterator over map entries
141:                Iterator iter = map.entrySet().iterator();
142:                System.out.println("Reading data");
143:                while (iter.hasNext()) {
144:                    Map.Entry entry = (Map.Entry) iter.next();
145:                    System.out.println(entry.getKey().toString() + ' '
146:                            + entry.getValue());
147:                }
148:            }
149:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.