KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > config > CacheLoaderConfig


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.config;
8
9 import org.jboss.cache.xml.XmlHelper;
10
11 import java.io.ByteArrayInputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 /**
18  * Holds the configuration of the cache loader chain. ALL cache loaders should be defined using this class, adding
19  * individual cache loaders to the chain by calling {@see CacheLoaderConfig#addIndividualCacheLoaderConfig}
20  *
21  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
22  * @author Brian Stansberry
23  */

24 public class CacheLoaderConfig extends ConfigurationComponent
25 {
26    private static final long serialVersionUID = 2210349340378984424L;
27
28    private boolean passivation;
29    private String JavaDoc preload;
30    private List JavaDoc<IndividualCacheLoaderConfig> cacheLoaderConfigs = new ArrayList JavaDoc<IndividualCacheLoaderConfig>();
31
32    private boolean shared;
33
34    public CacheLoaderConfig()
35    {
36    }
37
38    public String JavaDoc getPreload()
39    {
40       return preload;
41    }
42
43    public void setPreload(String JavaDoc preload)
44    {
45       testImmutability("preload");
46       this.preload = preload;
47    }
48
49    public void setPassivation(boolean passivation)
50    {
51       testImmutability("passivation");
52       this.passivation = passivation;
53    }
54
55    public boolean isPassivation()
56    {
57       return passivation;
58    }
59
60    public void addIndividualCacheLoaderConfig(IndividualCacheLoaderConfig clc)
61    {
62       testImmutability("cacheLoaderConfigs");
63       cacheLoaderConfigs.add(clc);
64       // Ensure this config gets our back ref to the cache
65
addChildConfig(clc);
66    }
67
68    public List JavaDoc<IndividualCacheLoaderConfig> getIndividualCacheLoaderConfigs()
69    {
70       return cacheLoaderConfigs;
71    }
72
73    public void setIndividualCacheLoaderConfigs(List JavaDoc<IndividualCacheLoaderConfig> configs)
74    {
75       testImmutability("cacheLoaderConfigs");
76       // Ensure these configs get our back ref to the cache
77
replaceChildConfigs(this.cacheLoaderConfigs, configs);
78       this.cacheLoaderConfigs = configs == null ? new ArrayList JavaDoc<IndividualCacheLoaderConfig>() : configs;
79    }
80
81    public IndividualCacheLoaderConfig getFirstCacheLoaderConfig()
82    {
83       if (cacheLoaderConfigs.size() == 0) return null;
84       return cacheLoaderConfigs.get(0);
85    }
86
87    public boolean useChainingCacheLoader()
88    {
89       return !isPassivation() && cacheLoaderConfigs.size() > 1;
90    }
91
92    public String JavaDoc toString()
93    {
94       return new StringBuffer JavaDoc().append("CacheLoaderConfig{").append("shared=").append(shared).append(", passivation=").append(passivation).append(", preload='").append(preload).append('\'').append(", cacheLoaderConfigs.size()=").append(cacheLoaderConfigs.size()).append('}').toString();
95    }
96
97    public void setShared(boolean shared)
98    {
99       testImmutability("shared");
100       this.shared = shared;
101    }
102
103    public boolean isShared()
104    {
105       return shared;
106    }
107
108    public boolean equals(Object JavaDoc obj)
109    {
110       if (this == obj)
111          return true;
112
113       if (obj instanceof CacheLoaderConfig)
114       {
115          CacheLoaderConfig other = (CacheLoaderConfig) obj;
116          return (this.passivation == other.passivation)
117                  && (this.shared == other.shared)
118                  && safeEquals(this.preload, other.preload)
119                  && safeEquals(this.cacheLoaderConfigs, other.cacheLoaderConfigs);
120       }
121       return false;
122    }
123
124    public int hashCode()
125    {
126       int result = 19;
127       result = 51 * result + (passivation ? 0 : 1);
128       result = 51 * result + (shared ? 0 : 1);
129       result = 51 * result + (preload == null ? 0 : preload.hashCode());
130       result = 51 * result + (cacheLoaderConfigs == null ? 0 : cacheLoaderConfigs.hashCode());
131       return result;
132    }
133
134
135    /**
136     * Configuration object that holds the confguration of an individual cache loader.
137     *
138     * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
139     */

140    public static class IndividualCacheLoaderConfig extends ConfigurationComponent
141    {
142       private static final long serialVersionUID = -2282396799100828593L;
143
144       private String JavaDoc className;
145       private boolean async;
146       private boolean ignoreModifications;
147       private boolean fetchPersistentState;
148       private boolean singletonStore;
149       private boolean pushStateWhenCoordinator;
150
151       private boolean purgeOnStartup;
152
153       private Properties JavaDoc properties;
154
155       public IndividualCacheLoaderConfig()
156       {
157       }
158
159       protected void populateFromBaseConfig(IndividualCacheLoaderConfig base)
160       {
161          if (base != null)
162          {
163             setAsync(base.isAsync());
164             setIgnoreModifications(base.isIgnoreModifications());
165             setFetchPersistentState(base.isFetchPersistentState());
166             setSingletonStore(base.isSingletonStore());
167             setPushStateWhenCoordinator(base.isPushStateWhenCoordinator());
168             setPurgeOnStartup(base.isPurgeOnStartup());
169             Properties JavaDoc props = base.getProperties();
170             if (props != null)
171                setProperties(props);
172          }
173       }
174
175       public boolean isPurgeOnStartup()
176       {
177          return purgeOnStartup;
178       }
179
180       public boolean isFetchPersistentState()
181       {
182          return fetchPersistentState;
183       }
184
185       public void setFetchPersistentState(boolean fetchPersistentState)
186       {
187          testImmutability("fetchPersistentState");
188          this.fetchPersistentState = fetchPersistentState;
189       }
190
191       public void setClassName(String JavaDoc className)
192       {
193          testImmutability("className");
194          this.className = className;
195       }
196
197       public String JavaDoc getClassName()
198       {
199          return className;
200       }
201
202       public void setAsync(boolean async)
203       {
204          testImmutability("async");
205          this.async = async;
206       }
207
208       public boolean isAsync()
209       {
210          return async;
211       }
212
213       public void setIgnoreModifications(boolean ignoreModifications)
214       {
215          testImmutability("ignoreModifications");
216          this.ignoreModifications = ignoreModifications;
217       }
218
219       public boolean isIgnoreModifications()
220       {
221          return ignoreModifications;
222       }
223
224       public void setProperties(String JavaDoc properties) throws IOException JavaDoc
225       {
226          if (properties == null) return;
227
228          testImmutability("properties");
229          // JBCACHE-531: escape all backslash characters
230
// replace any "\" that is not preceded by a backslash with "\\"
231
properties = XmlHelper.escapeBackslashes(properties);
232          ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(properties.trim().getBytes("ISO8859_1"));
233          this.properties = new Properties JavaDoc();
234          this.properties.load(is);
235          is.close();
236       }
237
238       public void setProperties(Properties JavaDoc properties)
239       {
240          testImmutability("properties");
241          this.properties = properties;
242       }
243
244       public Properties JavaDoc getProperties()
245       {
246          return properties;
247       }
248
249       public void setPurgeOnStartup(boolean purgeOnStartup)
250       {
251          testImmutability("purgeOnStartup");
252          this.purgeOnStartup = purgeOnStartup;
253       }
254
255       public boolean isSingletonStore()
256       {
257          return singletonStore;
258       }
259
260       public void setSingletonStore(boolean singletonStore)
261       {
262          testImmutability("singletonStore");
263          this.singletonStore = singletonStore;
264       }
265
266       public boolean isPushStateWhenCoordinator()
267       {
268          return pushStateWhenCoordinator;
269       }
270
271       public void setPushStateWhenCoordinator(boolean pushStateWhenCoordinator)
272       {
273          testImmutability("pushStateWhenCoordinator");
274          if (singletonStore)
275          {
276             /* pushStateWhenCoordinator only makes sense if the cache loader
277      has been configured as a singleton store */

278             this.pushStateWhenCoordinator = pushStateWhenCoordinator;
279          }
280       }
281
282       public boolean equals(Object JavaDoc obj)
283       {
284          return equalsExcludingProperties(obj)
285                  && safeEquals(this.properties, ((IndividualCacheLoaderConfig) obj).properties);
286       }
287
288       protected boolean equalsExcludingProperties(Object JavaDoc obj)
289       {
290          if (this == obj)
291             return true;
292
293          if (obj instanceof IndividualCacheLoaderConfig)
294          {
295             IndividualCacheLoaderConfig other = (IndividualCacheLoaderConfig) obj;
296             return safeEquals(this.className, other.className)
297                     && (this.async == other.async)
298                     && (this.ignoreModifications == other.ignoreModifications)
299                     && (this.fetchPersistentState == other.fetchPersistentState)
300                     && (this.singletonStore == other.singletonStore)
301                     && (this.pushStateWhenCoordinator == other.pushStateWhenCoordinator);
302          }
303          return false;
304
305       }
306
307       public int hashCode()
308       {
309          return 31 * hashCodeExcludingProperties() + (properties == null ? 0 : properties.hashCode());
310       }
311
312       protected int hashCodeExcludingProperties()
313       {
314          int result = 17;
315          result = 31 * result + (className == null ? 0 : className.hashCode());
316          result = 31 * result + (async ? 0 : 1);
317          result = 31 * result + (ignoreModifications ? 0 : 1);
318          result = 31 * result + (fetchPersistentState ? 0 : 1);
319          result = 31 * result + (singletonStore ? 0 : 1);
320          result = 31 * result + (pushStateWhenCoordinator ? 0 : 1);
321          result = 31 * result + (purgeOnStartup ? 0 : 1);
322          return result;
323       }
324
325       public String JavaDoc toString()
326       {
327          return new StringBuffer JavaDoc().append("IndividualCacheLoaderConfig{").append("className='").append(className).append('\'')
328                  .append(", async=").append(async)
329                  .append(", ignoreModifications=").append(ignoreModifications)
330                  .append(", fetchPersistentState=").append(fetchPersistentState)
331                  .append(", properties=").append(properties).append('}')
332                  .append(", purgeOnStartup=").append(purgeOnStartup)
333                  .append(", singletonStore=").append(singletonStore)
334                  .append(", singletonStore.pushStateWhenCoordinator=").append(pushStateWhenCoordinator)
335                  .toString();
336       }
337    }
338 }
339
Popular Tags