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.passivation.*;
008: import org.space4j.implementation.*;
009: import org.space4j.commands.*;
010: import org.space4j.*;
011:
012: import java.util.*;
013: import java.io.*;
014: import java.net.*;
015:
016: /**
017: * This is to show you how simple it is to use passivation in a cluster !!!
018: * To make a cluster, open up two shells (or DOS) and execute in the first:<br>
019: * java org.space4j.demos.phonebook.PhoneBookPassivationCluster master<br><br>
020: * and in the second:<br>
021: * java org.space4j.demos.PhoneBookPassivationCluster slave 127.0.0.1<br><br>
022: * You can also use two different machines. Just pass the master IP address to the slave instead of 127.0.0.1.<br>
023: * IMPORTANT: You must have the main Space4J dir (space4j_db) mounted, so both machines can have access to it.<br>
024: * Each machine must access its own space4j_pv dir for passivation.
025: */
026: public class PhoneBookPassivationCluster {
027:
028: protected Space4J space4j = null;
029: protected Space space = null;
030:
031: public PhoneBookPassivationCluster(boolean master, String master_ip)
032: throws LoggerException, CommandException,
033: UnknownHostException, IOException, ClassNotFoundException {
034:
035: // Initializes the system, passing the dir where the logs and snapshots are stored.
036: // NOTE: The system will load everything in memory here. Snapshot and last commands.
037: if (master) {
038: space4j = new MasterSpace4J("PhoneBookPassivation");
039: } else {
040: space4j = new SlaveSpace4J("PhoneBookPassivation",
041: master_ip);
042: }
043:
044: // Start the system...
045: space4j.start();
046:
047: // Grab the space where all data resides...
048: space = space4j.getSpace();
049:
050: // If this is the first time, create our main hashmap...
051: // Note: To avoid any race-condition, only the master will try to do this...
052: if (master && space.getObject("phonenumbers") == null) {
053: space4j.executeCommand(new ObjCreateCmd("phonenumbers",
054: new PassivationMap(space4j)));
055: }
056:
057: // Create a sequence for our objects...
058: if (master && space.getObject("phonenumbers_seq") == null) {
059: space4j.executeCommand(new ObjCreateCmd("phonenumbers_seq",
060: new Integer(0)));
061: }
062: }
063:
064: public void executeSnapshot() throws LoggerException {
065: space4j.executeSnapshot();
066: }
067:
068: public synchronized void addNumber(String name, String number)
069: throws CommandException, LoggerException {
070: int seq = space4j.executeCommand(new IncrementSeqCmd(
071: "phonenumbers_seq"));
072: PhoneBookRecord phone = new PhoneBookRecord(seq);
073: phone.setName(name);
074: phone.setNumber(number);
075: space4j.executeCommand(new MapPutCmd("phonenumbers",
076: new Integer(seq), phone));
077: }
078:
079: // no indexing, on porpouse...
080: private PassivationObj getByName(String name) {
081: Map phonenumbers = (Map) space.getObject("phonenumbers");
082: if (phonenumbers == null)
083: return null;
084:
085: Iterator iter = phonenumbers.values().iterator();
086: while (iter.hasNext()) {
087: PassivationObj po = (PassivationObj) iter.next();
088: PhoneBookRecord phone = (PhoneBookRecord) po.get();
089: if (phone.getName().equalsIgnoreCase(name)) {
090: return po;
091: }
092: }
093: return null;
094: }
095:
096: public String getNumber(String name) {
097: PassivationObj po = getByName(name);
098: if (po == null)
099: return null;
100: PhoneBookRecord phone = (PhoneBookRecord) po.get();
101: return phone.getNumber();
102: }
103:
104: public boolean delNumber(String name) throws CommandException,
105: LoggerException {
106: PassivationObj po = getByName(name);
107: if (po == null)
108: return false;
109: PhoneBookRecord phone = (PhoneBookRecord) po.get();
110: int id = phone.getId();
111: int x = space4j.executeCommand(new MapRemoveCmd("phonenumbers",
112: new Integer(id)));
113: if (x > 0)
114: return true;
115: return false;
116: }
117:
118: public ArrayList getNames() {
119: Iterator iter = space.getSafeIterator("phonenumbers");
120: if (iter == null)
121: return null;
122: ArrayList list = new ArrayList();
123: while (iter.hasNext()) {
124: PassivationObj po = (PassivationObj) iter.next();
125: PhoneBookRecord phone = (PhoneBookRecord) po.get();
126: list.add(phone.getName());
127: }
128: return list;
129: }
130:
131: // This has nothing to do with Space4J.
132: // Just a simple logic for a minimal PhoneBook application.
133: // ToDo: Make it a Swing application. (Anyone?)
134: public static void main(String[] args) throws Exception {
135: boolean master = false;
136: String master_ip = null;
137:
138: if (args[0].equals("master"))
139: master = true;
140: else if (args[0].equals("slave")) {
141: master = false;
142: if (args.length > 1)
143: master_ip = args[1];
144: else
145: master_ip = "127.0.0.1";
146: }
147:
148: PhoneBookPassivationCluster book = new PhoneBookPassivationCluster(
149: master, master_ip);
150: BufferedReader input = new BufferedReader(
151: new InputStreamReader(System.in));
152: System.out
153: .print("(L)ist | (A)dd | (F)ind | (R)emove | (S)napshot | (Q)uit => ");
154: while (true) {
155: String cmd = input.readLine();
156: System.out.println();
157: if (cmd.equalsIgnoreCase("l")) {
158: ArrayList list = book.getNames();
159: if (list == null || list.size() == 0)
160: System.out.println("No entries in phone book!\n");
161: else {
162: Iterator iter = list.iterator();
163: while (iter.hasNext()) {
164: String key = (String) iter.next();
165: String value = book.getNumber(key);
166: System.out.println(key + ": " + value);
167: }
168: System.out
169: .println("\nTotal: " + list.size() + "\n");
170: }
171: } else if (cmd.equalsIgnoreCase("a")) {
172: System.out.print("Name: ");
173: String name = input.readLine();
174: System.out.print("Tel: ");
175: String tel = input.readLine();
176: if (!name.trim().equals("") && !tel.trim().equals("")) {
177: book.addNumber(name, tel);
178: System.out.println(name + ": " + tel + " added!\n");
179: }
180: } else if (cmd.equalsIgnoreCase("f")) {
181: System.out.print("Name: ");
182: String name = input.readLine();
183: String tel = book.getNumber(name);
184: if (tel != null) {
185: System.out.println("Number: " + tel + "\n");
186: } else {
187: System.out.println("Nothing found!\n");
188: }
189: } else if (cmd.equalsIgnoreCase("r")) {
190: System.out.print("Name: ");
191: String name = input.readLine();
192: if (book.delNumber(name)) {
193: System.out.println("Item removed!\n");
194: } else {
195: System.out.println("Not found!\n");
196: }
197: } else if (cmd.equalsIgnoreCase("s")) {
198: book.executeSnapshot();
199: System.out.println("Snapshot taken!\n");
200: } else if (cmd.equalsIgnoreCase("q")) {
201: break;
202: }
203: System.out
204: .print("(L)ist | (A)dd | (F)ind | (R)emove | (S)napshot | (Q)uit => ");
205: }
206: }
207: }
|