001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.persistency.ocache.adapter;
010:
011: import com.completex.objective.components.persistency.ocache.impl.PersistencyCacheFactoryImpl;
012: import com.completex.objective.components.sdl.reader.SdlReader;
013: import com.completex.objective.components.sdl.reader.impl.SdlReaderImpl;
014: import com.completex.objective.components.ocache.OdalCacheConfig;
015: import com.completex.objective.components.ocache.ConfigurableCache;
016: import com.completex.objective.components.ocache.OdalRuntimeCacheException;
017: import com.completex.objective.components.ocache.OdalCache;
018: import com.completex.objective.util.PropertyMap;
019:
020: import java.util.Map;
021: import java.util.Iterator;
022: import java.io.FileReader;
023: import java.io.IOException;
024:
025: /**
026: * @author Gennady Krizhevsky
027: */
028: public class PersistencyCacheFactoryAdapter extends
029: PersistencyCacheFactoryImpl {
030:
031: private SdlReader sdlReader = new SdlReaderImpl();
032:
033: public static final String PROP_GLOBAL = "global";
034: public static final String PROP_CACHES = "caches";
035: public static final String PROP_CLASS = "class";
036:
037: public PersistencyCacheFactoryAdapter() {
038: }
039:
040: /**
041: * Map config.
042: * Sample:
043: * <p/>
044: * <PRE>
045: * // {
046: * // global = {
047: * // transactionSensitive = TRUE
048: * // flatten = TRUE
049: * // clone = TRUE
050: * // markCacheCollectionElements = TRUE
051: * // maxSize = 100
052: * // cacheFlushInterval = "24:00:00.000"
053: * // entryFlushInterval = "24:00:00.000"
054: * // disabled = FALSE
055: * // }
056: * //
057: * // caches = {
058: * // nameOne = {
059: * // class = com.completex.objective.components.ocache.impl.ExpiringLruCache
060: * // transactionSensitive = TRUE
061: * // flatten = TRUE
062: * // clone = TRUE
063: * // markCacheCollectionElements = TRUE
064: * // maxSize = 100
065: * // cacheFlushInterval = "24:00:00.000"
066: * // entryFlushInterval = "24:00:00.000"
067: * // disabled = FALSE
068: * // }
069: * // }
070: * // }
071: * </PRE>
072: *
073: * @param config
074: */
075: public PersistencyCacheFactoryAdapter(Map config) {
076: config(config);
077: }
078:
079: /**
080: * @see #PersistencyCacheFactoryAdapter(Map config)
081: * @param configFilePath
082: * @throws IOException
083: */
084: public PersistencyCacheFactoryAdapter(String configFilePath)
085: throws IOException {
086: Map configMap = (Map) sdlReader.read(new FileReader(
087: configFilePath));
088: config(configMap);
089: }
090:
091: /**
092: * Allows to decouple instatiation and configuration.
093: *
094: * @param config
095: */
096: public void config(Map config) {
097: PropertyMap propertyMap = PropertyMap.toPropertyMap(config);
098:
099: PropertyMap globalConfig = propertyMap
100: .getPropertyMap(PROP_GLOBAL);
101: setGlobalConfig(new OdalCacheConfig(globalConfig));
102:
103: PropertyMap cacheConfigs = propertyMap
104: .getPropertyMap(PROP_CACHES);
105: if (cacheConfigs != null) {
106: for (Iterator it = cacheConfigs.keySet().iterator(); it
107: .hasNext();) {
108: String cacheName = (String) it.next();
109: PropertyMap cacheConfig = cacheConfigs.getPropertyMap(
110: cacheName, true);
111: String className = cacheConfig.getProperty(PROP_CLASS);
112:
113: ConfigurableCache configurableCache = instantiateConfigurableCache(className);
114: configurableCache.configure(cacheConfig);
115: if (!(configurableCache instanceof OdalCache)) {
116: throw new OdalRuntimeCacheException(
117: "Cannot register cache "
118: + configurableCache
119: + " since it is not instance of OdalCache");
120: }
121:
122: configurableCache.setName(cacheName);
123: OdalCache odalCache = ((OdalCache) configurableCache);
124: registerCache(odalCache);
125: }
126: }
127: }
128:
129: protected ConfigurableCache instantiateConfigurableCache(
130: String className) throws OdalRuntimeCacheException {
131: ConfigurableCache configurableCache = null;
132: if (className != null) {
133: // Then it is expected to be instance of ConfigurableCache and to have a no args constructor
134: try {
135: configurableCache = (ConfigurableCache) Class.forName(
136: className).newInstance();
137: } catch (Exception e) {
138: throw new OdalRuntimeCacheException(
139: "Cannot instantiate "
140: + className
141: + ": it is expected to be instance"
142: + " of ConfigurableCache and to have a no args constructor. Please verify.");
143: }
144: }
145: return configurableCache;
146: }
147:
148: }
|