KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > server > store > PersistentStoreManager


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.framework.server.store;
20
21 import java.util.Map JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.io.Serializable JavaDoc;
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 /**
36  * This class represents the main persistent store of the Sync4j server. It
37  * handles everything related to saving and reading information to and from the
38  * database, delegating the work to other <i>PersistentStore</i>s if necessary.
39  * <p>
40  * <i>PersistentStoreManager</i> can be configured with a list of <i>PersistetStore</i>s
41  * that are called in sequence until one of them can process the given object.<br>
42  * This list is expressed in the form of the string array <i>stores</i>; each
43  * string is the name of a bean (or a class) and is loaded by
44  * <i>sync4j.framework.tools.beans.BeanFramework</i>.
45  * <p>
46  *
47  * @author Stefano Fornari @ Funambol
48  *
49  * @version $Id: PersistentStoreManager.java,v 1.18 2005/03/02 20:57:38 harrie Exp $
50  */

51 public class PersistentStoreManager
52 implements PersistentStore, Serializable JavaDoc, LazyInitBean {
53     
54     // --------------------------------------------------------------- Constants
55

56     public static final String JavaDoc
57     CONFIG_JNDI_DATA_SOURCE_NAME = "jndi-data-source-name";
58     
59     public static final String JavaDoc
60     CONFIG_USERNAME = "username";
61
62     public static final String JavaDoc
63     CONFIG_PASSWORD = "password";
64     
65     // ------------------------------------------------------------ Private data
66

67     private PersistentStore persistentStores[] = null;
68     
69     // ------------------------------------------------------------ Constructors
70

71     // -------------------------------------------------------------- Properties
72

73     /**
74      * The persistent stores handled by this manager
75      */

76     private String JavaDoc[] stores = null;
77     
78     /** Getter for property stores.
79      * @return Value of property stores.
80      *
81      */

82     public String JavaDoc[] getStores() {
83         return this.stores;
84     }
85     
86     /** Setter for property stores.
87      * @param stores New value of property stores.
88      *
89      */

90     public void setStores(String JavaDoc[] stores) {
91         this.stores = stores;
92     }
93     
94     /**
95      * The JNDI name of the datasource to be used
96      */

97     private String JavaDoc jndiDataSourceName = null;
98     
99     public String JavaDoc getJndiDataSourceName() {
100         return this.jndiDataSourceName;
101     }
102     
103     public void setJndiDataSourceName(String JavaDoc jndiDataSourceName) {
104         this.jndiDataSourceName = jndiDataSourceName;
105     }
106     
107     /**
108      * The database user
109      */

110     private String JavaDoc username = null;
111     
112     public String JavaDoc getUsername() {
113         return username;
114     }
115     
116     public void setUsername(String JavaDoc username) {
117         this.username = username;
118     }
119     
120     /**
121      * The database password
122      */

123     private String JavaDoc password = null;
124     
125     public String JavaDoc getPassword() {
126         return password;
127     }
128     
129     public void setPassword(String JavaDoc password) {
130         this.password = password;
131     }
132     
133     // ---------------------------------------------------------- Public methods
134

135     public boolean store(Object JavaDoc 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 JavaDoc 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     /** Read all objects stored the persistent media.
158      *
159      * @return an array containing the objects read. If no objects are found an
160      * empty array is returned. If the persistent store has not
161      * processed the quest, null is returned.
162      *
163      * @throws PersistentStoreException
164      *
165      */

166     public Object JavaDoc[] read(Class JavaDoc objClass) throws PersistentStoreException {
167         Object JavaDoc[] 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     /**
178      * This method performs the following configuration taks:
179      * <ul>
180      * <li>for each persistent store in <i>persistentStores</i></li>
181      * <ul>
182      * <li>configure the persistent store instance</li>
183      * </ul>
184      * </ul>
185      *
186      * NOTE: this method is different from the init method by design choice. It
187      * is more generic and allow to add additional properties that a particular
188      * datastore may require. For example a JDO-based persistent store may need
189      * more configuration properties than just the jndi name of the datasource.
190      *
191      * @throws ConfigPersistentStoreException if one of the persistent store
192      * instances could not be configured
193      *
194      */

195     public void configure(final Map JavaDoc 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     /**
207      * Initialization of the PersistentStoreManager.
208      * Th initialization is done as follows:
209      * <ul>
210      * <li>for each persistent store in <i>stores</i></li>
211      * <ul>
212      * <li>create the persistent store instance</li>
213      * </ul>
214      * <li>call <i>configure(...)</i></li>
215      * </ul>
216      *
217      * @throws BeanInitializationException in case of inittialization errors
218      */

219     public void init()
220     throws BeanInitializationException{
221         //
222
// Instantiates the persistent stores
223
//
224
if ((stores == null) || (stores.length == 0)) {
225             return;
226         }
227         
228         persistentStores = new PersistentStore[stores.length];
229         
230                 
231         //
232
// Creates the managed persistent stores
233
//
234
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 JavaDoc e) {
242             throw new BeanInitializationException(e.getMessage(), e.getCause());
243         }
244             
245         
246         try {
247             //
248
// Prepares the configuration map for the persistent stores
249
//
250
HashMap JavaDoc props = new HashMap JavaDoc(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 JavaDoc toString() {
263         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc 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 JavaDoc[] read(Object JavaDoc o, Clause clause) throws PersistentStoreException
290     {
291         Object JavaDoc[] 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 JavaDoc 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