KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > tiles > definition > ComponentDefinitionsFactoryWrapper


1 /*
2  * $Id: ComponentDefinitionsFactoryWrapper.java 54929 2004-10-16 16:38:42Z germuska $
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.definition;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.ServletRequest JavaDoc;
26
27 import org.apache.struts.tiles.ComponentDefinition;
28 import org.apache.struts.tiles.ComponentDefinitionsFactory;
29 import org.apache.struts.tiles.DefinitionsFactory;
30 import org.apache.struts.tiles.DefinitionsFactoryConfig;
31 import org.apache.struts.tiles.DefinitionsFactoryException;
32 import org.apache.struts.tiles.NoSuchDefinitionException;
33 import org.apache.struts.util.RequestUtils;
34
35 /**
36  * Wrapper from new definition factory interface to old interface.
37  * This class provides mapping from the old interface's life cycle to the new life cycle.
38  * @since 20020708
39  */

40 public class ComponentDefinitionsFactoryWrapper implements DefinitionsFactory {
41
42     /**
43      * The underlying factory.
44      */

45     private ComponentDefinitionsFactory factory = null;
46
47     /**
48      * Factory configuration,
49      */

50     private DefinitionsFactoryConfig config = null;
51
52     /**
53      * Constructor.
54      * Create new wrapper for specified factory.
55      * @param factory The factory to create a wrapper for.
56      */

57     public ComponentDefinitionsFactoryWrapper(ComponentDefinitionsFactory factory) {
58         this.factory = factory;
59     }
60
61     /**
62      * Constructor.
63      * Create new wrapper.
64      * The config object passed to init method should reference a factory implementing
65      * {@link ComponentDefinitionsFactory}.
66      */

67     public ComponentDefinitionsFactoryWrapper() {
68         super();
69     }
70
71     /**
72      * Get requested definition.
73      * @param name Name of the definition.
74      * @param request The request we are processing.
75      * @param servletContext Our servlet context.
76      * @return ComponentDefition
77      */

78     public ComponentDefinition getDefinition(
79         String JavaDoc name,
80         ServletRequest JavaDoc request,
81         ServletContext JavaDoc servletContext)
82         throws NoSuchDefinitionException, DefinitionsFactoryException {
83
84         return factory.getDefinition(name, request, servletContext);
85     }
86
87     /**
88      * Call underlying factory init method.
89      * @param config DefinitionsFactoryConfig.
90      * @param servletContext Our servlet context.
91      */

92     public void init(DefinitionsFactoryConfig config, ServletContext JavaDoc servletContext)
93         throws DefinitionsFactoryException {
94
95         this.config = config;
96
97         // create factory and initialize it
98
if (factory == null) {
99             factory = createFactoryInstance(config.getFactoryClassname());
100         }
101
102         factory.initFactory(servletContext, createConfigMap(config));
103     }
104
105     /**
106      * Do nothing because old life cycle has no equivalent.
107      */

108     public void destroy() {
109         factory = null;
110     }
111
112     /**
113      * Set underlying factory configuration.
114      * @param config DefinitionsFactoryConfig to use.
115      * @param servletContext Our servlet context.
116      *
117      */

118     public void setConfig(
119         DefinitionsFactoryConfig config,
120         ServletContext JavaDoc servletContext)
121         throws DefinitionsFactoryException {
122
123         ComponentDefinitionsFactory newFactory =
124             createFactoryInstance(config.getFactoryClassname());
125
126         newFactory.initFactory(servletContext, createConfigMap(config));
127         factory = newFactory;
128     }
129
130     /**
131      * Get underlying factory configuration.
132      * @return DefinitionsFactoryConfig.
133      */

134     public DefinitionsFactoryConfig getConfig() {
135         return config;
136     }
137
138     /**
139      * Get internal factory.
140      * @return The internal ComponentDefitionsFactory.
141      */

142     public ComponentDefinitionsFactory getInternalFactory() {
143         return factory;
144     }
145
146     /**
147      * Create Definition factory from provided classname which must implement {@link ComponentDefinitionsFactory}.
148      * Factory class must extend {@link DefinitionsFactory}.
149      * @param classname Class name of the factory to create.
150      * @return newly created factory.
151      * @throws DefinitionsFactoryException If an error occur while initializing factory
152      */

153     protected ComponentDefinitionsFactory createFactoryInstance(String JavaDoc classname)
154         throws DefinitionsFactoryException {
155
156         try {
157             Class JavaDoc factoryClass = RequestUtils.applicationClass(classname);
158             Object JavaDoc factory = factoryClass.newInstance();
159             return (ComponentDefinitionsFactory) factory;
160
161         } catch (ClassCastException JavaDoc ex) { // Bad classname
162
throw new DefinitionsFactoryException(
163                 "Error - createDefinitionsFactory : Factory class '"
164                     + classname
165                     + " must implement 'DefinitionsFactory'.",
166                 ex);
167
168         } catch (ClassNotFoundException JavaDoc ex) { // Bad classname
169
throw new DefinitionsFactoryException(
170                 "Error - createDefinitionsFactory : Bad class name '"
171                     + classname
172                     + "'.",
173                 ex);
174
175         } catch (InstantiationException JavaDoc ex) { // Bad constructor or error
176
throw new DefinitionsFactoryException(ex);
177
178         } catch (IllegalAccessException JavaDoc ex) {
179             throw new DefinitionsFactoryException(ex);
180         }
181
182     }
183
184     /**
185      * Return String representation.
186      * Calls toString() on underlying factory.
187      * @return String representation.
188      */

189     public String JavaDoc toString() {
190         return getInternalFactory().toString();
191     }
192
193     /**
194      * Create map of configuration attributes from configuration object.
195      * Mapping is done between old names and new names.
196      * @param config The DefinitionsFactoryConfig to use.
197      * @return Map Map of name/value pairs.
198      */

199     public static Map JavaDoc createConfigMap(DefinitionsFactoryConfig config) {
200         Map JavaDoc map = new HashMap JavaDoc(config.getAttributes());
201         // Add property attributes using old names
202
map.put(
203             DefinitionsFactoryConfig.DEFINITIONS_CONFIG_PARAMETER_NAME,
204             config.getDefinitionConfigFiles());
205
206         map.put(
207             DefinitionsFactoryConfig.PARSER_VALIDATE_PARAMETER_NAME,
208             (config.getParserValidate() ? Boolean.TRUE.toString() : Boolean.FALSE.toString()));
209
210         if (!"org.apache.struts.tiles.xmlDefinition.I18nFactorySet"
211             .equals(config.getFactoryClassname())) {
212
213             map.put(
214                 DefinitionsFactoryConfig.FACTORY_CLASSNAME_PARAMETER_NAME,
215                 config.getFactoryClassname());
216         }
217
218         return map;
219     }
220
221 }
222
Popular Tags