GenericContainer.java

biz/hammurapi/config/GenericContainer.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 015 Do not change parameter value. For comprehensibility, formal parameters should be final 2 62:25
Java Inspector 089 Undocumented top level type 2 19:1
Java Inspector 089 Undocumented method 2 23:9
Java Inspector 089 Undocumented method 2 32:25
Java Inspector 089 Undocumented method 2 36:25
Java Inspector 089 Parameter name is not documented 2 50:9
Java Inspector 089 Method return value is not properly documented 2 50:9
Java Inspector 089 Parameter name is not documented 2 60:9
Java Inspector 089 Parameter component is not documented 2 60:9
Java Inspector 089 Undocumented method 2 84:41
Java Inspector 089 Undocumented method 2 113:9
Java Inspector 089 Undocumented method 2 128:9
Java Inspector 089 Undocumented method 2 144:9
Java Inspector 089 Undocumented parameter executionContext 2 151:9
Java Inspector 089 Undocumented method 2 161:9
Java Inspector 089 Undocumented method 2 165:9
Java Inspector 089 Undocumented method 2 169:9
Java Inspector 089 Undocumented method 2 173:9
Java Inspector 089 Undocumented method 2 177:9
Java Inspector 089 Undocumented method 2 181:9
Java Inspector 089 Undocumented method 2 185:9
Java Inspector 089 Undocumented method 2 192:9
Java Inspector 089 Undocumented method 2 196:9
Java Inspector 089 Parameter ownerType is not documented 2 207:9
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 62:32
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 86:106

Source code

1package biz.hammurapi.config;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.Collections;
6import java.util.Iterator;
7import java.util.LinkedHashMap;
8import java.util.List;
9import java.util.Map;
10import java.util.Set;
11
12import biz.hammurapi.metrics.MeasurementCollector;
13import biz.hammurapi.metrics.MeasurementConsumer;
14import biz.hammurapi.util.Attributable;
15import biz.hammurapi.util.CollectionVisitable;
16import biz.hammurapi.util.VisitableBase;
17import biz.hammurapi.util.Visitor;
18
19public class GenericContainer extends VisitableBase implements Component, Command, Attributable, Context, MeasurementCollector, MeasurementConsumer {
20
21 private Map componentMap = new LinkedHashMap();
22
23 protected Map getComponentMap() {
24 return componentMap;
25 }
26
27 private boolean started;
28 private Object owner;
29
30 private PathNavigator pathNavigator = new PathNavigator(this) {
31
32 protected Object getParent() {
33 return owner;
34 }
35
36 protected Object getChild(String name) {
37 return componentMap.get(name);
38 }
39
40 };
41
42 private LinkedHashMap attributes = new LinkedHashMap();
43 private MeasurementConsumer measurementConsumer;
44
45 /**
46 * Looks up component in component tree.
47 * @param name
48 * @return
49 */
50 public Object get(String name) {
51 return pathNavigator.get(name);
52 }
53
54 /**
55 * Adds component and starts it.
56 * @param name
57 * @param component
58 * @throws ConfigurationException
59 */
60 public void addComponent(String name, Object component) throws ConfigurationException {
61 if (name==null) {
62 name = "Anonymous component "+componentMap.size();
63 }
64
65 // Remove previous component
66 Object prevComponent=componentMap.remove(name);
67 if (prevComponent!=null) {
68 if (prevComponent instanceof Component) {
69 if (started) {
70 ((Component) prevComponent).stop();
71 }
72 ((Component) prevComponent).setOwner(null);
73 }
74
75 if (prevComponent instanceof MeasurementCollector) {
76 ((MeasurementCollector) prevComponent).setMeasurementConsumer(null);
77 }
78 }
79
80 if (component!=null) {
81 if (component instanceof MeasurementCollector) {
82 final String componentName = name;
83 MeasurementConsumer cmc = new MeasurementConsumer() {
84 public void addMeasurement(String mName, double value, long time) {
85 if (measurementConsumer!=null) {
86 measurementConsumer.addMeasurement(componentName+"."+mName, value, time==0 ? System.currentTimeMillis() : time);
87 }
88 }
89 };
90 ((MeasurementCollector) component).setMeasurementConsumer(cmc);
91 }
92
93 if (component instanceof NamedComponent) {
94 ((NamedComponent) component).setName(name);
95 }
96
97 // Add new component
98 if (component instanceof Component) {
99 Component theComponent = (Component) component;
100 theComponent.setOwner(this);
101 if (started) {
102 theComponent.start();
103 }
104 }
105
106 if (name!=null) {
107 componentMap.put(name, component);
108 }
109
110 }
111 }
112
113 public void start() throws ConfigurationException {
114 if (getMeasurementConsumer() instanceof Component) {
115 ((Component) getMeasurementConsumer()).start();
116 }
117
118 Iterator it=componentMap.values().iterator();
119 while (it.hasNext()) {
120 Object component = it.next();
121 if (component instanceof Component) {
122 ((Component) component).start();
123 }
124 }
125 started=true;
126 }
127
128 public void stop() throws ConfigurationException {
129 started=false;
130 List reverseComponentList=new ArrayList(componentMap.values());
131 Collections.reverse(reverseComponentList);
132 Iterator it=reverseComponentList.iterator();
133 while (it.hasNext()) {
134 Object component = it.next();
135 if (component instanceof Component) {
136 ((Component) component).stop();
137 }
138 }
139 if (getMeasurementConsumer() instanceof Component) {
140 ((Component) getMeasurementConsumer()).stop();
141 }
142 }
143
144 public void setOwner(Object owner) {
145 this.owner=owner;
146 }
147
148 /**
149 * Invokes execute() of executable components sequentially.
150 */
151 public void execute(Object executionContext) {
152 Iterator it=componentMap.values().iterator();
153 while (it.hasNext()) {
154 Object component = it.next();
155 if (component instanceof Command) {
156 ((Command) component).execute(executionContext);
157 }
158 }
159 }
160
161 protected void acceptChildren(Visitor visitor) {
162 new CollectionVisitable(componentMap.values(), false).accept(visitor);
163 }
164
165 protected Collection getComponents() {
166 return componentMap.values();
167 }
168
169 public Set getComponentNames() {
170 return Collections.unmodifiableSet(componentMap.keySet());
171 }
172
173 public void setAttribute(Object key, Object value) {
174 attributes.put(key, value);
175 }
176
177 public Object getAttribute(Object key) {
178 return attributes.get(key);
179 }
180
181 public Object removeAttribute(Object key) {
182 return attributes.remove(key);
183 }
184
185 public void setMeasurementConsumer(MeasurementConsumer measurementConsumer) {
186 this.measurementConsumer = measurementConsumer;
187 if (measurementConsumer instanceof Component) {
188 ((Component) measurementConsumer).setOwner(this);
189 }
190 }
191
192 public MeasurementConsumer getMeasurementConsumer() {
193 return measurementConsumer;
194 }
195
196 public void addMeasurement(String name, double value, long time) {
197 if (measurementConsumer!=null) {
198 measurementConsumer.addMeasurement(name, value, time);
199 }
200 }
201
202 /**
203 * Finds component owner of particular type.
204 * @param ownerType
205 * @return Owner which is an instance of specified type or null if no such owner is found.
206 */
207 public Object getOwner(Class ownerType) {
208 if (owner==null || ownerType.isInstance(owner)) {
209 return owner;
210 }
211
212 if (owner instanceof ComponentBase) {
213 return ((ComponentBase) owner).getOwner(ownerType);
214 }
215
216 if (owner instanceof GenericContainer) {
217 return ((GenericContainer) owner).getOwner(ownerType);
218 }
219
220 return null;
221 }
222}
223