KickJava   Java API By Example, From Geeks To Geeks.

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


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.CacheImpl;
10 import org.jboss.cache.Version;
11 import org.jboss.cache.factories.XmlConfigurationParser;
12 import org.jboss.cache.lock.IsolationLevel;
13 import org.w3c.dom.Element JavaDoc;
14
15 /**
16  * Encapsulates the configuration of a Cache.
17  *
18  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
19  */

20 public class Configuration
21         extends ConfigurationComponent
22         implements Cloneable JavaDoc
23 {
24    private static final long serialVersionUID = 5553791890144997466L;
25
26    /**
27     * Public enum that encapsulates cache mode
28     */

29    public enum CacheMode
30    {
31       LOCAL, REPL_SYNC, REPL_ASYNC, INVALIDATION_SYNC, INVALIDATION_ASYNC
32    }
33
34    public static CacheMode legacyModeToCacheMode(int legacyMode)
35    {
36       switch (legacyMode)
37       {
38          case 1:
39             return CacheMode.LOCAL;
40          case 2:
41             return CacheMode.REPL_ASYNC;
42          case 3:
43             return CacheMode.REPL_SYNC;
44          case 4:
45             return CacheMode.INVALIDATION_ASYNC;
46          case 5:
47             return CacheMode.INVALIDATION_SYNC;
48          default:
49             throw new IllegalArgumentException JavaDoc("Unknown legacy cache mode " +
50                     legacyMode);
51       }
52    }
53
54    /**
55     * Public enum that encapsulates node locking scheme
56     */

57    public enum NodeLockingScheme
58    {
59       PESSIMISTIC, OPTIMISTIC
60    }
61
62    /**
63     * Default replication version, from {@link Version#getVersionShort}.
64     */

65    public static final short DEFAULT_REPLICATION_VERSION = Version.getVersionShort();
66
67    // ------------------------------------------------------------------------------------------------------------
68
// CONFIGURATION OPTIONS
69
// ------------------------------------------------------------------------------------------------------------
70

71    private String JavaDoc clusterName = "JBossCache-Cluster";
72    private String JavaDoc clusterConfig = null;
73    private boolean useReplQueue = false;
74    @Dynamic
75    private int replQueueMaxElements = 1000;
76    @Dynamic
77    private long replQueueInterval = 5000;
78    private boolean exposeManagementStatistics = true;
79    @Dynamic
80    private boolean fetchInMemoryState = true;
81    private short replicationVersion = DEFAULT_REPLICATION_VERSION;
82    private String JavaDoc replVersionString = Version.getVersionString(DEFAULT_REPLICATION_VERSION);
83    @Dynamic
84    private long lockAcquisitionTimeout = 10000;
85    @Dynamic
86    private long syncReplTimeout = 15000;
87    private CacheMode cacheMode = CacheMode.LOCAL;
88    private boolean inactiveOnStartup = false;
89    private String JavaDoc serviceName;
90    private long initialStateRetrievalTimeout = 10000;
91    private IsolationLevel isolationLevel = IsolationLevel.REPEATABLE_READ;
92    @Dynamic
93    private EvictionConfig evictionConfig = null;
94    private boolean useRegionBasedMarshalling = false;
95    private String JavaDoc transactionManagerLookupClass = null;
96    private CacheLoaderConfig cacheLoaderConfig = null;
97    @Dynamic
98    private boolean syncCommitPhase = false;
99    @Dynamic
100    private boolean syncRollbackPhase = false;
101    private BuddyReplicationConfig buddyReplicationConfig;
102    private boolean nodeLockingOptimistic = false;
103    private NodeLockingScheme nodeLockingScheme = NodeLockingScheme.PESSIMISTIC;
104    private String JavaDoc muxServiceName = null;
105    private String JavaDoc muxStackName = null;
106    private boolean usingMultiplexer = false;
107    private transient RuntimeConfig runtimeConfig;
108
109    // ------------------------------------------------------------------------------------------------------------
110
// CONSTRUCTORS
111
// ------------------------------------------------------------------------------------------------------------
112

113    /**
114     * Sets a reference to an existing CacheImpl instance
115     *
116     * @param cache
117     */

118    public Configuration(CacheImpl cache)
119    {
120       setCacheImpl(cache);
121    }
122
123    /**
124     * Default empty constructor
125     */

126    public Configuration()
127    {
128    }
129
130    // ------------------------------------------------------------------------------------------------------------
131
// SETTERS - MAKE SURE ALL SETTERS PERFORM testImmutability()!!!
132
// ------------------------------------------------------------------------------------------------------------
133

134    /**
135     * Converts a list of elements to a Java Groups property string.
136     */

137    public void setClusterConfig(Element JavaDoc config)
138    {
139       setClusterConfig(XmlConfigurationParser.parseClusterConfigXml(config));
140    }
141
142    public void setClusterName(String JavaDoc clusterName)
143    {
144       testImmutability("clusterName");
145       this.clusterName = clusterName;
146    }
147
148    public void setClusterConfig(String JavaDoc clusterConfig)
149    {
150       testImmutability("clusterConfig");
151       this.clusterConfig = clusterConfig;
152    }
153
154    public void setReplQueueMaxElements(int replQueueMaxElements)
155    {
156       testImmutability("replQueueMaxElements");
157       this.replQueueMaxElements = replQueueMaxElements;
158    }
159
160    public void setReplQueueInterval(long replQueueInterval)
161    {
162       testImmutability("replQueueInterval");
163       this.replQueueInterval = replQueueInterval;
164    }
165
166    public void setExposeManagementStatistics(boolean useMbean)
167    {
168       testImmutability("exposeManagementStatistics");
169       this.exposeManagementStatistics = useMbean;
170    }
171
172    public void setFetchInMemoryState(boolean fetchInMemoryState)
173    {
174       testImmutability("fetchInMemoryState");
175       this.fetchInMemoryState = fetchInMemoryState;
176    }
177
178    public void setReplicationVersion(short replicationVersion)
179    {
180       testImmutability("replicationVersion");
181       this.replicationVersion = replicationVersion;
182    }
183
184    public void setReplVersionString(String JavaDoc replVersionString)
185    {
186       testImmutability("replVersionString");
187       this.replVersionString = replVersionString;
188    }
189
190    public void setLockAcquisitionTimeout(long lockAcquisitionTimeout)
191    {
192       testImmutability("lockAcquisitionTimeout");
193       this.lockAcquisitionTimeout = lockAcquisitionTimeout;
194    }
195
196    public void setSyncReplTimeout(long syncReplTimeout)
197    {
198       testImmutability("syncReplTimeout");
199       this.syncReplTimeout = syncReplTimeout;
200    }
201
202    public void setCacheMode(CacheMode cacheModeInt)
203    {
204       testImmutability("cacheMode");
205       this.cacheMode = cacheModeInt;
206    }
207
208    public void setCacheMode(String JavaDoc cacheMode)
209    {
210       testImmutability("cacheMode");
211       if (cacheMode == null) throw new ConfigurationException("Cache mode cannot be null", "CacheMode");
212       this.cacheMode = CacheMode.valueOf(cacheMode.toUpperCase());
213       if (this.cacheMode == null)
214       {
215          log.warn("Unknown cache mode '" + cacheMode + "', using defaults.");
216          this.cacheMode = CacheMode.LOCAL;
217       }
218    }
219
220    public String JavaDoc getCacheModeString()
221    {
222       return cacheMode == null ? null : cacheMode.toString();
223    }
224
225    public void setCacheModeString(String JavaDoc cacheMode)
226    {
227       setCacheMode(cacheMode);
228    }
229
230    public void setInactiveOnStartup(boolean inactiveOnStartup)
231    {
232       testImmutability("inactiveOnStartup");
233       this.inactiveOnStartup = inactiveOnStartup;
234    }
235
236    public EvictionConfig getEvictionConfig()
237    {
238       return evictionConfig;
239    }
240
241    public void setEvictionConfig(EvictionConfig config)
242    {
243       testImmutability("evictionConfig");
244       this.evictionConfig = config;
245    }
246
247    public void setUseRegionBasedMarshalling(boolean useRegionBasedMarshalling)
248    {
249       testImmutability("useRegionBasedMarshalling");
250       this.useRegionBasedMarshalling = useRegionBasedMarshalling;
251    }
252
253    public void setTransactionManagerLookupClass(String JavaDoc transactionManagerLookupClass)
254    {
255       testImmutability("transactionManagerLookupClass");
256       this.transactionManagerLookupClass = transactionManagerLookupClass;
257    }
258
259    public void setCacheLoaderConfig(CacheLoaderConfig config)
260    {
261       testImmutability("cacheLoaderConfig");
262       replaceChildConfig(this.cacheLoaderConfig, config);
263       this.cacheLoaderConfig = config;
264    }
265
266    public void setSyncCommitPhase(boolean syncCommitPhase)
267    {
268       testImmutability("syncCommitPhase");
269       this.syncCommitPhase = syncCommitPhase;
270    }
271
272    public void setSyncRollbackPhase(boolean syncRollbackPhase)
273    {
274       testImmutability("syncRollbackPhase");
275       this.syncRollbackPhase = syncRollbackPhase;
276    }
277
278    public void setBuddyReplicationConfig(BuddyReplicationConfig config)
279    {
280       testImmutability("buddyReplicationConfig");
281       replaceChildConfig(this.buddyReplicationConfig, config);
282       this.buddyReplicationConfig = config;
283    }
284
285    public void setNodeLockingScheme(NodeLockingScheme nodeLockingScheme)
286    {
287       testImmutability("nodeLockingScheme");
288       testImmutability("nodeLockingOptimistic");
289       this.nodeLockingScheme = nodeLockingScheme;
290       this.nodeLockingOptimistic = (nodeLockingScheme == NodeLockingScheme.OPTIMISTIC);
291    }
292
293    public void setUseReplQueue(boolean useReplQueue)
294    {
295       testImmutability("useReplQueue");
296       this.useReplQueue = useReplQueue;
297    }
298
299    public void setIsolationLevel(IsolationLevel isolationLevel)
300    {
301       testImmutability("isolationLevel");
302       this.isolationLevel = isolationLevel;
303    }
304
305    public void setNodeLockingOptimistic(boolean nodeLockingOptimistic)
306    {
307       testImmutability("nodeLockingOptimistic");
308       this.nodeLockingOptimistic = nodeLockingOptimistic;
309    }
310
311    public void setInitialStateRetrievalTimeout(long initialStateRetrievalTimeout)
312    {
313       testImmutability("initialStateRetrievalTimeout");
314       this.initialStateRetrievalTimeout = initialStateRetrievalTimeout;
315    }
316
317    public void setNodeLockingScheme(String JavaDoc nodeLockingScheme)
318    {
319       testImmutability("nodeLockingScheme");
320       testImmutability("nodeLockingOptimistic");
321       if (nodeLockingScheme == null)
322       {
323          throw new ConfigurationException("Node locking scheme cannot be null", "NodeLockingScheme");
324       }
325       this.nodeLockingScheme = NodeLockingScheme.valueOf(nodeLockingScheme.toUpperCase());
326       if (this.nodeLockingScheme == null)
327       {
328          log.warn("Unknown node locking scheme '" + nodeLockingScheme + "', using defaults.");
329          this.nodeLockingScheme = NodeLockingScheme.PESSIMISTIC;
330       }
331
332       this.nodeLockingOptimistic = (this.nodeLockingScheme == NodeLockingScheme.OPTIMISTIC);
333    }
334
335    public String JavaDoc getNodeLockingSchemeString()
336    {
337       return nodeLockingScheme == null ? null : nodeLockingScheme.toString();
338    }
339
340    public void setNodeLockingSchemeString(String JavaDoc nodeLockingScheme)
341    {
342       setNodeLockingScheme(nodeLockingScheme);
343    }
344
345    public void setIsolationLevel(String JavaDoc isolationLevel)
346    {
347       testImmutability("isolationLevel");
348       if (isolationLevel == null) throw new ConfigurationException("Isolation level cannot be null", "IsolationLevel");
349       this.isolationLevel = IsolationLevel.valueOf(isolationLevel.toUpperCase());
350       if (this.isolationLevel == null)
351       {
352          log.warn("Unknown isolation level '" + isolationLevel + "', using defaults.");
353          this.isolationLevel = IsolationLevel.REPEATABLE_READ;
354       }
355    }
356
357    public String JavaDoc getIsolationLevelString()
358    {
359       return isolationLevel == null ? null : isolationLevel.toString();
360    }
361
362    public void setIsolationLevelString(String JavaDoc isolationLevel)
363    {
364       setIsolationLevel(isolationLevel);
365    }
366
367    public void setMultiplexerStack(String JavaDoc stackName)
368    {
369       testImmutability("muxStackName");
370       this.muxStackName = stackName;
371    }
372
373    public void setMultiplexerService(String JavaDoc serviceName)
374    {
375       testImmutability("muxServiceName");
376       this.muxServiceName = serviceName;
377    }
378
379    public boolean isUsingMultiplexer()
380    {
381       return usingMultiplexer;
382    }
383
384    public void setUsingMultiplexer(boolean usingMultiplexer)
385    {
386       testImmutability("usingMultiplexer");
387       this.usingMultiplexer = usingMultiplexer;
388    }
389
390    /**
391     * Sets the JMX service name to bind the cache to.
392     *
393     * @param serviceName
394     */

395    public void setServiceName(String JavaDoc serviceName)
396    {
397       testImmutability("serviceName");
398       this.serviceName = serviceName;
399    }
400
401    // ------------------------------------------------------------------------------------------------------------
402
// GETTERS
403
// ------------------------------------------------------------------------------------------------------------
404

405
406    public boolean isNodeLockingOptimistic()
407    {
408       return nodeLockingOptimistic;
409    }
410
411    public boolean isUseReplQueue()
412    {
413       return useReplQueue;
414    }
415
416    public String JavaDoc getClusterName()
417    {
418       return clusterName;
419    }
420
421    public String JavaDoc getClusterConfig()
422    {
423       return clusterConfig;
424    }
425
426    public int getReplQueueMaxElements()
427    {
428       return replQueueMaxElements;
429    }
430
431    public long getReplQueueInterval()
432    {
433       return replQueueInterval;
434    }
435
436    public boolean getExposeManagementStatistics()
437    {
438       return exposeManagementStatistics;
439    }
440
441    public boolean isFetchInMemoryState()
442    {
443       return fetchInMemoryState;
444    }
445
446    public short getReplicationVersion()
447    {
448       return replicationVersion;
449    }
450
451    public String JavaDoc getReplVersionString()
452    {
453       return replVersionString;
454    }
455
456    public long getLockAcquisitionTimeout()
457    {
458       return lockAcquisitionTimeout;
459    }
460
461    public long getSyncReplTimeout()
462    {
463       return syncReplTimeout;
464    }
465
466    public CacheMode getCacheMode()
467    {
468       return cacheMode;
469    }
470
471    public boolean isInactiveOnStartup()
472    {
473       return inactiveOnStartup;
474    }
475
476    public IsolationLevel getIsolationLevel()
477    {
478       return isolationLevel;
479    }
480
481    public boolean isUseRegionBasedMarshalling()
482    {
483       return useRegionBasedMarshalling;
484    }
485
486    public String JavaDoc getTransactionManagerLookupClass()
487    {
488       return transactionManagerLookupClass;
489    }
490
491    public CacheLoaderConfig getCacheLoaderConfig()
492    {
493       return cacheLoaderConfig;
494    }
495
496    public boolean isSyncCommitPhase()
497    {
498       return syncCommitPhase;
499    }
500
501    public boolean isSyncRollbackPhase()
502    {
503       return syncRollbackPhase;
504    }
505
506    public BuddyReplicationConfig getBuddyReplicationConfig()
507    {
508       return buddyReplicationConfig;
509    }
510
511    public NodeLockingScheme getNodeLockingScheme()
512    {
513       return nodeLockingScheme;
514    }
515
516    public long getInitialStateRetrievalTimeout()
517    {
518       return initialStateRetrievalTimeout;
519    }
520
521    public String JavaDoc getMultiplexerStack()
522    {
523       return muxStackName;
524    }
525
526    public String JavaDoc getMultiplexerService()
527    {
528       return muxServiceName;
529    }
530
531    /**
532     * Retrieves the JMX service name to bind the cache to, if any.
533     */

534    public String JavaDoc getServiceName()
535    {
536       return serviceName;
537    }
538
539    public synchronized RuntimeConfig getRuntimeConfig()
540    {
541       if (runtimeConfig == null)
542       {
543          setRuntimeConfig(new RuntimeConfig(), false);
544       }
545       return runtimeConfig;
546    }
547
548    public void setRuntimeConfig(RuntimeConfig runtimeConfig)
549    {
550       setRuntimeConfig(runtimeConfig, true);
551    }
552
553    private void setRuntimeConfig(RuntimeConfig runtimeConfig, boolean testImmutability)
554    {
555       if (testImmutability)
556          testImmutability("runtimeConfig");
557       this.runtimeConfig = runtimeConfig;
558    }
559
560    // ------------------------------------------------------------------------------------------------------------
561
// HELPERS
562
// ------------------------------------------------------------------------------------------------------------
563

564    // ------------------------------------------------------------------------------------------------------------
565
// OVERRIDDEN METHODS
566
// ------------------------------------------------------------------------------------------------------------
567

568
569    public boolean equals(Object JavaDoc o)
570    {
571       if (this == o) return true;
572       if (o == null || getClass() != o.getClass()) return false;
573
574       final Configuration that = (Configuration) o;
575
576       if (fetchInMemoryState != that.fetchInMemoryState) return false;
577       if (inactiveOnStartup != that.inactiveOnStartup) return false;
578       if (initialStateRetrievalTimeout != that.initialStateRetrievalTimeout) return false;
579       if (lockAcquisitionTimeout != that.lockAcquisitionTimeout) return false;
580       if (nodeLockingOptimistic != that.nodeLockingOptimistic) return false;
581       if (replQueueInterval != that.replQueueInterval) return false;
582       if (replQueueMaxElements != that.replQueueMaxElements) return false;
583       if (replicationVersion != that.replicationVersion) return false;
584       if (syncCommitPhase != that.syncCommitPhase) return false;
585       if (syncReplTimeout != that.syncReplTimeout) return false;
586       if (syncRollbackPhase != that.syncRollbackPhase) return false;
587       if (exposeManagementStatistics != that.exposeManagementStatistics) return false;
588       if (useRegionBasedMarshalling != that.useRegionBasedMarshalling) return false;
589       if (useReplQueue != that.useReplQueue) return false;
590       if (buddyReplicationConfig != null ? !buddyReplicationConfig.equals(that.buddyReplicationConfig) : that.buddyReplicationConfig != null)
591       {
592          return false;
593       }
594       if (cacheLoaderConfig != null ? !cacheLoaderConfig.equals(that.cacheLoaderConfig) : that.cacheLoaderConfig != null)
595       {
596          return false;
597       }
598       if (cacheMode != that.cacheMode) return false;
599       if (clusterConfig != null ? !clusterConfig.equals(that.clusterConfig) : that.clusterConfig != null) return false;
600       if (clusterName != null ? !clusterName.equals(that.clusterName) : that.clusterName != null) return false;
601       if (evictionConfig != null ? !evictionConfig.equals(that.evictionConfig) : that.evictionConfig != null)
602       {
603          return false;
604       }
605       if (isolationLevel != that.isolationLevel) return false;
606       if (muxServiceName != null ? !muxServiceName.equals(that.muxServiceName) : that.muxServiceName != null)
607       {
608          return false;
609       }
610       if (muxStackName != null ? !muxStackName.equals(that.muxStackName) : that.muxStackName != null) return false;
611       if (nodeLockingScheme != that.nodeLockingScheme) return false;
612       if (transactionManagerLookupClass != null ? !transactionManagerLookupClass.equals(that.transactionManagerLookupClass) : that.transactionManagerLookupClass != null)
613       {
614          return false;
615       }
616       if (!safeEquals(runtimeConfig, that.runtimeConfig))
617          return false;
618
619       return true;
620    }
621
622    public int hashCode()
623    {
624       int result = 17;
625       result = 29 * result + (clusterName != null ? clusterName.hashCode() : 0);
626       result = 29 * result + (clusterConfig != null ? clusterConfig.hashCode() : 0);
627       result = 29 * result + (useReplQueue ? 1 : 0);
628       result = 29 * result + replQueueMaxElements;
629       result = 29 * result + (int) (replQueueInterval ^ (replQueueInterval >>> 32));
630       result = 29 * result + (exposeManagementStatistics ? 1 : 0);
631       result = 29 * result + (fetchInMemoryState ? 1 : 0);
632       result = 29 * result + (int) replicationVersion;
633       result = 29 * result + (int) (lockAcquisitionTimeout ^ (lockAcquisitionTimeout >>> 32));
634       result = 29 * result + (int) (syncReplTimeout ^ (syncReplTimeout >>> 32));
635       result = 29 * result + (cacheMode != null ? cacheMode.hashCode() : 0);
636       result = 29 * result + (inactiveOnStartup ? 1 : 0);
637       result = 29 * result + (int) (initialStateRetrievalTimeout ^ (initialStateRetrievalTimeout >>> 32));
638       result = 29 * result + (isolationLevel != null ? isolationLevel.hashCode() : 0);
639       result = 29 * result + (evictionConfig != null ? evictionConfig.hashCode() : 0);
640       result = 29 * result + (useRegionBasedMarshalling ? 1 : 0);
641       result = 29 * result + (transactionManagerLookupClass != null ? transactionManagerLookupClass.hashCode() : 0);
642       result = 29 * result + (cacheLoaderConfig != null ? cacheLoaderConfig.hashCode() : 0);
643       result = 29 * result + (syncCommitPhase ? 1 : 0);
644       result = 29 * result + (syncRollbackPhase ? 1 : 0);
645       result = 29 * result + (buddyReplicationConfig != null ? buddyReplicationConfig.hashCode() : 0);
646       result = 29 * result + (nodeLockingOptimistic ? 1 : 0);
647       result = 29 * result + (nodeLockingScheme != null ? nodeLockingScheme.hashCode() : 0);
648       result = 29 * result + (muxServiceName != null ? muxServiceName.hashCode() : 0);
649       result = 29 * result + (muxStackName != null ? muxStackName.hashCode() : 0);
650       result = 29 * result + (runtimeConfig != null ? runtimeConfig.hashCode() : 0);
651       return result;
652    }
653
654    public Configuration clone() throws CloneNotSupportedException JavaDoc
655    {
656       Configuration c = (Configuration) super.clone();
657       if (buddyReplicationConfig != null)
658       {
659          c.setBuddyReplicationConfig((BuddyReplicationConfig) buddyReplicationConfig.clone());
660       }
661       if (evictionConfig != null)
662       {
663          c.setEvictionConfig((EvictionConfig) evictionConfig.clone());
664       }
665       if (cacheLoaderConfig != null)
666       {
667          c.setCacheLoaderConfig((CacheLoaderConfig) cacheLoaderConfig.clone());
668       }
669       if (runtimeConfig != null)
670       {
671          c.setRuntimeConfig((RuntimeConfig) runtimeConfig.clone());
672          // always make sure we reset the runtime when cloning.
673
// c.getRuntimeConfig().reset();
674
// don't reset the runtime for now, just un-set the NodeFactory.
675
c.getRuntimeConfig().setNodeFactory(null);
676       }
677       return c;
678    }
679
680 }
681
Popular Tags