Source Code Cross Referenced for PhoneBookIndexing.java in  » Database-JDBC-Connection-Pool » Space4J » org » space4j » demos » phonebook » 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 » Database JDBC Connection Pool » Space4J » org.space4j.demos.phonebook 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Space4J(TM) - Object Persistence in RAM
002:        // Copyright (C) 2003 Sergio Oliveira Junior
003:        // This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
004:
005:        package org.space4j.demos.phonebook;
006:
007:        import org.space4j.demos.phonebook.commands.*;
008:        import org.space4j.passivation.*;
009:        import org.space4j.implementation.*;
010:        import org.space4j.commands.*;
011:        import org.space4j.indexing.*;
012:        import org.space4j.*;
013:
014:        import java.util.*;
015:        import java.io.*;
016:        import java.net.*;
017:
018:        /** 
019:         * This is a PhoneBook application that illustrates the framework for indexing.<br>
020:         * java org.space4j.demos.phonebook.PhoneBookIndexing<br>
021:         */
022:        public class PhoneBookIndexing {
023:
024:            protected Space4J space4j = null;
025:            protected Space space = null;
026:            private Index indexByName;
027:            private IndexManager im = null;
028:
029:            public PhoneBookIndexing() throws LoggerException,
030:                    CommandException, UnknownHostException, IOException,
031:                    ClassNotFoundException {
032:                // Initializes the system, passing the dir where the logs and snapshots are stored.
033:                // NOTE: The system will load everything in memory here. Snapshot and last commands.
034:                space4j = new SimpleSpace4J("PhoneBookIndexing");
035:
036:                // Start the system...
037:                space4j.start();
038:
039:                // Grab the space where all data resides...
040:                space = space4j.getSpace();
041:
042:                // If this is the first time, create our main hashmap...
043:                if (space.getObject("phonenumbers") == null) {
044:                    space4j.executeCommand(new ObjCreateCmd("phonenumbers",
045:                            new HashMap()));
046:                }
047:
048:                // Create a sequence for our objects...
049:                // This is just an Integer in memory !!!
050:                if (space.getObject("phonenumbers_seq") == null) {
051:                    space4j.executeCommand(new ObjCreateCmd("phonenumbers_seq",
052:                            new Integer(0)));
053:                }
054:
055:                // Let's create a index for our data structure....
056:                // phones by name:
057:                im = space4j.getIndexManager();
058:                indexByName = new Index("indx_phonenumbers_name",
059:                        PhoneBookRecord.class, new String[] { "name" });
060:                if (!im.checkIndex(indexByName)) {
061:                    boolean ok = im.createIndex(indexByName, "phonenumbers",
062:                            new HashMap(), space4j);
063:                    if (ok)
064:                        System.out
065:                                .println(indexByName.toString() + " created!");
066:                    else
067:                        System.out.println(indexByName.toString()
068:                                + " could not be created!");
069:                }
070:
071:            }
072:
073:            public void executeSnapshot() throws LoggerException {
074:                space4j.executeSnapshot();
075:            }
076:
077:            public synchronized void addNumber(String name, String number)
078:                    throws CommandException, LoggerException {
079:                int seq = space4j.executeCommand(new IncrementSeqCmd(
080:                        "phonenumbers_seq"));
081:                space4j.executeCommand(new CreatePhoneCmd("phonenumbers", name,
082:                        number, seq));
083:            }
084:
085:            // now we are indexing...
086:            private PhoneBookRecord getByName(String name) {
087:                Map phonesByName = (Map) im.getIndexedMap(indexByName);
088:                Key key = new Key(name);
089:                PhoneBookRecord phone = (PhoneBookRecord) phonesByName.get(key);
090:                return phone;
091:            }
092:
093:            public String getNumber(String name) {
094:                PhoneBookRecord phone = getByName(name);
095:                if (phone == null)
096:                    return null;
097:                return phone.getNumber();
098:            }
099:
100:            public boolean delNumber(String name) throws CommandException,
101:                    LoggerException {
102:                PhoneBookRecord phone = getByName(name);
103:                if (phone == null)
104:                    return false;
105:                int x = space4j.executeCommand(new RemovePhoneCmd(
106:                        "phonenumbers", phone.getId()));
107:                if (x > 0)
108:                    return true;
109:                return false;
110:            }
111:
112:            public ArrayList getNames() {
113:                Iterator iter = space.getSafeIterator("phonenumbers");
114:                if (iter == null)
115:                    return null;
116:                ArrayList list = new ArrayList();
117:                while (iter.hasNext()) {
118:                    PhoneBookRecord phone = (PhoneBookRecord) iter.next();
119:                    list.add(phone.getName());
120:                }
121:                return list;
122:            }
123:
124:            // This has nothing to do with Space4J.
125:            // Just a simple logic for a minimal PhoneBook application.
126:            // ToDo: Make it a Swing application. (Anyone?)
127:            public static void main(String[] args) throws Exception {
128:                PhoneBookIndexing book = new PhoneBookIndexing();
129:                BufferedReader input = new BufferedReader(
130:                        new InputStreamReader(System.in));
131:                System.out
132:                        .print("(L)ist | (A)dd | (F)ind | (R)emove | (S)napshot | (Q)uit => ");
133:                while (true) {
134:                    String cmd = input.readLine();
135:                    System.out.println();
136:                    if (cmd.equalsIgnoreCase("l")) {
137:                        ArrayList list = book.getNames();
138:                        if (list == null || list.size() == 0)
139:                            System.out.println("No entries in phone book!\n");
140:                        else {
141:                            Iterator iter = list.iterator();
142:                            while (iter.hasNext()) {
143:                                String key = (String) iter.next();
144:                                String value = book.getNumber(key);
145:                                System.out.println(key + ": " + value);
146:                            }
147:                            System.out
148:                                    .println("\nTotal: " + list.size() + "\n");
149:                        }
150:                    } else if (cmd.equalsIgnoreCase("a")) {
151:                        System.out.print("Name: ");
152:                        String name = input.readLine();
153:                        System.out.print("Tel: ");
154:                        String tel = input.readLine();
155:                        if (!name.trim().equals("") && !tel.trim().equals("")) {
156:                            book.addNumber(name, tel);
157:                            System.out.println(name + ": " + tel + " added!\n");
158:                        }
159:                    } else if (cmd.equalsIgnoreCase("f")) {
160:                        System.out.print("Name: ");
161:                        String name = input.readLine();
162:                        String tel = book.getNumber(name);
163:                        if (tel != null) {
164:                            System.out.println("Number: " + tel + "\n");
165:                        } else {
166:                            System.out.println("Nothing found!\n");
167:                        }
168:                    } else if (cmd.equalsIgnoreCase("r")) {
169:                        System.out.print("Name: ");
170:                        String name = input.readLine();
171:                        if (book.delNumber(name)) {
172:                            System.out.println("Item removed!\n");
173:                        } else {
174:                            System.out.println("Not found!\n");
175:                        }
176:                    } else if (cmd.equalsIgnoreCase("s")) {
177:                        book.executeSnapshot();
178:                        System.out.println("Snapshot taken!\n");
179:                    } else if (cmd.equalsIgnoreCase("q")) {
180:                        break;
181:                    }
182:                    System.out
183:                            .print("(L)ist | (A)dd | (F)ind | (R)emove | (S)napshot | (Q)uit => ");
184:                }
185:            }
186:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.