View Javadoc
1   /*
2    * 
3    * $Revision: 1.4 $
4    * $Date: 2004/07/09 18:02:27 $
5    *
6    * ====================================================================
7    * struts-it
8    * Copyright (C) 2004 - strutsit community
9    * 
10   * Licensed under the Apache License, Version 2.0 (the "License");
11   * you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   * 
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   * 
16   * Unless required by applicable law or agreed to in writing, software
17   * distributed under the License is distributed on an "AS IS" BASIS,
18   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   * See the License for the specific language governing permissions and
20   * limitations under the License.
21   *
22   * created: 2004-06-05  Manfred Wolff
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      /// Check if init is called.
49      private boolean initHasCalled = false;
50  
51      /// The logger of this class
52      private static transient Log log = LogFactory
53              .getLog(ChainConfigurationBase.class);
54  
55      /// Map with all names of commands
56      private List names = new ArrayList();
57  
58      /// Map with all classnames of commands
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         // Iterates over all names of the Catalog. That maybe commands or
106         // chains.
107         // Only chains with the Flag checkDependencies should be verified.
108         for (Iterator it = catalog.getNames(); it.hasNext();) {
109 
110             String name = (String) it.next();
111             Command chain = catalog.getCommand(name);
112 
113             // Only verify if it is a chain and the checkDependencies attribute
114             // is true
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                     // registers the command with name and classname
128                     names.add(name);
129                     classes.add(dependCommand.getClass().getName());
130 
131                     if (dependCommand instanceof DependCommand) {
132 
133                         // Registers the dependencies
134                         ((DependCommand) dependCommand).doRegister();
135 
136                         List commandNames = ((DependCommand) dependCommand)
137                                 .getNames();
138                         List commandClassNames = ((DependCommand) dependCommand)
139                                 .getClassNames();
140 
141                         // Iterates over all names, that depends to this
142                         // command.
143                         // This name must be in the names-List of this
144                         // configuration!
145                         for (Iterator it2 = commandNames.iterator(); it2
146                                 .hasNext();) {
147 
148                             // cannot be refactored out of the iteration,
149                             // because
150                             // this iteration might not be attached, maybe
151                             // empty.
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                         // Iterates over all classNames, that depends to this
174                         // command.
175                         // This name must be in the classNames-List of this
176                         // configuration!
177                         for (Iterator it2 = commandClassNames.iterator(); it2
178                                 .hasNext();) {
179 
180                             // cannot be refactored out of the iteration,
181                             // because
182                             // this iteration might not be attached, maybe
183                             // empty.
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         // creates a PropertyReader object
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 }