1 package org.jgroups.persistence; 2 3 9 10 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 14 import java.io.FileInputStream ; 15 import java.io.IOException ; 16 import java.lang.reflect.Constructor ; 17 import java.util.Properties ; 18 19 20 public class PersistenceFactory 21 { 22 23 protected final Log log=LogFactory.getLog(this.getClass()); 24 25 28 private PersistenceFactory() 29 { 30 } 31 32 33 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 52 public synchronized void registerManager(PersistenceManager manager) 53 { 54 _manager = manager; 55 } 56 57 65 public synchronized PersistenceManager createManager() throws Exception { 66 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 86 public synchronized PersistenceManager createManager (String filePath) throws Exception 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 104 private PersistenceManager createManagerDB(String filePath) throws Exception 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 } 113 117 private PersistenceManager createManagerFile(String filePath) 118 { 119 120 if(log.isInfoEnabled()) log.info("Creating file manager: " + filePath); 121 Properties props; 122 123 try 124 { 125 if (_manager == null) 126 { 127 props=readProps(filePath); 128 String classname=props.getProperty(filePersistMgr); 129 if(classname != null) 130 { 131 Class cl=Thread.currentThread().getContextClassLoader().loadClass(classname); 132 Constructor ctor=cl.getConstructor(new Class []{String .class}); 133 _manager=(PersistenceManager)ctor.newInstance(new Object []{filePath}); 134 } 135 else 136 { 137 _manager = new FilePersistenceManager(filePath); 138 } 139 } 140 return _manager; 141 } 142 catch (Throwable t) 143 { 144 t.printStackTrace(); 145 return null; 146 } 147 } 149 150 155 private boolean checkDB() throws Exception 156 { 157 Properties props=readProps(propPath); 158 String persist = props.getProperty(persistProp); 159 if ("DB".equals(persist)) 160 return true; 161 return false; 162 } 163 164 165 166 167 171 private boolean checkDB(String filePath) throws Exception 172 { 173 Properties props=readProps(filePath); 174 String persist = props.getProperty(persistProp); 175 if ("DB".equals(persist)) 176 return true; 177 return false; 178 } 179 180 181 Properties readProps(String fileName) throws IOException 182 { 183 Properties props; 184 FileInputStream _stream = new FileInputStream (fileName); 185 props=new Properties (); 186 props.load(_stream); 187 return props; 188 } 189 190 private static PersistenceManager _manager = null; 191 private static PersistenceFactory _factory = null; 192 193 194 195 final static String propPath = "persist.properties"; 196 final static String persistProp = "persist"; 197 198 199 final static String filePersistMgr="filePersistMgr"; 200 } 201 | Popular Tags |