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