View Javadoc

1   /*
2    * $Id: DefinitionsFactoryConfig.java 421151 2006-07-12 06:07:14Z wsmoak $
3    *
4    * Copyright 1999-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.struts.tiles;
20  
21  import java.io.Serializable;
22  import java.lang.reflect.InvocationTargetException;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.apache.commons.beanutils.BeanUtils;
29  
30  /***
31   * A TilesFactoryConfig object hold configuration attributes for a tile
32   * definition factory.
33   *
34   * @since Struts 1.1
35   * @version $Rev: 421151 $ $Date: 2006-07-11 23:07:14 -0700 (Tue, 11 Jul 2006) $
36   */
37  public class DefinitionsFactoryConfig implements Serializable {
38  
39      /***
40       * Fully qualified classname of the factory to create.
41       * If no classname is set, a default factory is created
42       * (of class "org.apache.struts.tiles.xmlDefinition.I18nFactorySet").
43       */
44      protected String factoryClassname =
45          "org.apache.struts.tiles.xmlDefinition.I18nFactorySet";
46  
47      /***
48       * Specifies whether the parser will validate configuration files.
49       * Default value is true.
50       */
51      protected boolean parserValidate = true;
52  
53      /***
54       * Definition configuration file specified by user.
55       */
56      protected String definitionConfigFiles = null;
57  
58      /***
59       * Specifies whether the factory is "module-aware".
60       */
61      protected boolean moduleAware = true;
62  
63      /***
64       * The name associated to this factory.
65       * <br>
66       * With Struts 1.1, this name is the module name to which this factory
67       * belong. It is set by the system.
68       * <br>
69       * In prior versions, this property is not used.
70       */
71      protected String factoryName;
72  
73      /***
74       * Alternate name for parser debug details properties in configuration file.
75       * @deprecated This will be removed in a release after Struts 1.2.
76       */
77      public static final String PARSER_DETAILS_PARAMETER_NAME =
78          "definitions-parser-details";
79  
80      /***
81       * Alternate name for parser validate properties in configuration file.
82       */
83      public static final String PARSER_VALIDATE_PARAMETER_NAME =
84          "definitions-parser-validate";
85  
86      /***
87       * Alternate name for factory classname properties in configuration file.
88       */
89      public static final String FACTORY_CLASSNAME_PARAMETER_NAME =
90          "definitions-factory-class";
91  
92      /***
93       * Alternate name for definition files properties in configuration file.
94       */
95      public static final String DEFINITIONS_CONFIG_PARAMETER_NAME =
96          "definitions-config";
97  
98      /***
99       * Alternate name for definition debug details properties in configuration file.
100      * @deprecated This will be removed in a release after Struts 1.2.
101      */
102     public static final String TILES_DETAILS_PARAMETER_NAME = "definitions-debug";
103 
104     /***
105      * Map of extra attribute available.
106      */
107     private Map extraAttributes = new HashMap();
108 
109     /***
110      * Default constructor.
111      */
112     public DefinitionsFactoryConfig() {
113         super();
114     }
115 
116     /***
117      * Constructor.
118      * Create configuration object, and initialize it with parameters from Map.
119      * Parameters corresponding to an attribute are filtered and stored in appropriate
120      * attribute.
121      * @param initParameters Map.
122      */
123     public DefinitionsFactoryConfig(Map initParameters) {
124         super();
125     }
126 
127     /***
128      * Get the module aware flag.
129      * @return <code>true</code>: user wants a single factory instance,
130      * <code>false</code>: user wants multiple factory instances (one per module with Struts)
131      */
132     public boolean isModuleAware() {
133         return moduleAware;
134     }
135     /***
136      * Set the module aware flag.
137      * @param moduleAware <code>true</code>: user wants a single factory instance,
138      * <code>false</code>: user wants multiple factory instances (one per module with Struts)
139      */
140     public void setModuleAware(boolean moduleAware) {
141         this.moduleAware = moduleAware;
142     }
143 
144     /***
145      * Get the classname of the factory.
146      * @return Classname.
147      */
148     public String getFactoryClassname() {
149         return factoryClassname;
150     }
151 
152     /***
153      * Set the classname of the factory..
154      * @param aFactoryClassname Classname of the factory.
155      */
156     public void setFactoryClassname(String aFactoryClassname) {
157         factoryClassname = aFactoryClassname;
158     }
159 
160     /***
161      * Determines if the parser is validating.
162      * @return <code>true<code> when in validating mode.
163      */
164     public boolean getParserValidate() {
165         return parserValidate;
166     }
167 
168     /***
169      * Set the validating mode for the parser.
170      * @param aParserValidate <code>true</code> for validation, <code>false</code> otherwise
171      */
172     public void setParserValidate(boolean aParserValidate) {
173         parserValidate = aParserValidate;
174     }
175 
176     /***
177      * Get the definition config files.
178      * @return Defition config files.
179      */
180     public String getDefinitionConfigFiles() {
181         return definitionConfigFiles;
182     }
183 
184     /***
185      * Set the definition config files.
186      * @param aDefinitionConfigFiles Definition config files.
187      */
188     public void setDefinitionConfigFiles(String aDefinitionConfigFiles) {
189         definitionConfigFiles = aDefinitionConfigFiles;
190     }
191 
192     /***
193      * Set value of an additional attribute.
194      * @param name Name of the attribute.
195      * @param value Value of the attribute.
196      */
197     public void setAttribute(String name, Object value) {
198         extraAttributes.put(name, value);
199     }
200 
201     /***
202      * Get value of an additional attribute.
203      * @param name Name of the attribute.
204      * @return Value of the attribute, or null if not found.
205      */
206     public Object getAttribute(String name) {
207         return extraAttributes.get(name);
208     }
209 
210     /***
211      * Get additional attributes as a Map.
212      * @return Map A Map containing attribute name - value pairs.
213      */
214     public Map getAttributes() {
215         Map map = new HashMap(extraAttributes);
216         // Add property attributes using old names
217         /*
218           map.put(DEFINITIONS_CONFIG_PARAMETER_NAME, getDefinitionConfigFiles());
219           map.put(TILES_DETAILS_PARAMETER_NAME, Integer.toString(getDebugLevel()) );
220           map.put(PARSER_DETAILS_PARAMETER_NAME, Integer.toString(getParserDebugLevel()) );
221           map.put(PARSER_VALIDATE_PARAMETER_NAME, new Boolean(getParserValidate()).toString() );
222 
223           if( ! "org.apache.struts.tiles.xmlDefinition.I18nFactorySet".equals(getFactoryClassname()) )
224           map.put(FACTORY_CLASSNAME_PARAMETER_NAME, getFactoryClassname());
225         */
226         return map;
227     }
228 
229     /***
230      * Populate this config object from properties map, based on
231      * the specified name/value pairs.  This method uses the populate() method from
232      * org.apache.commons.beanutils.BeanUtil.
233      * <p>
234      * Properties keys are scanned for old property names, and linked to the new name
235      * if necessary. This modifies the properties map.
236      * <p>
237      * The particular setter method to be called for each property is
238      * determined using the usual JavaBeans introspection mechanisms.  Thus,
239      * you may identify custom setter methods using a BeanInfo class that is
240      * associated with the class of the bean itself.  If no such BeanInfo
241      * class is available, the standard method name conversion ("set" plus
242      * the capitalized name of the property in question) is used.
243      * <p>
244      * <strong>NOTE</strong>:  It is contrary to the JavaBeans Specification
245      * to have more than one setter method (with different argument
246      * signatures) for the same property.
247      *
248      * @param properties Map keyed by property name, with the
249      *  corresponding (String or String[]) value(s) to be set.
250      *
251      * @exception IllegalAccessException if the caller does not have
252      *  access to the property accessor method.
253      * @exception InvocationTargetException if the property accessor method
254      *  throws an exception.
255      * @see org.apache.commons.beanutils.BeanUtils
256      */
257     public void populate(Map properties)
258         throws IllegalAccessException, InvocationTargetException {
259 
260         // link old parameter names for backward compatibility
261         linkOldPropertyNames(properties);
262         BeanUtils.populate(this, properties);
263     }
264 
265     /***
266      * Link old property names to new property names.
267      * This modifies the map.
268      * @param properties Map keyed by property name, with the
269      *  corresponding (String or String[]) value(s) to be set.
270      */
271     static public void linkOldPropertyNames(Map properties) {
272         Set entries = properties.entrySet();
273         Map toAdd = new HashMap();
274         Iterator i = entries.iterator();
275         while (i.hasNext()) {
276             Map.Entry entry = (Map.Entry) i.next();
277 
278             if (DEFINITIONS_CONFIG_PARAMETER_NAME.equals(entry.getKey())) {
279                 toAdd.put("definitionConfigFiles", entry.getValue());
280 
281             } else if (FACTORY_CLASSNAME_PARAMETER_NAME.equals(entry.getKey())) {
282                 toAdd.put("factoryClassname", entry.getValue());
283 
284             } else if (PARSER_DETAILS_PARAMETER_NAME.equals(entry.getKey())) {
285                 toAdd.put("parserDebugLevel", entry.getValue());
286 
287             } else if (PARSER_VALIDATE_PARAMETER_NAME.equals(entry.getKey())) {
288                 toAdd.put("parserValidate", entry.getValue());
289 
290             } else if (TILES_DETAILS_PARAMETER_NAME.equals(entry.getKey())) {
291                 toAdd.put("debugLevel", entry.getValue());
292             }
293         }
294 
295         if (toAdd.size() > 0) {
296             properties.putAll(toAdd);
297         }
298     }
299 
300     /***
301      * Get the factory name.
302      */
303     public String getFactoryName() {
304         return factoryName;
305     }
306     /***
307      * Set the factory name.
308      * @param factoryName Name of the factory.
309      */
310     public void setFactoryName(String factoryName) {
311         this.factoryName = factoryName;
312     }
313 }