CompositeWorkerContainer.java

biz/hammurapi/util/CompositeWorkerContainer.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 082 Parenthesis are redundant. 2 164:94
Java Inspector 089 Type documentation is too short. It is only 2 words. Should be at least 3 words. 2 51:1
Java Inspector 089 Undocumented method 2 59:17
Java Inspector 089 Undocumented method 2 63:17
Java Inspector 089 Undocumented constructor 2 70:9
Java Inspector 089 Parameter name is not documented 2 79:9
Java Inspector 089 Method return value is not properly documented 2 79:9
Java Inspector 089 Undocumented method 2 122:9
Java Inspector 089 Undocumented method 2 133:9
Java Inspector 089 Undocumented method 2 146:9
Java Inspector 089 Undocumented method 2 150:9
Java Inspector 089 Undocumented method 2 163:81
Java Inspector 089 Undocumented parameter job 2 193:9
Java Inspector 089 Method return value is not documented 2 193:9
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 153:75
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 156:37
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 158:73
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 158:99
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 165:146
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 165:182
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 175:82
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 179:120
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 179:156
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 182:67
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 182:93
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 70:9

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.util;
24
25import java.util.ArrayList;
26import java.util.Collections;
27import java.util.HashMap;
28import java.util.Iterator;
29import java.util.List;
30import java.util.Map;
31
32import org.w3c.dom.Element;
33import org.w3c.dom.Node;
34import org.w3c.dom.NodeList;
35
36import biz.hammurapi.config.ClassAcceptor;
37import biz.hammurapi.config.Component;
38import biz.hammurapi.config.ConfigurationException;
39import biz.hammurapi.config.Context;
40import biz.hammurapi.config.DomConfigFactory;
41import biz.hammurapi.config.DomConfigurable;
42import biz.hammurapi.config.PathNavigator;
43import biz.hammurapi.xml.dom.DOMUtils;
44
45
46/**
47 * Simple container
48 * @author Pavel Vlasov
49 * @revision $Revision$
50 */
51public class CompositeWorkerContainer implements Component, DomConfigurable, Worker, Context {
52 private Map workerMap=new HashMap();
53 private List workers=new ArrayList();
54 private boolean started;
55 private Object parent;
56
57 private PathNavigator pathNavigator=new PathNavigator(this) {
58
59 protected Object getParent() {
60 return parent;
61 }
62
63 protected Object getChild(String name) {
64 return workerMap.get(name);
65 }
66
67 };
68
69
70 public CompositeWorkerContainer() {
71 // Default constructor
72 }
73
74 /**
75 * Looks up component in component tree.
76 * @param name
77 * @return
78 */
79 public Object get(String name) {
80 return pathNavigator.get(name);
81 }
82
83 /**
84 * Adds component and starts it.
85 * @param name
86 * @param worker
87 * @throws ConfigurationException
88 */
89 private void addWorker(String name, Worker worker) throws ConfigurationException {
90 if (name!=null) {
91 // Remove previous worker
92 Object prevWorkerWithSameName=workerMap.remove(name);
93 if (prevWorkerWithSameName!=null) {
94 workers.remove(prevWorkerWithSameName);
95 if (prevWorkerWithSameName instanceof Component) {
96 if (started) {
97 ((Component) prevWorkerWithSameName).stop();
98 }
99 ((Component) prevWorkerWithSameName).setOwner(null);
100 }
101 }
102 }
103
104 if (worker!=null) {
105 // Add new worker
106 if (worker instanceof Component) {
107 Component theComponent = (Component) worker;
108 theComponent.setOwner(this);
109 if (started) {
110 theComponent.start();
111 }
112 }
113
114 if (name!=null) {
115 workerMap.put(name, worker);
116 }
117
118 workers.add(worker);
119 }
120 }
121
122 public void start() throws ConfigurationException {
123 Iterator it=workers.iterator();
124 while (it.hasNext()) {
125 Object worker = it.next();
126 if (worker instanceof Component) {
127 ((Component) worker).start();
128 }
129 }
130 started=true;
131 }
132
133 public void stop() throws ConfigurationException {
134 started=false;
135 List reverseWorkerList=new ArrayList(workers);
136 Collections.reverse(reverseWorkerList);
137 Iterator it=reverseWorkerList.iterator();
138 while (it.hasNext()) {
139 Object worker = it.next();
140 if (worker instanceof Component) {
141 ((Component) worker).stop();
142 }
143 }
144 }
145
146 public void setOwner(Object owner) {
147 parent=owner;
148 }
149
150 public void configure(Node configNode, Context context, ClassLoader classLoader) throws ConfigurationException {
151 DomConfigFactory factory=new DomConfigFactory(context);
152 try {
153 NodeList nl = DOMUtils.selectNodeList(configNode, "worker|worker-ref");
154 for (int i=0, l=nl.getLength(); i<l; ++i) {
155 Element we = (Element) nl.item(i);
156 if ("worker".equals(we.getNodeName())) {
157 addWorker(
158 we.hasAttribute("name") ? we.getAttribute("name") : null,
159 (Worker)factory.create(
160 we,
161 new ClassAcceptor() {
162
163 public void accept(Class clazz) throws ConfigurationException {
164 if (!(Worker.class.isAssignableFrom(clazz))) {
165 throw new ConfigurationException(clazz.getName()+" does not implement "+Worker.class+" interface.");
166 }
167 }
168
169 },
170 null));
171 } else {
172 String ref = DOMUtils.getElementText(we);
173 Object referencedWorker=get(ref);
174 if (referencedWorker==null) {
175 throw new ConfigurationException("Invalid worker reference: "+ref);
176 }
177
178 if (!(referencedWorker instanceof Worker)) {
179 throw new ConfigurationException(referencedWorker.getClass().getName()+" does not implement "+Worker.class+" interface.");
180 }
181
182 addWorker(we.hasAttribute("name") ? we.getAttribute("name") : null, (Worker) referencedWorker);
183 }
184 }
185 } catch (Exception e) {
186 throw new ConfigurationException(e);
187 }
188 }
189
190 /**
191 * Gives job to workers until one of them accepts it.
192 */
193 public boolean post(Runnable job) {
194 Iterator it=workers.iterator();
195 while (it.hasNext()) {
196 Worker worker=(Worker) it.next();
197 if (worker.post(job)) {
198 return true;
199 }
200 }
201 return false;
202 }
203}
204