1 18 19 package sync4j.framework.server.store; 20 21 import java.util.Map ; 22 import java.util.HashMap ; 23 import java.io.Serializable ; 24 25 import sync4j.server.config.Configuration; 26 import sync4j.framework.server.store.Clause; 27 import sync4j.framework.server.store.PersistentStore; 28 import sync4j.framework.server.store.PersistentStoreException; 29 import sync4j.framework.server.store.ConfigPersistentStoreException; 30 import sync4j.framework.tools.beans.BeanException; 31 import sync4j.framework.tools.beans.BeanFactory; 32 import sync4j.framework.tools.beans.LazyInitBean; 33 import sync4j.framework.tools.beans.BeanInitializationException; 34 35 51 public class PersistentStoreManager 52 implements PersistentStore, Serializable , LazyInitBean { 53 54 56 public static final String 57 CONFIG_JNDI_DATA_SOURCE_NAME = "jndi-data-source-name"; 58 59 public static final String 60 CONFIG_USERNAME = "username"; 61 62 public static final String 63 CONFIG_PASSWORD = "password"; 64 65 67 private PersistentStore persistentStores[] = null; 68 69 71 73 76 private String [] stores = null; 77 78 82 public String [] getStores() { 83 return this.stores; 84 } 85 86 90 public void setStores(String [] stores) { 91 this.stores = stores; 92 } 93 94 97 private String jndiDataSourceName = null; 98 99 public String getJndiDataSourceName() { 100 return this.jndiDataSourceName; 101 } 102 103 public void setJndiDataSourceName(String jndiDataSourceName) { 104 this.jndiDataSourceName = jndiDataSourceName; 105 } 106 107 110 private String username = null; 111 112 public String getUsername() { 113 return username; 114 } 115 116 public void setUsername(String username) { 117 this.username = username; 118 } 119 120 123 private String password = null; 124 125 public String getPassword() { 126 return password; 127 } 128 129 public void setPassword(String password) { 130 this.password = password; 131 } 132 133 135 public boolean store(Object o) 136 throws PersistentStoreException { 137 for (int i=0; ((persistentStores != null) && (i<persistentStores.length)); ++i) { 138 if (persistentStores[i].store(o)) { 139 return true; 140 } 141 } 142 143 return false; 144 } 145 146 public boolean read(Object o) 147 throws PersistentStoreException { 148 for (int i=0; ((persistentStores != null) && (i<persistentStores.length)); ++i) { 149 if (persistentStores[i].read(o)) { 150 return true; 151 } 152 } 153 154 return false; 155 } 156 157 166 public Object [] read(Class objClass) throws PersistentStoreException { 167 Object [] objs = null; 168 for (int i=0; ((persistentStores != null) && (i<persistentStores.length)); ++i) { 169 if ((objs = persistentStores[i].read(objClass)) != null) { 170 return objs; 171 } 172 } 173 174 return null; 175 } 176 177 195 public void configure(final Map config) 196 throws ConfigPersistentStoreException { 197 if (persistentStores == null) { 198 return; 199 } 200 201 for (int i=0; i<persistentStores.length; ++i) { 202 persistentStores[i].configure(config); 203 } 204 } 205 206 219 public void init() 220 throws BeanInitializationException{ 221 if ((stores == null) || (stores.length == 0)) { 225 return; 226 } 227 228 persistentStores = new PersistentStore[stores.length]; 229 230 231 Configuration config = Configuration.getConfiguration(); 235 236 try { 237 for (int i=0; ((stores != null) && (i<stores.length)); ++i) { 238 persistentStores[i] = 239 (PersistentStore)config.getBeanInstanceByName(stores[i]); 240 } 241 } catch (Exception e) { 242 throw new BeanInitializationException(e.getMessage(), e.getCause()); 243 } 244 245 246 try { 247 HashMap props = new HashMap (3); 251 props.put(CONFIG_JNDI_DATA_SOURCE_NAME, jndiDataSourceName); 252 props.put(CONFIG_USERNAME, username); 253 props.put(CONFIG_PASSWORD, password); 254 255 configure(props); 256 257 } catch (ConfigPersistentStoreException e) { 258 throw new BeanInitializationException(e.getMessage(), e.getCause()); 259 } 260 } 261 262 public String toString() { 263 StringBuffer sb = new StringBuffer (); 264 265 sb.append(getClass().getName()).append(" - {"); 266 sb.append("jndiDataSourceName: ").append(jndiDataSourceName); 267 sb.append("; stores: "); 268 for (int i=0; ((stores != null) && (i<stores.length)); ++i) { 269 if (i>0) { 270 sb.append(","); 271 } 272 sb.append(stores[i]); 273 } 274 275 return sb.toString(); 276 } 277 278 public boolean delete(Object o) throws PersistentStoreException 279 { 280 for (int i=0; ((persistentStores != null) && (i<persistentStores.length)); ++i) { 281 if (persistentStores[i].delete(o)) { 282 return true; 283 } 284 } 285 286 return false; 287 } 288 289 public Object [] read(Object o, Clause clause) throws PersistentStoreException 290 { 291 Object [] objs = null; 292 for (int i=0; ((persistentStores != null) && (i<persistentStores.length)); ++i) { 293 if ((objs = persistentStores[i].read(o, clause)) != null) { 294 return objs; 295 } 296 } 297 298 return null; 299 } 300 301 public int count(Object o, Clause clause) throws PersistentStoreException 302 { 303 int count = -1; 304 for (int i=0; ((persistentStores != null) && (i<persistentStores.length)); ++i) { 305 if ((count = persistentStores[i].count(o, clause)) != -1) { 306 return count; 307 } 308 } 309 return -1; 310 } 311 312 } 313 | Popular Tags |