1 7 package org.jboss.cache.config; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.jboss.cache.CacheImpl; 12 13 import java.io.Serializable ; 14 import java.util.Collection ; 15 import java.util.Collections ; 16 import java.util.HashSet ; 17 import java.util.Set ; 18 19 27 public class ConfigurationComponent implements Serializable , Cloneable 28 { 29 private static final long serialVersionUID = 4879873994727821938L; 30 31 protected Log log = LogFactory.getLog(getClass()); 32 private CacheImpl cache; private Set <ConfigurationComponent> children = 34 Collections.synchronizedSet(new HashSet <ConfigurationComponent>()); 35 36 protected ConfigurationComponent() 37 { 38 } 39 40 public void passCacheToChildConfig(ConfigurationComponent child) 41 { 42 if (child != null) 43 { 44 child.setCacheImpl(cache); 45 } 46 } 47 48 protected void addChildConfig(ConfigurationComponent child) 49 { 50 if (child != null && children.add(child)) 51 child.setCacheImpl(cache); 52 } 53 54 protected void addChildConfigs(Collection <? extends ConfigurationComponent> toAdd) 55 { 56 if (toAdd != null) 57 { 58 for (ConfigurationComponent child : toAdd) 59 addChildConfig(child); 60 } 61 } 62 63 protected void removeChildConfig(ConfigurationComponent child) 64 { 65 children.remove(child); 66 } 67 68 protected void removeChildConfigs(Collection <? extends ConfigurationComponent> toRemove) 69 { 70 if (toRemove != null) 71 { 72 for (ConfigurationComponent child : toRemove) 73 removeChildConfig(child); 74 } 75 } 76 77 protected void replaceChildConfig(ConfigurationComponent oldConfig, ConfigurationComponent newConfig) 78 { 79 removeChildConfig(oldConfig); 80 addChildConfig(newConfig); 81 } 82 83 protected void replaceChildConfigs(Collection <? extends ConfigurationComponent> oldConfigs, 84 Collection <? extends ConfigurationComponent> newConfigs) 85 { 86 synchronized (children) 87 { 88 removeChildConfigs(oldConfigs); 89 addChildConfigs(newConfigs); 90 } 91 } 92 93 98 protected void testImmutability(String fieldName) 99 { 100 try 101 { 102 if (cache != null && cache.started && !getClass().getDeclaredField(fieldName).isAnnotationPresent(Dynamic.class)) 103 { 104 throw new ConfigurationException("Attempted to modify a non-Dynamic configuration element [" + fieldName + "] after the cache has started!"); 105 } 106 } 107 catch (NoSuchFieldException e) 108 { 109 log.warn("Field " + fieldName + " not found!!"); 110 } 111 } 112 113 protected CacheImpl getTreeCache() 114 { 115 return cache; 116 } 117 118 123 public void setCacheImpl(CacheImpl cache) 124 { 125 this.cache = cache; 126 synchronized (children) 127 { 128 for (ConfigurationComponent child : children) 129 { 130 child.setCacheImpl(cache); 131 } 132 } 133 } 134 135 public ConfigurationComponent clone() throws CloneNotSupportedException 136 { 137 ConfigurationComponent c = (ConfigurationComponent) super.clone(); 138 c.setCacheImpl(null); 139 return c; 140 } 141 142 147 protected static boolean safeEquals(Object a, Object b) 148 { 149 return (a == b) || (a != null && a.equals(b)); 150 } 151 } 152 | Popular Tags |