001: package com.completex.objective.components.ocache;
002:
003: import com.completex.objective.util.PropertyMap;
004:
005: import java.util.Map;
006:
007: /**
008: * @author Gennady Krizhevsky
009: */
010: public class OdalCacheConfig {
011:
012: public static final String PROP_TRANSACTION_SENSITIVE = "transactionSensitive";
013: public static final String PROP_FLATTEN = "flatten";
014: public static final String PROP_CLONE = "clone";
015: public static final String PROP_MARK_CACHE_COLLECTION_ELEMENTS = "markCacheCollectionElements";
016: public static final String PROP_MAX_SIZE = "maxSize";
017: public static final String PROP_CACHE_FLUSH_INTERVAL = "cacheFlushInterval";
018: public static final String PROP_ENTRY_FLUSH_INTERVAL = "entryFlushInterval";
019: public static final String PROP_DISABLED = "disabled";
020:
021: private Boolean transactionSensitive;
022: private Boolean flatten;
023: private Boolean clone;
024: private Boolean markCacheCollectionElements;
025: private Integer maxSize;
026: private String cacheFlushInterval;
027: private String entryFlushInterval;
028: private Boolean disabled;
029:
030: public OdalCacheConfig() {
031: }
032:
033: public OdalCacheConfig(OdalCacheConfig currentConfig) {
034: if (currentConfig == null) {
035: return;
036: }
037:
038: this .transactionSensitive = currentConfig.transactionSensitive;
039: this .flatten = currentConfig.flatten;
040: this .clone = currentConfig.clone;
041: this .markCacheCollectionElements = currentConfig.markCacheCollectionElements;
042: this .maxSize = currentConfig.maxSize;
043: this .cacheFlushInterval = currentConfig.cacheFlushInterval;
044: this .entryFlushInterval = currentConfig.entryFlushInterval;
045: this .disabled = currentConfig.disabled;
046: }
047:
048: public OdalCacheConfig(Map currentConfig) {
049: if (currentConfig == null) {
050: return;
051: }
052: PropertyMap propertyMap = new PropertyMap(currentConfig);
053: this .transactionSensitive = propertyMap
054: .getBooleanObj(PROP_TRANSACTION_SENSITIVE);
055: this .flatten = propertyMap.getBooleanObj(PROP_FLATTEN);
056: this .clone = propertyMap.getBooleanObj(PROP_CLONE);
057: this .markCacheCollectionElements = propertyMap
058: .getBooleanObj(PROP_MARK_CACHE_COLLECTION_ELEMENTS);
059: this .maxSize = propertyMap.getIntObj(PROP_MAX_SIZE);
060: this .cacheFlushInterval = propertyMap
061: .getProperty(PROP_CACHE_FLUSH_INTERVAL);
062: this .entryFlushInterval = propertyMap
063: .getProperty(PROP_ENTRY_FLUSH_INTERVAL);
064: this .disabled = propertyMap.getBooleanObj(PROP_DISABLED);
065: }
066:
067: /**
068: * We assume that merging is done into copy of global config which is always set to not null value.
069: * It contains the default values.
070: *
071: * @param currentConfig
072: */
073: public void merge(OdalCacheConfig currentConfig) {
074: if (currentConfig == null) {
075: return;
076: }
077:
078: transactionSensitive = merge(transactionSensitive,
079: currentConfig.transactionSensitive);
080: flatten = merge(flatten, currentConfig.flatten);
081: clone = merge(clone, currentConfig.clone);
082: markCacheCollectionElements = merge(
083: markCacheCollectionElements,
084: currentConfig.markCacheCollectionElements);
085: maxSize = merge(maxSize, currentConfig.maxSize);
086: cacheFlushInterval = merge(cacheFlushInterval,
087: currentConfig.cacheFlushInterval);
088: entryFlushInterval = merge(entryFlushInterval,
089: currentConfig.entryFlushInterval);
090: //
091: // Disabled merge does not follow the comon rule:
092: // disabled is true if any of the disabled flags is true:
093: //
094: disabled = Boolean.TRUE.equals(disabled)
095: || Boolean.TRUE.equals(currentConfig.disabled) ? Boolean.TRUE
096: : Boolean.FALSE;
097: }
098:
099: protected String merge(String this Value, String thatValue) {
100: return (String) merge0(this Value, thatValue);
101: }
102:
103: protected Long merge(Long this Value, Long thatValue) {
104: return (Long) merge0(this Value, thatValue);
105: }
106:
107: protected Integer merge(Integer this Value, Integer thatValue) {
108: return (Integer) merge0(this Value, thatValue);
109: }
110:
111: protected Boolean merge(Boolean this Value, Boolean thatValue) {
112: return (Boolean) merge0(this Value, thatValue);
113: }
114:
115: protected Object merge0(Object this Value, Object thatValue) {
116: if (thatValue != null) {
117: return thatValue;
118: } else {
119: return this Value;
120: }
121: }
122:
123: public boolean isTransactionSensitive() {
124: return isTransactionSensitiveSet()
125: && transactionSensitive.booleanValue();
126: }
127:
128: public boolean isTransactionSensitiveSet() {
129: return transactionSensitive != null;
130: }
131:
132: public void setTransactionSensitive(Boolean transactionSensitive) {
133: this .transactionSensitive = transactionSensitive;
134: }
135:
136: public boolean isFlatten() {
137: return isFlattenSet() && flatten.booleanValue();
138: }
139:
140: public boolean isFlattenSet() {
141: return flatten != null;
142: }
143:
144: public void setFlatten(Boolean flatten) {
145: this .flatten = flatten;
146: }
147:
148: public boolean isClone() {
149: return isCloneSet() && clone.booleanValue();
150: }
151:
152: public boolean isCloneSet() {
153: return clone != null;
154: }
155:
156: public void setClone(Boolean clone) {
157: this .clone = clone;
158: }
159:
160: public boolean isMarkCacheCollectionElements() {
161: return isMarkCacheCollectionElementsSet()
162: && markCacheCollectionElements.booleanValue();
163: }
164:
165: public boolean isMarkCacheCollectionElementsSet() {
166: return markCacheCollectionElements != null;
167: }
168:
169: public void setMarkCacheCollectionElements(
170: Boolean markCacheCollectionElements) {
171: this .markCacheCollectionElements = markCacheCollectionElements;
172: }
173:
174: public int getMaxSize() {
175: return isMaxCacheSizeSet() ? maxSize.intValue() : 0;
176: }
177:
178: public boolean isMaxCacheSizeSet() {
179: return maxSize != null;
180: }
181:
182: public void setMaxSize(Integer maxSize) {
183: this .maxSize = maxSize;
184: }
185:
186: public String getCacheFlushInterval() {
187: return cacheFlushInterval;
188: }
189:
190: public boolean isCacheLifeSpanSet() {
191: return cacheFlushInterval != null;
192: }
193:
194: public void setCacheFlushInterval(String cacheFlushInterval) {
195: this .cacheFlushInterval = cacheFlushInterval;
196: }
197:
198: public String getEntryFlushInterval() {
199: return entryFlushInterval;
200: }
201:
202: public boolean isEntryLifeSpanSet() {
203: return entryFlushInterval != null;
204: }
205:
206: public void setEntryFlushInterval(String entryFlushInterval) {
207: this .entryFlushInterval = entryFlushInterval;
208: }
209:
210: public boolean isDisabled() {
211: return isDisabledSet() && disabled.booleanValue();
212: }
213:
214: public boolean isDisabledSet() {
215: return disabled != null;
216: }
217:
218: public void setDisabled(Boolean disabled) {
219: this .disabled = disabled;
220: }
221:
222: public OdalCacheConfig cloneOdalCacheConfig() {
223: return new OdalCacheConfig(this );
224: }
225:
226: /**
227: * Merges global settings with local ones.
228: * General rule is: local settings override the global ones.
229: * The only exception is 'disabled' flag: 'disabled' is true if any of the disabled flags
230: * (global or local) is true.
231: *
232: * @param globalConfig global config is normally set on the cache factory
233: * @param localConfig is supplied when registering cache in cache factory
234: * @return merged OdalCacheConfig
235: */
236: public static OdalCacheConfig merge(OdalCacheConfig globalConfig,
237: OdalCacheConfig localConfig) {
238: if (globalConfig == null) {
239: throw new OdalRuntimeCacheException("globalConfig == null");
240: }
241: globalConfig.validateAllSet();
242: OdalCacheConfig newGlobalConfig = globalConfig
243: .cloneOdalCacheConfig();
244: newGlobalConfig.merge(localConfig);
245: return newGlobalConfig;
246: }
247:
248: public void validateAllSet() {
249: DescValue[] values = {
250: new DescValue(PROP_TRANSACTION_SENSITIVE,
251: transactionSensitive),
252: new DescValue(PROP_FLATTEN, flatten),
253: new DescValue(PROP_CLONE, clone),
254: new DescValue(PROP_MARK_CACHE_COLLECTION_ELEMENTS,
255: markCacheCollectionElements),
256: new DescValue(PROP_MAX_SIZE, maxSize),
257: new DescValue(PROP_CACHE_FLUSH_INTERVAL,
258: cacheFlushInterval),
259: new DescValue(PROP_ENTRY_FLUSH_INTERVAL,
260: entryFlushInterval),
261: new DescValue(PROP_DISABLED, disabled), };
262:
263: for (int i = 0; i < values.length; i++) {
264: DescValue value = values[i];
265: if (value.value == null) {
266: throw new OdalRuntimeCacheException("Property "
267: + value.description + " not set");
268: }
269: }
270: }
271:
272: class DescValue {
273: public DescValue(String description, Object value) {
274: this .description = description;
275: this .value = value;
276: }
277:
278: private String description;
279: private Object value;
280: }
281:
282: }
|