001: package org.jgroups.persistence;
002:
003: /**
004: * @author Mandar Shinde
005: * The class implements the PersistenceManager interface and provides users
006: * a file based implementation when required.
007: * The state of this class is current NOOP. Implementation will be in place
008: * once a better structure for file based properties will be designed.
009: */
010:
011: import java.io.*;
012: import java.util.*;
013:
014: public class FilePersistenceManager implements PersistenceManager {
015: private final File file;
016:
017: /**
018: * Default constructor
019: */
020: public FilePersistenceManager(String propertiesFilename)
021: throws Exception {
022: Properties properties = new Properties();
023: properties.load(new FileInputStream(propertiesFilename));
024: String path = properties
025: .getProperty(PersistenceFactory.persistProp);
026: file = new File(path);
027: file.createNewFile();
028: }
029:
030: /**
031: * Save new NV pair as serializable objects or if already exist; store
032: * new state
033: */
034: public void save(Serializable key, Serializable val)
035: throws CannotPersistException {
036: try {
037: Map map = retrieveAll();
038: map.put(key, val);
039: saveAll(map);
040: } catch (CannotRetrieveException e) {
041: throw new CannotPersistException(e,
042: "Unable to pre-load existing store.");
043: }
044: }
045:
046: /**
047: * Remove existing NV from being persisted
048: */
049: public Serializable remove(Serializable key)
050: throws CannotRemoveException {
051: Object o;
052: try {
053: Map map = retrieveAll();
054: o = map.remove(key);
055: saveAll(map);
056: } catch (CannotRetrieveException e) {
057: throw new CannotRemoveException(e,
058: "Unable to pre-load existing store.");
059: } catch (CannotPersistException e) {
060: throw new CannotRemoveException(e,
061: "Unable to pre-load existing store.");
062: }
063: return (Serializable) o;
064: }
065:
066: /**
067: * Use to store a complete map into persistent state
068: * @exception CannotPersistException;
069: */
070: public void saveAll(Map map) throws CannotPersistException {
071: try {
072: OutputStream fos = new FileOutputStream(file);
073: Properties prop = new Properties();
074: // NB: For some reason Properties.putAll(map) doesn't seem to work - dimc@users.sourceforge.net
075: for (Iterator iterator = map.entrySet().iterator(); iterator
076: .hasNext();) {
077: Map.Entry entry = (Map.Entry) iterator.next();
078: prop.setProperty(entry.getKey().toString(), entry
079: .getValue().toString());
080: }
081: prop.store(fos, null);
082: fos.flush();
083: fos.close();
084: } catch (IOException e) {
085: throw new CannotPersistException(e, "Cannot save to: "
086: + file.getAbsolutePath());
087: }
088: }
089:
090: /**
091: * Gives back the Map in last known state
092: * @return Map;
093: * @exception CannotRetrieveException;
094: */
095: public Map retrieveAll() throws CannotRetrieveException {
096: try {
097: Properties prop = new Properties();
098: FileInputStream fis = new FileInputStream(file);
099: prop.load(fis);
100: fis.close();
101: return filterLoadedValues(prop);
102: } catch (IOException e) {
103: throw new CannotRetrieveException(e,
104: "Unable to load from file: "
105: + file.getAbsolutePath());
106: }
107: }
108:
109: /**
110: * Turns the values into Floats to enable
111: * {@link org.jgroups.demos.DistributedHashtableDemo} to work.
112: * Subclasses should override this method to convert the incoming map
113: * of string/string key/value pairs into the types they want.
114: * @param in
115: * @return Map
116: */
117: protected Map filterLoadedValues(Map in) {
118: Map out = new HashMap();
119: for (Iterator iterator = in.entrySet().iterator(); iterator
120: .hasNext();) {
121: Map.Entry entry = (Map.Entry) iterator.next();
122: out.put(entry.getKey().toString(), Float.valueOf(entry
123: .getValue().toString()));
124: }
125: return out;
126: }
127:
128: /**
129: * Clears the complete NV state from the DB
130: * @exception CannotRemoveException;
131: x*/
132: public void clear() throws CannotRemoveException {
133: try {
134: saveAll(Collections.EMPTY_MAP);
135: } catch (CannotPersistException e) {
136: throw new CannotRemoveException(e, "Unable to clear map.");
137: }
138: }
139:
140: /**
141: * Used to handle shutdown call the PersistenceManager implementation.
142: * Persistent engines can leave this implementation empty.
143: */
144: public void shutDown() {
145: return;
146: }
147: }// end of class
|