Source Code Cross Referenced for PhoneBookCluster.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:        package org.space4j.demos.phonebook;
002:
003:        import org.space4j.implementation.*;
004:        import org.space4j.commands.*;
005:        import org.space4j.*;
006:
007:        import java.util.*;
008:        import java.io.*;
009:        import java.net.*;
010:
011:        /**
012:         * This is to show you how simple it is to make a Space4J cluster!
013:         * To make a cluster, open up two shells (or DOS) and execute in the first:<br>
014:         * java org.space4j.demos.phonebook.PhoneBookCluster master<br><br>
015:         * and in the second:<br>
016:         * java org.space4j.demos.PhoneBookCluster slave 127.0.0.1<br><br>
017:         * You can also use two different machines. Just pass the master IP address to the slave instead of 127.0.0.1.<br>
018:         * IMPORTANT: You must have the main Space4J dir (space4j_db) mounted, so both machines can have access to it.
019:         */
020:        public class PhoneBookCluster extends PhoneBook {
021:
022:            public PhoneBookCluster(boolean master, String master_ip)
023:                    throws LoggerException, CommandException,
024:                    UnknownHostException, IOException, ClassNotFoundException {
025:
026:                // Initializes the system, passing the dir where the logs and snapshots are stored.
027:                // NOTE: The system will load everything in memory here. Snapshot and last commands.
028:                if (master) {
029:                    space4j = new MasterSpace4J("PhoneBook");
030:                } else {
031:                    space4j = new SlaveSpace4J("PhoneBook", master_ip);
032:                }
033:
034:                // Start the system...
035:                space4j.start();
036:
037:                // Grab the space where all data resides...        
038:                space = space4j.getSpace();
039:
040:                // If this is the first time, create our main hashmap...
041:                // Note: To avoid any race-condition, only the master will try to do this...
042:                if (master && space.getObject("phonenumbers") == null) {
043:                    space4j.executeCommand(new MapCreateCmd("phonenumbers",
044:                            new HashMap()));
045:                }
046:            }
047:
048:            // This has nothing to do with Space4J.
049:            // Just a simple logic for a minimal PhoneBook application.
050:            // ToDo: Make it a Swing application. (Anyone?)
051:            public static void main(String[] args) throws Exception {
052:                boolean master = false;
053:                String master_ip = null;
054:
055:                if (args[0].equals("master"))
056:                    master = true;
057:                else if (args[0].equals("slave")) {
058:                    master = false;
059:                    if (args.length > 1)
060:                        master_ip = args[1];
061:                    else
062:                        master_ip = "127.0.0.1";
063:                }
064:
065:                PhoneBookCluster book = new PhoneBookCluster(master, master_ip);
066:                BufferedReader input = new BufferedReader(
067:                        new InputStreamReader(System.in));
068:                System.out
069:                        .print("(L)ist | (A)dd | (F)ind | (R)emove | (S)napshot | (Q)uit => ");
070:                while (true) {
071:                    String cmd = input.readLine();
072:                    System.out.println();
073:                    if (cmd.equalsIgnoreCase("l")) {
074:                        ArrayList list = book.getNames();
075:                        if (list == null || list.size() == 0)
076:                            System.out.println("No entries in phone book!\n");
077:                        else {
078:                            Iterator iter = list.iterator();
079:                            while (iter.hasNext()) {
080:                                String key = (String) iter.next();
081:                                String value = book.getNumber(key);
082:                                System.out.println(key + ": " + value);
083:                            }
084:                            System.out
085:                                    .println("\nTotal: " + list.size() + "\n");
086:                        }
087:                    } else if (cmd.equalsIgnoreCase("a")) {
088:                        System.out.print("Name: ");
089:                        String name = input.readLine();
090:                        System.out.print("Tel: ");
091:                        String tel = input.readLine();
092:                        if (!name.trim().equals("") && !tel.trim().equals("")) {
093:                            book.addNumber(name, tel);
094:                            System.out.println(name + ": " + tel + " added!\n");
095:                        }
096:                    } else if (cmd.equalsIgnoreCase("f")) {
097:                        System.out.print("Name: ");
098:                        String name = input.readLine();
099:                        String tel = book.getNumber(name);
100:                        if (tel != null) {
101:                            System.out.println("Number: " + tel + "\n");
102:                        } else {
103:                            System.out.println("Nothing found!\n");
104:                        }
105:                    } else if (cmd.equalsIgnoreCase("r")) {
106:                        System.out.print("Name: ");
107:                        String name = input.readLine();
108:                        if (book.delNumber(name)) {
109:                            System.out.println("Item removed!\n");
110:                        } else {
111:                            System.out.println("Not found!\n");
112:                        }
113:                    } else if (cmd.equalsIgnoreCase("s")) {
114:                        book.executeSnapshot();
115:                        System.out.println("Snapshot taken!\n");
116:                    } else if (cmd.equalsIgnoreCase("q")) {
117:                        break;
118:                    }
119:                    System.out
120:                            .print("(L)ist | (A)dd | (F)ind | (R)emove | (S)napshot | (Q)uit => ");
121:                }
122:            }
123:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.