001: package biz.hammurapi.config;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Collections;
006: import java.util.Iterator;
007: import java.util.LinkedHashMap;
008: import java.util.List;
009: import java.util.Map;
010: import java.util.Set;
011:
012: import biz.hammurapi.metrics.MeasurementCollector;
013: import biz.hammurapi.metrics.MeasurementConsumer;
014: import biz.hammurapi.util.Attributable;
015: import biz.hammurapi.util.CollectionVisitable;
016: import biz.hammurapi.util.VisitableBase;
017: import biz.hammurapi.util.Visitor;
018:
019: public class GenericContainer extends VisitableBase implements
020: Component, Command, Attributable, Context,
021: MeasurementCollector, MeasurementConsumer {
022:
023: private Map componentMap = new LinkedHashMap();
024:
025: protected Map getComponentMap() {
026: return componentMap;
027: }
028:
029: private boolean started;
030: private Object owner;
031:
032: private PathNavigator pathNavigator = new PathNavigator(this ) {
033:
034: protected Object getParent() {
035: return owner;
036: }
037:
038: protected Object getChild(String name) {
039: return componentMap.get(name);
040: }
041:
042: };
043:
044: private LinkedHashMap attributes = new LinkedHashMap();
045: private MeasurementConsumer measurementConsumer;
046:
047: /**
048: * Looks up component in component tree.
049: * @param name
050: * @return
051: */
052: public Object get(String name) {
053: return pathNavigator.get(name);
054: }
055:
056: /**
057: * Adds component and starts it.
058: * @param name
059: * @param component
060: * @throws ConfigurationException
061: */
062: public void addComponent(String name, Object component)
063: throws ConfigurationException {
064: if (name == null) {
065: name = "Anonymous component " + componentMap.size();
066: }
067:
068: // Remove previous component
069: Object prevComponent = componentMap.remove(name);
070: if (prevComponent != null) {
071: if (prevComponent instanceof Component) {
072: if (started) {
073: ((Component) prevComponent).stop();
074: }
075: ((Component) prevComponent).setOwner(null);
076: }
077:
078: if (prevComponent instanceof MeasurementCollector) {
079: ((MeasurementCollector) prevComponent)
080: .setMeasurementConsumer(null);
081: }
082: }
083:
084: if (component != null) {
085: if (component instanceof MeasurementCollector) {
086: final String componentName = name;
087: MeasurementConsumer cmc = new MeasurementConsumer() {
088: public void addMeasurement(String mName,
089: double value, long time) {
090: if (measurementConsumer != null) {
091: measurementConsumer
092: .addMeasurement(
093: componentName + "." + mName,
094: value,
095: time == 0 ? System
096: .currentTimeMillis()
097: : time);
098: }
099: }
100: };
101: ((MeasurementCollector) component)
102: .setMeasurementConsumer(cmc);
103: }
104:
105: if (component instanceof NamedComponent) {
106: ((NamedComponent) component).setName(name);
107: }
108:
109: // Add new component
110: if (component instanceof Component) {
111: Component theComponent = (Component) component;
112: theComponent.setOwner(this );
113: if (started) {
114: theComponent.start();
115: }
116: }
117:
118: if (name != null) {
119: componentMap.put(name, component);
120: }
121:
122: }
123: }
124:
125: public void start() throws ConfigurationException {
126: if (getMeasurementConsumer() instanceof Component) {
127: ((Component) getMeasurementConsumer()).start();
128: }
129:
130: Iterator it = componentMap.values().iterator();
131: while (it.hasNext()) {
132: Object component = it.next();
133: if (component instanceof Component) {
134: ((Component) component).start();
135: }
136: }
137: started = true;
138: }
139:
140: public void stop() throws ConfigurationException {
141: started = false;
142: List reverseComponentList = new ArrayList(componentMap.values());
143: Collections.reverse(reverseComponentList);
144: Iterator it = reverseComponentList.iterator();
145: while (it.hasNext()) {
146: Object component = it.next();
147: if (component instanceof Component) {
148: ((Component) component).stop();
149: }
150: }
151: if (getMeasurementConsumer() instanceof Component) {
152: ((Component) getMeasurementConsumer()).stop();
153: }
154: }
155:
156: public void setOwner(Object owner) {
157: this .owner = owner;
158: }
159:
160: /**
161: * Invokes execute() of executable components sequentially.
162: */
163: public void execute(Object executionContext) {
164: Iterator it = componentMap.values().iterator();
165: while (it.hasNext()) {
166: Object component = it.next();
167: if (component instanceof Command) {
168: ((Command) component).execute(executionContext);
169: }
170: }
171: }
172:
173: protected void acceptChildren(Visitor visitor) {
174: new CollectionVisitable(componentMap.values(), false)
175: .accept(visitor);
176: }
177:
178: protected Collection getComponents() {
179: return componentMap.values();
180: }
181:
182: public Set getComponentNames() {
183: return Collections.unmodifiableSet(componentMap.keySet());
184: }
185:
186: public void setAttribute(Object key, Object value) {
187: attributes.put(key, value);
188: }
189:
190: public Object getAttribute(Object key) {
191: return attributes.get(key);
192: }
193:
194: public Object removeAttribute(Object key) {
195: return attributes.remove(key);
196: }
197:
198: public void setMeasurementConsumer(
199: MeasurementConsumer measurementConsumer) {
200: this .measurementConsumer = measurementConsumer;
201: if (measurementConsumer instanceof Component) {
202: ((Component) measurementConsumer).setOwner(this );
203: }
204: }
205:
206: public MeasurementConsumer getMeasurementConsumer() {
207: return measurementConsumer;
208: }
209:
210: public void addMeasurement(String name, double value, long time) {
211: if (measurementConsumer != null) {
212: measurementConsumer.addMeasurement(name, value, time);
213: }
214: }
215:
216: /**
217: * Finds component owner of particular type.
218: * @param ownerType
219: * @return Owner which is an instance of specified type or null if no such owner is found.
220: */
221: public Object getOwner(Class ownerType) {
222: if (owner == null || ownerType.isInstance(owner)) {
223: return owner;
224: }
225:
226: if (owner instanceof ComponentBase) {
227: return ((ComponentBase) owner).getOwner(ownerType);
228: }
229:
230: if (owner instanceof GenericContainer) {
231: return ((GenericContainer) owner).getOwner(ownerType);
232: }
233:
234: return null;
235: }
236: }
|