1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.strutsit.chain.configuration;
25
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29
30 import org.apache.commons.chain.Catalog;
31 import org.apache.commons.chain.Command;
32 import org.apache.commons.chain.impl.CatalogBase;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.strutsit.chain.ChainException;
36 import org.strutsit.chain.DependCommand;
37 import org.strutsit.chain.interfaces.ChainConfiguration;
38
39 /***
40 * Base implementation of a {@link ChainConfiguration}-interface.
41 *
42 * @author <a href="mailto:wolff@struts-it.de">Manfred Wolff </a>
43 * @since JDK 1.4
44 * @version $Revision: 1.4 $
45 */
46 public abstract class ChainConfigurationBase implements ChainConfiguration {
47
48
49 private boolean initHasCalled = false;
50
51
52 private static transient Log log = LogFactory
53 .getLog(ChainConfigurationBase.class);
54
55
56 private List names = new ArrayList();
57
58
59 private List classes = new ArrayList();
60
61 /***
62 * Default name of the configfile.
63 */
64 private String configFile = "/chain-config.xml";
65
66 /***
67 * @return Returns the configFile.
68 */
69 public final String getConfigFile() {
70 return configFile;
71 }
72
73 /***
74 * Sets the configfile.
75 *
76 * @param aConfigFile The configFile to set.
77 */
78 public final void setConfigFile(final String aConfigFile) {
79 this.configFile = aConfigFile;
80 }
81
82 /***
83 * Checks the configuration of all chains
84 *
85 * @return true if all dependencies are all right, false otherwise
86 * @throws ChainException
87 */
88 public boolean checkConfiguration() throws ChainException {
89
90 boolean check = true;
91 String exError = null;
92
93 if (log.isTraceEnabled()) {
94 log.trace("checkConfiguration -> START");
95 }
96
97 if (!this.initHasCalled) {
98 init();
99 }
100
101 CatalogBase catalog = (CatalogBase) getCatalog();
102 if (catalog == null) { throw new IllegalArgumentException(
103 "Error, not catalog initialized."); }
104
105
106
107
108 for (Iterator it = catalog.getNames(); it.hasNext();) {
109
110 String name = (String) it.next();
111 Command chain = catalog.getCommand(name);
112
113
114
115 if ((chain instanceof ChainConfBase)
116 && ("true".equals(((ChainConfBase) chain)
117 .getCheckDependencies()))) {
118
119 Command commands[] = ((ChainConfBase) chain).getAllCommands();
120 names.clear();
121 classes.clear();
122
123 for (int i = 0; i < commands.length; ++i) {
124
125 Command dependCommand = commands[i];
126
127
128 names.add(name);
129 classes.add(dependCommand.getClass().getName());
130
131 if (dependCommand instanceof DependCommand) {
132
133
134 ((DependCommand) dependCommand).doRegister();
135
136 List commandNames = ((DependCommand) dependCommand)
137 .getNames();
138 List commandClassNames = ((DependCommand) dependCommand)
139 .getClassNames();
140
141
142
143
144
145 for (Iterator it2 = commandNames.iterator(); it2
146 .hasNext();) {
147
148
149
150
151
152 check = false;
153 String commandName = (String) it2.next();
154 if (log.isInfoEnabled()) {
155 log.info("Looking for dependency: "
156 + commandName);
157 }
158 if (!names.contains(commandName)) {
159 exError = new StringBuffer().append(
160 "Failure: [").append(
161 dependCommand.getClass().getName())
162 .append("] Don't find [").append(
163 commandName).append(
164 "] in namelist.").toString();
165 log.error(exError);
166 continue;
167 } else {
168 check = true;
169 break;
170 }
171 }
172
173
174
175
176
177 for (Iterator it2 = commandClassNames.iterator(); it2
178 .hasNext();) {
179
180
181
182
183
184 check = false;
185 String commandClassName = (String) it2.next();
186 if (log.isInfoEnabled()) {
187 log.info("Looking for dependency: "
188 + commandClassName);
189 }
190 if (!classes.contains(commandClassName)) {
191 exError = new StringBuffer().append(
192 "Failure: [").append(
193 dependCommand.getClass().getName())
194 .append("] Don't find [").append(
195 commandClassName).append(
196 "] in classnamelist.")
197 .toString();
198 ;
199 log.error(exError);
200 continue;
201 } else {
202 check = true;
203 break;
204 }
205 }
206 }
207 }
208 if (check == false) { throw new ChainException(exError); }
209 }
210 }
211
212 if (log.isTraceEnabled()) {
213 log.trace("checkConfiguration -> END");
214 }
215
216 return check;
217
218 }
219
220 /***
221 * Inits the configuration.
222 *
223 * @throws ChainException if something goes wrong
224 */
225 public void init() throws ChainException {
226
227
228 PropertyReader reader = PropertyReader.getInstance();
229 setConfigFile(reader.getConfName());
230 initHasCalled = true;
231 }
232
233 /***
234 * Rereads the configuration.
235 */
236 public void reread() {
237
238 }
239
240 /***
241 * @return Returns the catalog.
242 */
243 public abstract Catalog getCatalog();
244
245 }