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 a PhoneBook application that supports passivation!<br>
018: * java org.space4j.demos.phonebook.PhoneBookPassivation<br>
019: * Note: We are not indexing anything here on porpouse.
020: */
021: public class PhoneBookPassivation {
022:
023: protected Space4J space4j = null;
024: protected Space space = null;
025:
026: public PhoneBookPassivation() throws LoggerException,
027: CommandException, UnknownHostException, IOException,
028: ClassNotFoundException {
029: // Initializes the system, passing the dir where the logs and snapshots are stored.
030: // NOTE: The system will load everything in memory here. Snapshot and last commands.
031: space4j = new SimpleSpace4J("PhoneBookPassivation");
032:
033: // Start the system...
034: space4j.start();
035:
036: // Grab the space where all data resides...
037: space = space4j.getSpace();
038:
039: // If this is the first time, create our main hashmap...
040: // We are not indexing anything here. This is another topic...
041: if (space.getObject("phonenumbers") == null) {
042: space4j.executeCommand(new ObjCreateCmd("phonenumbers",
043: new PassivationMap(space4j)));
044: }
045:
046: // Create a sequence for our objects...
047: // This is just an Integer in memory !!!
048: if (space.getObject("phonenumbers_seq") == null) {
049: space4j.executeCommand(new ObjCreateCmd("phonenumbers_seq",
050: new Integer(0)));
051: }
052:
053: }
054:
055: public void executeSnapshot() throws LoggerException {
056: space4j.executeSnapshot();
057: }
058:
059: public synchronized void addNumber(String name, String number)
060: throws CommandException, LoggerException {
061: int seq = space4j.executeCommand(new IncrementSeqCmd(
062: "phonenumbers_seq"));
063: PhoneBookRecord phone = new PhoneBookRecord(seq);
064: phone.setName(name);
065: phone.setNumber(number);
066: space4j.executeCommand(new MapPutCmd("phonenumbers",
067: new Integer(seq), phone));
068: }
069:
070: // no indexing, on porpouse...
071: private PassivationObj getByName(String name) {
072: Map phonenumbers = (Map) space.getObject("phonenumbers");
073: if (phonenumbers == null)
074: return null;
075:
076: Iterator iter = phonenumbers.values().iterator();
077: while (iter.hasNext()) {
078: PassivationObj po = (PassivationObj) iter.next();
079: PhoneBookRecord phone = (PhoneBookRecord) po.get();
080: if (phone.getName().equalsIgnoreCase(name)) {
081: return po;
082: }
083: }
084: return null;
085: }
086:
087: public String getNumber(String name) {
088: PassivationObj po = getByName(name);
089: if (po == null)
090: return null;
091: PhoneBookRecord phone = (PhoneBookRecord) po.get();
092: return phone.getNumber();
093: }
094:
095: public boolean delNumber(String name) throws CommandException,
096: LoggerException {
097: PassivationObj po = getByName(name);
098: if (po == null)
099: return false;
100: PhoneBookRecord phone = (PhoneBookRecord) po.get();
101: int id = phone.getId();
102: int x = space4j.executeCommand(new MapRemoveCmd("phonenumbers",
103: new Integer(id)));
104: if (x > 0)
105: return true;
106: return false;
107: }
108:
109: public ArrayList getNames() {
110: Iterator iter = space.getSafeIterator("phonenumbers");
111: if (iter == null)
112: return null;
113: ArrayList list = new ArrayList();
114: while (iter.hasNext()) {
115: PassivationObj po = (PassivationObj) iter.next();
116: PhoneBookRecord phone = (PhoneBookRecord) po.get();
117: list.add(phone.getName());
118: }
119: return list;
120: }
121:
122: // This has nothing to do with Space4J.
123: // Just a simple logic for a minimal PhoneBook application.
124: // ToDo: Make it a Swing application. (Anyone?)
125: public static void main(String[] args) throws Exception {
126: PhoneBookPassivation book = new PhoneBookPassivation();
127: BufferedReader input = new BufferedReader(
128: new InputStreamReader(System.in));
129: System.out
130: .print("(L)ist | (A)dd | (F)ind | (R)emove | (S)napshot | (Q)uit => ");
131: while (true) {
132: String cmd = input.readLine();
133: System.out.println();
134: if (cmd.equalsIgnoreCase("l")) {
135: ArrayList list = book.getNames();
136: if (list == null || list.size() == 0)
137: System.out.println("No entries in phone book!\n");
138: else {
139: Iterator iter = list.iterator();
140: while (iter.hasNext()) {
141: String key = (String) iter.next();
142: String value = book.getNumber(key);
143: System.out.println(key + ": " + value);
144: }
145: System.out
146: .println("\nTotal: " + list.size() + "\n");
147: }
148: } else if (cmd.equalsIgnoreCase("a")) {
149: System.out.print("Name: ");
150: String name = input.readLine();
151: System.out.print("Tel: ");
152: String tel = input.readLine();
153: if (!name.trim().equals("") && !tel.trim().equals("")) {
154: book.addNumber(name, tel);
155: System.out.println(name + ": " + tel + " added!\n");
156: }
157: } else if (cmd.equalsIgnoreCase("f")) {
158: System.out.print("Name: ");
159: String name = input.readLine();
160: String tel = book.getNumber(name);
161: if (tel != null) {
162: System.out.println("Number: " + tel + "\n");
163: } else {
164: System.out.println("Nothing found!\n");
165: }
166: } else if (cmd.equalsIgnoreCase("r")) {
167: System.out.print("Name: ");
168: String name = input.readLine();
169: if (book.delNumber(name)) {
170: System.out.println("Item removed!\n");
171: } else {
172: System.out.println("Not found!\n");
173: }
174: } else if (cmd.equalsIgnoreCase("s")) {
175: book.executeSnapshot();
176: System.out.println("Snapshot taken!\n");
177: } else if (cmd.equalsIgnoreCase("q")) {
178: break;
179: }
180: System.out
181: .print("(L)ist | (A)dd | (F)ind | (R)emove | (S)napshot | (Q)uit => ");
182: }
183: }
184: }
|