📄 persistentstoremanager.java
字号:
/** * Copyright (C) 2003-2004 Funambol * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package sync4j.framework.server.store;import java.util.Map;import java.util.HashMap;import java.io.Serializable;import sync4j.framework.server.store.Clause;import sync4j.framework.server.store.PersistentStore;import sync4j.framework.server.store.PersistentStoreException;import sync4j.framework.tools.beans.BeanFactory;/** * This class represents the main persistent store of the Sync4j server. It * handles everything related to saving and reading information to and from the * database, delegating the work to other <i>PersistentStore</i>s if necessary. * <p> * <i>PersistentStoreManager</i> can be configured with a list of <i>PersistetStore</i>s * that are called in sequence until one of them can process the given object.<br> * This list is expressed in the form of the string array <i>stores</i>; each * string is the name of a bean (or a class) and is loaded by * <i>sync4j.framework.tools.beans.BeanFramework</i>. * <p> * * @author Stefano Fornari @ Funambol * * @version $Id: PersistentStoreManager.java,v 1.11 2004/04/13 09:37:34 luigia Exp $ */public class PersistentStoreManager implements PersistentStore, Serializable { // --------------------------------------------------------------- Constants public static final String CONFIG_CLASS_LOADER = "class-loader"; public static final String CONFIG_JNDI_DATA_SOURCE_NAME = "jndi-data-source-name"; public static final String CONFIG_USERNAME = "username"; public static final String CONFIG_PASSWORD = "password"; // ------------------------------------------------------------ Private data private PersistentStore persistentStores[] = null; // ------------------------------------------------------------ Constructors // -------------------------------------------------------------- Properties /** * The persistent stores handled by this manager */ private String[] stores = null; /** Getter for property stores. * @return Value of property stores. * */ public String[] getStores() { return this.stores; } /** Setter for property stores. * @param stores New value of property stores. * */ public void setStores(String[] stores) { this.stores = stores; } /** * The JNDI name of the datasource to be used */ private String jndiDataSourceName = null; public String getJndiDataSourceName() { return this.jndiDataSourceName; } public void setJndiDataSourceName(String jndiDataSourceName) { this.jndiDataSourceName = jndiDataSourceName; } /** * The database user */ private String username = null; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } /** * The database password */ private String password = null; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } // ---------------------------------------------------------- Public methods public boolean store(Object o) throws PersistentStoreException { for (int i=0; ((persistentStores != null) && (i<persistentStores.length)); ++i) { if (persistentStores[i].store(o)) { return true; } } return false; } public boolean read(Object o) throws PersistentStoreException { for (int i=0; ((persistentStores != null) && (i<persistentStores.length)); ++i) { if (persistentStores[i].read(o)) { return true; } } return false; } /** Read all objects stored the persistent media. * * @return an array containing the objects read. If no objects are found an * empty array is returned. If the persistent store has not * processed the quest, null is returned. * * @throws PersistentStoreException * */ public Object[] read(Class objClass) throws PersistentStoreException { Object[] objs = null; for (int i=0; ((persistentStores != null) && (i<persistentStores.length)); ++i) { if ((objs = persistentStores[i].read(objClass)) != null) { return objs; } } return null; } /** Configures the persistent store. The following configuration parameters * are required: * <ul> * <li>class-loader - the class loader to be used to load persistent sotres * </ul> * * @param config an <i>Map</i> containing configuration parameters. * * @throws ConfigPersistentStoreException * */ public void configure(Map config) throws ConfigPersistentStoreException { // // Checks required configuration parameters // ClassLoader classLoader = (ClassLoader)config.get(CONFIG_CLASS_LOADER); if (classLoader == null) { throw new ConfigPersistentStoreException( "Missing required parameter: " + CONFIG_CLASS_LOADER ); } // // Instantiates the persistent stores // if (stores == null) { return; } persistentStores = new PersistentStore[stores.length]; // // Prepares the configuration map for the persistent stores // config = new HashMap(3); config.put(CONFIG_JNDI_DATA_SOURCE_NAME, jndiDataSourceName); config.put(CONFIG_USERNAME, username); config.put(CONFIG_PASSWORD, password); // // Creates and configures the managed persistent stores // int i = 0; try { for (; ((stores != null) && (i<stores.length)); ++i) { persistentStores[i] = (PersistentStore)BeanFactory.getBeanInstance(classLoader, stores[i]); persistentStores[i].configure(config); } } catch (Exception e) { throw new ConfigPersistentStoreException( "Error in loading the persistent store " + stores[i] + ": " + e.getMessage() , e ); } } public String toString() { StringBuffer sb = new StringBuffer(); sb.append(getClass().getName()).append(" - {"); sb.append("jndiDataSourceName: ").append(jndiDataSourceName); sb.append("; stores: "); for (int i=0; ((stores != null) && (i<stores.length)); ++i) { if (i>0) { sb.append(","); } sb.append(stores[i]); } return sb.toString(); } public boolean delete(Object o) throws PersistentStoreException { for (int i=0; ((persistentStores != null) && (i<persistentStores.length)); ++i) { if (persistentStores[i].delete(o)) { return true; } } return false; } public Object[] read(Object o, Clause clause) throws PersistentStoreException { Object[] objs = null; for (int i=0; ((persistentStores != null) && (i<persistentStores.length)); ++i) { if ((objs = persistentStores[i].read(o, clause)) != null) { return objs; } } return null; } public boolean store(String id, Object o, String operation) throws PersistentStoreException { for (int i=0; ((persistentStores != null) && (i<persistentStores.length)); ++i) { if (persistentStores[i].store(id, o, operation)) { return true; } } return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -