View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.config;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.Comparator;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.concurrent.ConcurrentHashMap;
29  import java.util.logging.Level;
30  import java.util.logging.Logger;
31  
32  import javax.el.ELResolver;
33  import javax.el.ExpressionFactory;
34  import javax.faces.context.ExternalContext;
35  import javax.faces.el.PropertyResolver;
36  import javax.faces.el.VariableResolver;
37  
38  import org.apache.commons.collections.Predicate;
39  import org.apache.myfaces.config.element.FaceletsProcessing;
40  import org.apache.myfaces.config.element.ManagedBean;
41  import org.apache.myfaces.config.element.NavigationRule;
42  import org.apache.myfaces.config.element.ResourceBundle;
43  
44  /**
45   * Holds all configuration information (from the faces-config xml files) that is needed later during runtime. The config
46   * information in this class is only available to the MyFaces core implementation classes (i.e. the myfaces source
47   * tree). See MyfacesConfig for config parameters that can be used for shared or component classes.
48   * 
49   * @author Manfred Geiler (latest modification by $Author: struberg $)
50   * @version $Revision: 1188895 $ $Date: 2011-10-25 15:31:51 -0500 (Tue, 25 Oct 2011) $
51   */
52  @SuppressWarnings("deprecation")
53  public class RuntimeConfig
54  {
55      //private static final Log log = LogFactory.getLog(RuntimeConfig.class);
56      private static final Logger log = Logger.getLogger(RuntimeConfig.class.getName());
57  
58      private static final String APPLICATION_MAP_PARAM_NAME = RuntimeConfig.class.getName();
59  
60      private final Collection<NavigationRule> _navigationRules = new ArrayList<NavigationRule>();
61      private final Map<String, ManagedBean> _managedBeans = new HashMap<String, ManagedBean>();
62      private boolean _navigationRulesChanged = false;
63      private final Map<String, ResourceBundle> _resourceBundles = new HashMap<String, ResourceBundle>();
64      private final Map<String, ManagedBean> _oldManagedBeans = new HashMap<String, ManagedBean>();
65      
66      private String _facesVersion;
67      
68      private List<ELResolver> facesConfigElResolvers;
69      private List<ELResolver> applicationElResolvers;
70  
71      private VariableResolver _variableResolver;
72      private PropertyResolver _propertyResolver;
73  
74      private ExpressionFactory _expressionFactory;
75  
76      private PropertyResolver _propertyResolverChainHead;
77  
78      private VariableResolver _variableResolverChainHead;
79      
80      private Comparator<ELResolver> _elResolverComparator;
81      
82      private Predicate _elResolverPredicate;
83  
84      private final Map<String, org.apache.myfaces.config.element.Converter> _converterClassNameToConfigurationMap =
85          new ConcurrentHashMap<String, org.apache.myfaces.config.element.Converter>();
86      
87      private NamedEventManager _namedEventManager;
88      
89      private final Map<String, FaceletsProcessing> _faceletsProcessingByFileExtension =
90          new HashMap<String, FaceletsProcessing>();
91  
92      public static RuntimeConfig getCurrentInstance(ExternalContext externalContext)
93      {
94          RuntimeConfig runtimeConfig = (RuntimeConfig) externalContext.getApplicationMap().get(
95                  APPLICATION_MAP_PARAM_NAME);
96          if (runtimeConfig == null)
97          {
98              runtimeConfig = new RuntimeConfig();
99              externalContext.getApplicationMap().put(APPLICATION_MAP_PARAM_NAME, runtimeConfig);
100         }
101         return runtimeConfig;
102     }
103 
104     public void purge()
105     {
106         _navigationRules.clear();
107         _oldManagedBeans.clear();
108         _oldManagedBeans.putAll(_managedBeans);
109         _managedBeans.clear();
110         _navigationRulesChanged = false;
111         _converterClassNameToConfigurationMap.clear();
112     }
113 
114     /**
115      * Return the navigation rules that can be used by the NavigationHandler implementation.
116      * 
117      * @return a Collection of {@link org.apache.myfaces.config.element.NavigationRule NavigationRule}s
118      */
119     public Collection<NavigationRule> getNavigationRules()
120     {
121         return Collections.unmodifiableCollection(_navigationRules);
122     }
123 
124     public void addNavigationRule(NavigationRule navigationRule)
125     {
126         _navigationRules.add(navigationRule);
127 
128         _navigationRulesChanged = true;
129     }
130 
131     public boolean isNavigationRulesChanged()
132     {
133         return _navigationRulesChanged;
134     }
135 
136     public void setNavigationRulesChanged(boolean navigationRulesChanged)
137     {
138         _navigationRulesChanged = navigationRulesChanged;
139     }
140 
141     /**
142      * Return the managed bean info that can be used by the VariableResolver implementation.
143      * 
144      * @return a {@link org.apache.myfaces.config.element.ManagedBean ManagedBean}
145      */
146     public ManagedBean getManagedBean(String name)
147     {
148         return _managedBeans.get(name);
149     }
150 
151     public Map<String, ManagedBean> getManagedBeans()
152     {
153         return Collections.unmodifiableMap(_managedBeans);
154     }
155 
156     public void addManagedBean(String name, ManagedBean managedBean)
157     {
158         _managedBeans.put(name, managedBean);
159         if(_oldManagedBeans!=null)
160         {
161             _oldManagedBeans.remove(name);
162         }
163     }
164 
165     
166     public final void addConverterConfiguration(final String converterClassName,
167             final org.apache.myfaces.config.element.Converter configuration)
168     {
169         checkNull(converterClassName, "converterClassName");
170         checkEmpty(converterClassName, "converterClassName");
171         checkNull(configuration, "configuration");
172 
173         _converterClassNameToConfigurationMap.put(converterClassName, configuration);
174     }
175     
176     public org.apache.myfaces.config.element.Converter getConverterConfiguration(String converterClassName)
177     {
178         return (org.apache.myfaces.config.element.Converter)
179                 _converterClassNameToConfigurationMap.get(converterClassName);
180     }
181     
182     private void checkNull(final Object param, final String paramName)
183     {
184         if (param == null)
185         {
186             throw new NullPointerException(paramName + " can not be null.");
187         }
188     }
189 
190     private void checkEmpty(final String param, final String paramName)
191     {
192         if (param.length() == 0)
193         {
194             throw new NullPointerException("String " + paramName + " can not be empty.");
195         }
196     }
197 
198     /**
199      * Return the resourcebundle which was configured in faces config by var name
200      * 
201      * @param name
202      *            the name of the resource bundle (content of var)
203      * @return the resource bundle or null if not found
204      */
205     public ResourceBundle getResourceBundle(String name)
206     {
207         return _resourceBundles.get(name);
208     }
209 
210     /**
211      * @return the resourceBundles
212      */
213     public Map<String, ResourceBundle> getResourceBundles()
214     {
215         return _resourceBundles;
216     }
217 
218     public void addResourceBundle(ResourceBundle bundle)
219     {
220         if (bundle == null)
221         {
222             throw new IllegalArgumentException("bundle must not be null");
223         }
224         String var = bundle.getVar();
225         if (_resourceBundles.containsKey(var) && log.isLoggable(Level.WARNING))
226         {
227             log.warning("Another resource bundle for var '" + var + "' with base name '"
228                     + _resourceBundles.get(var).getBaseName() + "' is already registered. '"
229                     + _resourceBundles.get(var).getBaseName() + "' will be replaced with '" + bundle.getBaseName()
230                     + "'.");
231         }
232         _resourceBundles.put(var, bundle);
233     }
234 
235     public void addFacesConfigElResolver(ELResolver resolver)
236     {
237         if (facesConfigElResolvers == null)
238         {
239             facesConfigElResolvers = new ArrayList<ELResolver>();
240         }
241         facesConfigElResolvers.add(resolver);
242     }
243 
244     public List<ELResolver> getFacesConfigElResolvers()
245     {
246         return facesConfigElResolvers;
247     }
248 
249     public void addApplicationElResolver(ELResolver resolver)
250     {
251         if (applicationElResolvers == null)
252         {
253             applicationElResolvers = new ArrayList<ELResolver>();
254         }
255         applicationElResolvers.add(resolver);
256     }
257 
258     public List<ELResolver> getApplicationElResolvers()
259     {
260         return applicationElResolvers;
261     }
262 
263     public void setVariableResolver(VariableResolver variableResolver)
264     {
265         _variableResolver = variableResolver;
266     }
267 
268     public VariableResolver getVariableResolver()
269     {
270         return _variableResolver;
271     }
272 
273     public void setPropertyResolver(PropertyResolver propertyResolver)
274     {
275         _propertyResolver = propertyResolver;
276     }
277 
278     public PropertyResolver getPropertyResolver()
279     {
280         return _propertyResolver;
281     }
282 
283     public ExpressionFactory getExpressionFactory()
284     {
285         return _expressionFactory;
286     }
287 
288     public void setExpressionFactory(ExpressionFactory expressionFactory)
289     {
290         _expressionFactory = expressionFactory;
291     }
292 
293     public void setPropertyResolverChainHead(PropertyResolver resolver)
294     {
295         _propertyResolverChainHead = resolver;
296     }
297 
298     public PropertyResolver getPropertyResolverChainHead()
299     {
300         return _propertyResolverChainHead;
301     }
302 
303     public void setVariableResolverChainHead(VariableResolver resolver)
304     {
305         _variableResolverChainHead = resolver;
306     }
307 
308     public VariableResolver getVariableResolverChainHead()
309     {
310         return _variableResolverChainHead;
311     }
312 
313     public Map<String, ManagedBean> getManagedBeansNotReaddedAfterPurge()
314     {
315         return _oldManagedBeans;
316     }
317 
318     public void resetManagedBeansNotReaddedAfterPurge()
319     {
320         _oldManagedBeans.clear();
321     }
322     
323     public String getFacesVersion ()
324     {
325         return _facesVersion;
326     }
327     
328     void setFacesVersion (String facesVersion)
329     {
330         _facesVersion = facesVersion;
331     }
332 
333     public NamedEventManager getNamedEventManager()
334     {
335         return _namedEventManager;
336     }
337 
338     public void setNamedEventManager(NamedEventManager namedEventManager)
339     {
340         this._namedEventManager = namedEventManager;
341     }
342 
343     public Comparator<ELResolver> getELResolverComparator()
344     {
345         return _elResolverComparator;
346     }
347     
348     public void setELResolverComparator(Comparator<ELResolver> elResolverComparator)
349     {
350         _elResolverComparator = elResolverComparator;
351     }
352     
353     public Predicate getELResolverPredicate()
354     {
355         return _elResolverPredicate;
356     }
357     
358     public void setELResolverPredicate(Predicate elResolverPredicate)
359     {
360         _elResolverPredicate = elResolverPredicate;
361     }
362     
363     public void addFaceletProcessingConfiguration(String fileExtension, FaceletsProcessing configuration)
364     {
365         checkNull(fileExtension, "fileExtension");
366         checkEmpty(fileExtension, "fileExtension");
367         checkNull(configuration, "configuration");
368 
369         this._faceletsProcessingByFileExtension.put(fileExtension, configuration);
370     }
371     
372     public FaceletsProcessing getFaceletProcessingConfiguration(String fileExtensions)
373     {
374         return _faceletsProcessingByFileExtension.get(fileExtensions);
375     }
376     
377     public Collection<FaceletsProcessing> getFaceletProcessingConfigurations()
378     {
379         return _faceletsProcessingByFileExtension.values();
380     }
381 }