001: package org.jgroups.persistence;
002:
003: /**
004: * @author Mandar Shinde
005: * This class is the factory to get access to any DB based or file based
006: * implementation. None of the implementations should expose directly
007: * to user for migration purposes
008: */
009:
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012: import org.jgroups.util.Util;
013:
014: import java.io.FileInputStream;
015: import java.io.IOException;
016: import java.lang.reflect.Constructor;
017: import java.util.Properties;
018:
019: public class PersistenceFactory {
020:
021: protected final static Log log = LogFactory
022: .getLog(PersistenceFactory.class);
023:
024: /**
025: * Default private constructor// does nothing
026: */
027: private PersistenceFactory() {
028: }
029:
030: /**
031: * Singular public method to get access to any of the Persistence
032: * Manager implementations. It is important to known at this point
033: * that properties determine the implementation of the Persistence
034: * Manager, there is no direct interface which gives access to
035: * either DB implemented ot FILE implemented storage API.
036: * @return PersistenceFactory;
037: */
038: public static PersistenceFactory getInstance() {
039: log.error(" getting factory instance ");
040: if (_factory == null)
041: _factory = new PersistenceFactory();
042: return _factory;
043: }
044:
045: /**
046: * Register a custom persistence manager as opposed to the
047: * {@link FilePersistenceManager} or {@link DBPersistenceManager}.
048: */
049: public synchronized void registerManager(PersistenceManager manager) {
050: _manager = manager;
051: }
052:
053: /**
054: * Reads the default properties and creates a persistencemanager
055: * The default properties are picked up from the $USER_HOME or
056: * from the classpath. Default properties are represented by
057: * "persist.properties"
058: * @return PersistenceManager
059: * @exception Exception;
060: */
061: public synchronized PersistenceManager createManager()
062: throws Exception {
063: // will return null if not initialized
064: // uses default properties
065: if (_manager == null) {
066: if (checkDB())
067: _manager = createManagerDB(propPath);
068: else
069: _manager = createManagerFile(propPath);
070: }
071: return _manager;
072: }
073:
074: /**
075: * Duplicated signature to create PersistenceManager to allow user to
076: * provide property path.
077: * @param filePath complete pathname to get the properties
078: * @return PersistenceManager;
079: * @exception Exception;
080: */
081: public synchronized PersistenceManager createManager(String filePath)
082: throws Exception {
083: if (_manager == null) {
084: if (checkDB(filePath))
085: _manager = createManagerDB(filePath);
086: else
087: _manager = createManagerFile(filePath);
088: }
089: return _manager;
090: }
091:
092: /**
093: * Internal creator of DB persistence manager, the outside user accesses only
094: * the PersistenceManager interface API
095: */
096: private PersistenceManager createManagerDB(String filePath)
097: throws Exception {
098:
099: if (log.isInfoEnabled())
100: log.info("Calling db persist from factory: " + filePath);
101: if (_manager == null)
102: _manager = new DBPersistenceManager(filePath);
103: return _manager;
104: }// end of DB persistence
105:
106: /**
107: * creates instance of file based persistency manager
108: * @return PersistenceManager
109: */
110: private PersistenceManager createManagerFile(String filePath) {
111:
112: if (log.isInfoEnabled())
113: log.info("Creating file manager: " + filePath);
114: Properties props;
115:
116: try {
117: if (_manager == null) {
118: props = readProps(filePath);
119: String classname = props.getProperty(filePersistMgr);
120: if (classname != null) {
121: Class cl = Util.loadClass(classname, this
122: .getClass());
123: Constructor ctor = cl
124: .getConstructor(new Class[] { String.class });
125: _manager = (PersistenceManager) ctor
126: .newInstance(new Object[] { filePath });
127: } else {
128: _manager = new FilePersistenceManager(filePath);
129: }
130: }
131: return _manager;
132: } catch (Throwable t) {
133: t.printStackTrace();
134: return null;
135: }
136: }// end of file persistence
137:
138: /**
139: * checks the default properties for DB/File flag
140: * @return boolean;
141: * @exception Exception;
142: */
143: private boolean checkDB() throws Exception {
144: Properties props = readProps(propPath);
145: String persist = props.getProperty(persistProp);
146: if ("DB".equals(persist))
147: return true;
148: return false;
149: }
150:
151: /**
152: * checks the provided properties for DB/File flag
153: * @return boolean;
154: */
155: private boolean checkDB(String filePath) throws Exception {
156: Properties props = readProps(filePath);
157: String persist = props.getProperty(persistProp);
158: if ("DB".equals(persist))
159: return true;
160: return false;
161: }
162:
163: Properties readProps(String fileName) throws IOException {
164: Properties props;
165: FileInputStream _stream = new FileInputStream(fileName);
166: props = new Properties();
167: props.load(_stream);
168: return props;
169: }
170:
171: private static volatile PersistenceManager _manager = null;
172: private static volatile PersistenceFactory _factory = null;
173:
174: /* Please set this according to configuration */
175: final static String propPath = "persist.properties";
176: final static String persistProp = "persist";
177:
178: /** The class that implements a file-based PersistenceManager */
179: final static String filePersistMgr = "filePersistMgr";
180: }
|