KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > persistence > PersistenceFactory


1 package org.jgroups.persistence;
2
3 /**
4  * @author Mandar Shinde
5  * This class is the factory to get access to any DB based or file based
6  * implementation. None of the implemenations should expose directly
7  * to user for migration purposes
8  */

9
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13
14 import java.io.FileInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.lang.reflect.Constructor JavaDoc;
17 import java.util.Properties JavaDoc;
18
19
20 public class PersistenceFactory
21 {
22
23     protected final Log log=LogFactory.getLog(this.getClass());
24
25     /**
26      * Default private constructor// does nothing
27      */

28     private PersistenceFactory()
29     {
30     }
31
32
33     /**
34      * Singular public method to get access to any of the Persistence
35      * Manager implementations. It is important to known at this point
36      * that properties determine the implementation of the Persistence
37      * Manager, there is no direct interface which gives access to
38      * either DB implemented ot FILE implemented storage API.
39      * @return PersistenceFactory;
40      */

41     public static PersistenceFactory getInstance() {
42         System.err.println(" getting factory instance ");
43         if (_factory == null)
44             _factory = new PersistenceFactory();
45         return _factory;
46     }
47
48     /**
49      * Register a custom persistence manager as opposed to the
50      * {@link FilePersistenceManager} or {@link DBPersistenceManager}.
51      */

52     public synchronized void registerManager(PersistenceManager manager)
53     {
54         _manager = manager;
55     }
56
57     /**
58      * Reads the default properties and creates a persistencemanager
59      * The default properties are picked up from the $USER_HOME or
60      * from the classpath. Default properties are represented by
61      * "persist.properties"
62      * @return PersistenceManager
63      * @exception Exception;
64      */

65     public synchronized PersistenceManager createManager() throws Exception JavaDoc {
66         // will return null if not initialized
67
// uses default properties
68
if (_manager == null)
69         {
70             if (checkDB())
71                 _manager = createManagerDB(propPath);
72             else
73                 _manager = createManagerFile(propPath);
74         }
75         return _manager;
76     }
77
78
79     /**
80      * Duplicated signature to create PersistenceManager to allow user to
81      * provide property path.
82      * @param filePath complete pathname to get the properties
83      * @return PersistenceManager;
84      * @exception Exception;
85      */

86     public synchronized PersistenceManager createManager (String JavaDoc filePath) throws Exception JavaDoc
87     {
88         if (_manager == null)
89         {
90             if (checkDB(filePath))
91                 _manager = createManagerDB(filePath);
92             else
93                 _manager = createManagerFile(filePath);
94         }
95         return _manager;
96     }
97
98
99
100     /**
101      * Internal creator of DB persistence manager, the outside user accesses only
102      * the PersistenceManager interface API
103      */

104     private PersistenceManager createManagerDB(String JavaDoc filePath) throws Exception JavaDoc
105     {
106
107             if(log.isInfoEnabled()) log.info("Calling db persist from factory: " + filePath);
108         if (_manager == null)
109             _manager = new DBPersistenceManager(filePath);
110         return _manager;
111     }// end of DB persistence
112

113     /**
114      * creates instance of file based persistency manager
115      * @return PersistenceManager
116      */

117     private PersistenceManager createManagerFile(String JavaDoc filePath)
118     {
119
120             if(log.isInfoEnabled()) log.info("Creating file manager: " + filePath);
121         Properties JavaDoc props;
122
123         try
124         {
125             if (_manager == null)
126             {
127                 props=readProps(filePath);
128                 String JavaDoc classname=props.getProperty(filePersistMgr);
129                 if(classname != null)
130                 {
131                     Class JavaDoc cl=Thread.currentThread().getContextClassLoader().loadClass(classname);
132                     Constructor JavaDoc ctor=cl.getConstructor(new Class JavaDoc[]{String JavaDoc.class});
133                     _manager=(PersistenceManager)ctor.newInstance(new Object JavaDoc[]{filePath});
134                 }
135                 else
136                 {
137                     _manager = new FilePersistenceManager(filePath);
138                 }
139             }
140             return _manager;
141         }
142         catch (Throwable JavaDoc t)
143         {
144             t.printStackTrace();
145             return null;
146         }
147     }// end of file persistence
148

149
150     /**
151      * checks the default properties for DB/File flag
152      * @return boolean;
153      * @exception Exception;
154      */

155     private boolean checkDB() throws Exception JavaDoc
156     {
157         Properties JavaDoc props=readProps(propPath);
158         String JavaDoc persist = props.getProperty(persistProp);
159         if ("DB".equals(persist))
160             return true;
161         return false;
162     }
163
164
165
166
167     /**
168      * checks the provided properties for DB/File flag
169      * @return boolean;
170      */

171     private boolean checkDB(String JavaDoc filePath) throws Exception JavaDoc
172     {
173         Properties JavaDoc props=readProps(filePath);
174         String JavaDoc persist = props.getProperty(persistProp);
175         if ("DB".equals(persist))
176             return true;
177         return false;
178     }
179
180
181     Properties JavaDoc readProps(String JavaDoc fileName) throws IOException JavaDoc
182     {
183         Properties JavaDoc props;
184         FileInputStream JavaDoc _stream = new FileInputStream JavaDoc(fileName);
185         props=new Properties JavaDoc();
186         props.load(_stream);
187         return props;
188     }
189
190     private static PersistenceManager _manager = null;
191     private static PersistenceFactory _factory = null;
192    
193
194     /* Please set this according to configuration */
195     final static String JavaDoc propPath = "persist.properties";
196     final static String JavaDoc persistProp = "persist";
197
198     /** The class that implements a file-based PersistenceManager */
199     final static String JavaDoc filePersistMgr="filePersistMgr";
200 }
201
Popular Tags