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.definition;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import jakarta.servlet.ServletContext;
28  import jakarta.servlet.ServletRequest;
29  
30  import org.apache.struts.tiles.ComponentDefinition;
31  import org.apache.struts.tiles.DefinitionsFactory;
32  import org.apache.struts.tiles.DefinitionsFactoryConfig;
33  import org.apache.struts.tiles.DefinitionsFactoryException;
34  import org.apache.struts.tiles.NoSuchDefinitionException;
35  import org.apache.struts.util.RequestUtils;
36  
37  /**
38   * Wrapper from new definition factory interface to old interface.
39   * This class provides mapping from the old interface's life cycle to the new life cycle.
40   *
41   * @since 20020708
42   *
43   * @deprecated Use DefinitionsFactory instead.
44   */
45  @Deprecated
46  public class ComponentDefinitionsFactoryWrapper implements DefinitionsFactory {
47      private static final long serialVersionUID = -1996134499246652941L;
48  
49      /**
50       * The underlying factory.
51       */
52      private org.apache.struts.tiles.ComponentDefinitionsFactory factory = null;
53  
54      /**
55       * Factory configuration,
56       */
57      private DefinitionsFactoryConfig config = null;
58  
59      /**
60       * Constructor.
61       * Create new wrapper for specified factory.
62       * @param factory The factory to create a wrapper for.
63       */
64      public ComponentDefinitionsFactoryWrapper(org.apache.struts.tiles.ComponentDefinitionsFactory factory) {
65          this.factory = factory;
66      }
67  
68      /**
69       * Constructor.
70       * Create new wrapper.
71       * The config object passed to init method should reference a factory implementing
72       * {@link org.apache.struts.tiles.ComponentDefinitionsFactory}.
73       */
74      public ComponentDefinitionsFactoryWrapper() {
75          super();
76      }
77  
78      /**
79       * Get requested definition.
80       * @param name Name of the definition.
81       * @param request The request we are processing.
82       * @param servletContext Our servlet context.
83       * @return ComponentDefition
84       */
85      public ComponentDefinition getDefinition(
86          String name,
87          ServletRequest request,
88          ServletContext servletContext)
89          throws NoSuchDefinitionException, DefinitionsFactoryException {
90  
91          return factory.getDefinition(name, request, servletContext);
92      }
93  
94      /**
95       * Call underlying factory init method.
96       * @param config DefinitionsFactoryConfig.
97       * @param servletContext Our servlet context.
98       */
99      public void init(DefinitionsFactoryConfig config, ServletContext servletContext)
100         throws DefinitionsFactoryException {
101 
102         this.config = config;
103 
104         // create factory and initialize it
105         if (factory == null) {
106             factory = createFactoryInstance(config.getFactoryClassname());
107         }
108 
109         factory.initFactory(servletContext, createConfigMap(config));
110     }
111 
112     /**
113      * Do nothing because old life cycle has no equivalent.
114      */
115     public void destroy() {
116         factory = null;
117     }
118 
119     /**
120      * Set underlying factory configuration.
121      * @param config DefinitionsFactoryConfig to use.
122      * @param servletContext Our servlet context.
123      *
124      */
125     public void setConfig(
126         DefinitionsFactoryConfig config,
127         ServletContext servletContext)
128         throws DefinitionsFactoryException {
129 
130         org.apache.struts.tiles.ComponentDefinitionsFactory newFactory =
131             createFactoryInstance(config.getFactoryClassname());
132 
133         newFactory.initFactory(servletContext, createConfigMap(config));
134         factory = newFactory;
135     }
136 
137     /**
138      * Get underlying factory configuration.
139      * @return DefinitionsFactoryConfig.
140      */
141     public DefinitionsFactoryConfig getConfig() {
142         return config;
143     }
144 
145     /**
146      * Get internal factory.
147      * @return The internal ComponentDefitionsFactory.
148      */
149     public org.apache.struts.tiles.ComponentDefinitionsFactory getInternalFactory() {
150         return factory;
151     }
152 
153     /**
154      * Create Definition factory from provided classname which must implement
155      * {@link org.apache.struts.tiles.ComponentDefinitionsFactory}. Factory
156      * class must extend {@link DefinitionsFactory}.
157      *
158      * @param classname Class name of the factory to create.
159      *
160      * @return newly created factory.
161      *
162      * @throws DefinitionsFactoryException If an error occur while initializing factory
163      */
164     protected org.apache.struts.tiles.ComponentDefinitionsFactory createFactoryInstance(String classname)
165         throws DefinitionsFactoryException {
166 
167         try {
168             Class<?> factoryClass = RequestUtils.applicationClass(classname);
169             Object factory = factoryClass.newInstance();
170             return (org.apache.struts.tiles.ComponentDefinitionsFactory) factory;
171 
172         } catch (ClassCastException ex) { // Bad classname
173             throw new DefinitionsFactoryException(
174                 "Error - createDefinitionsFactory : Factory class '"
175                     + classname
176                     + " must implement 'DefinitionsFactory'.",
177                 ex);
178 
179         } catch (ClassNotFoundException ex) { // Bad classname
180             throw new DefinitionsFactoryException(
181                 "Error - createDefinitionsFactory : Bad class name '"
182                     + classname
183                     + "'.",
184                 ex);
185 
186         } catch (InstantiationException ex) { // Bad constructor or error
187             throw new DefinitionsFactoryException(ex);
188 
189         } catch (IllegalAccessException ex) {
190             throw new DefinitionsFactoryException(ex);
191         }
192 
193     }
194 
195     /**
196      * Return String representation.
197      * Calls toString() on underlying factory.
198      * @return String representation.
199      */
200     public String toString() {
201         return getInternalFactory().toString();
202     }
203 
204     /**
205      * Create map of configuration attributes from configuration object.
206      * Mapping is done between old names and new names.
207      * @param config The DefinitionsFactoryConfig to use.
208      * @return Map Map of name/value pairs.
209      */
210     public static Map<String, Object> createConfigMap(DefinitionsFactoryConfig config) {
211         Map<String, Object> map = new HashMap<>(config.getAttributes());
212         // Add property attributes using old names
213         map.put(
214             DefinitionsFactoryConfig.DEFINITIONS_CONFIG_PARAMETER_NAME,
215             config.getDefinitionConfigFiles());
216 
217         map.put(
218             DefinitionsFactoryConfig.PARSER_VALIDATE_PARAMETER_NAME,
219             (config.getParserValidate() ? Boolean.TRUE.toString() : Boolean.FALSE.toString()));
220 
221         if (!"org.apache.struts.tiles.xmlDefinition.I18nFactorySet"
222             .equals(config.getFactoryClassname())) {
223 
224             map.put(
225                 DefinitionsFactoryConfig.FACTORY_CLASSNAME_PARAMETER_NAME,
226                 config.getFactoryClassname());
227         }
228 
229         return map;
230     }
231 
232 }