001: package org.shiftone.cache.config;
002:
003: import org.shiftone.cache.CacheFactory;
004: import org.shiftone.cache.ConfigurationException;
005: import org.shiftone.cache.policy.zero.ZeroCacheFactory;
006: import org.shiftone.cache.util.Log;
007:
008: import java.util.*;
009:
010: /**
011: * @version $Revision: 1.9 $
012: * @author <a href="mailto:jeff@shiftone.org">Jeff Drost</a>
013: */
014: public class ConfigurationInternals {
015:
016: private static final Log LOG = new Log(ConfigurationInternals.class);
017: private Map factories = new Hashtable();
018: Node rootNode;
019:
020: public ConfigurationInternals(Properties properties)
021: throws ConfigurationException {
022:
023: PropertiesTree tree = new PropertiesTree(properties);
024:
025: rootNode = tree.getRoot();
026:
027: Node factoryNode = rootNode.getRequiredNode("factory");
028: Node titleNode = rootNode.getRequiredNode("title");
029: Node versionNode = rootNode.getRequiredNode("version");
030: List factoryList = new ArrayList(factoryNode.getChildren());
031:
032: LOG.info(titleNode.getValue() + " v" + versionNode.getValue());
033: rootNode.print();
034:
035: // pass one (create stage)
036: for (int i = 0; i < factoryList.size(); i++) {
037: addFactory((Node) factoryList.get(i));
038: }
039:
040: // pass two - set property stage
041: for (int i = 0; i < factoryList.size(); i++) {
042: setFactoryProperties((Node) factoryList.get(i));
043: }
044: }
045:
046: public String getConfiguredCacheProperty(String property,
047: String cacheName) throws ConfigurationException {
048:
049: String[] cacheNameTokens = PropertiesTree.tokenize(cacheName);
050: Node node = rootNode.getRequiredNode("cache").getRequiredNode(
051: property);
052: String value = node.getValue();
053:
054: for (int i = 0; i < cacheNameTokens.length; i++) {
055: LOG.debug(i + " => " + cacheNameTokens[i]);
056:
057: if (node.hasNode(cacheNameTokens[i])) {
058: node = node.getNode(cacheNameTokens[i]);
059:
060: if (node.getValue() != null) {
061: value = node.getValue();
062: }
063: } else {
064: break;
065: }
066: }
067:
068: return value;
069: }
070:
071: private void addFactory(Node factoryNode)
072: throws ConfigurationException {
073:
074: String name = factoryNode.getKey();
075: String factoryClassName = factoryNode.getValue();
076: Class factoryClass;
077: CacheFactory cacheFactory;
078:
079: try {
080: factoryClass = Class.forName(factoryClassName);
081: cacheFactory = (CacheFactory) factoryClass.newInstance();
082:
083: factories.put(name, cacheFactory);
084: } catch (Exception e) {
085: throw new ConfigurationException(
086: "unable to create factory : " + name + "="
087: + factoryClassName, e);
088: }
089: }
090:
091: private void setFactoryProperties(Node factoryNode)
092: throws ConfigurationException {
093:
094: String name = factoryNode.getKey();
095: CacheFactory cacheFactory = getFactory(name);
096: BeanWrapper wrapper = new BeanWrapper(cacheFactory);
097: Iterator iterator = factoryNode.getChildren().iterator();
098: Node node;
099:
100: while (iterator.hasNext()) {
101: node = (Node) iterator.next();
102:
103: setFactoryProperty(name, wrapper, node.getKey(), node
104: .getValue());
105: }
106:
107: LOG.debug(name + " => " + cacheFactory);
108: }
109:
110: private void setFactoryProperty(String cacheName,
111: BeanWrapper factoryWrapper, String name, String value)
112: throws ConfigurationException {
113:
114: CacheFactory factory;
115:
116: try {
117: Class type = factoryWrapper.getType(name);
118:
119: if (CacheFactory.class.isAssignableFrom(type)) {
120: factory = getFactory(value);
121:
122: if (factory == null) {
123: LOG
124: .warn("factory '"
125: + value
126: + "' is not defined. Property '"
127: + name
128: + "' can not be set on '"
129: + cacheName
130: + "'. Setting zero cache factory instead.");
131:
132: factory = ZeroCacheFactory.NULL_CACHE_FACTORY;
133: }
134:
135: factoryWrapper.setProperty(name, factory);
136: } else {
137: factoryWrapper.setProperty(name, value);
138: }
139: } catch (Exception e) {
140: throw new ConfigurationException(e);
141: }
142: }
143:
144: public CacheFactory getFactory(String name) {
145: return (CacheFactory) factories.get(name);
146: }
147: }
|