KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.jboss.cache.CacheImpl;
12
13 import java.io.Serializable JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Set JavaDoc;
18
19 /**
20  * Base superclass of Cache configuration classes that expose some properties
21  * that can be changed after the cache is started.
22  *
23  * @author <a HREF="brian.stansberry@jboss.com">Brian Stansberry</a>
24  * @version $Revision: 1.4 $
25  * @see #testImmutability(String)
26  */

27 public class ConfigurationComponent implements Serializable JavaDoc, Cloneable JavaDoc
28 {
29    private static final long serialVersionUID = 4879873994727821938L;
30
31    protected Log log = LogFactory.getLog(getClass());
32    private CacheImpl cache; // back-reference to test whether the cache is running.
33
private Set JavaDoc<ConfigurationComponent> children =
34            Collections.synchronizedSet(new HashSet JavaDoc<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 JavaDoc<? 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 JavaDoc<? 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 JavaDoc<? extends ConfigurationComponent> oldConfigs,
84                                       Collection JavaDoc<? extends ConfigurationComponent> newConfigs)
85    {
86       synchronized (children)
87       {
88          removeChildConfigs(oldConfigs);
89          addChildConfigs(newConfigs);
90       }
91    }
92
93    /**
94     * Checks field modifications via setters
95     *
96     * @param fieldName
97     */

98    protected void testImmutability(String JavaDoc 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 JavaDoc e)
108       {
109          log.warn("Field " + fieldName + " not found!!");
110       }
111    }
112
113    protected CacheImpl getTreeCache()
114    {
115       return cache;
116    }
117
118    /**
119     * Sets a back-reference to the cache associated with this configuration
120     *
121     * @param cache
122     */

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 JavaDoc
136    {
137       ConfigurationComponent c = (ConfigurationComponent) super.clone();
138       c.setCacheImpl(null);
139       return c;
140    }
141
142    /**
143     * Null-safe equality test.
144     * <p/>
145     * FIXME this must be written elsewhere.
146     */

147    protected static boolean safeEquals(Object JavaDoc a, Object JavaDoc b)
148    {
149       return (a == b) || (a != null && a.equals(b));
150    }
151 }
152
Popular Tags