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.myfaces.config.element.ManagedBean;
39  import org.apache.myfaces.config.element.NavigationRule;
40  import org.apache.myfaces.config.element.ResourceBundle;
41  
42  /**
43   * Holds all configuration information (from the faces-config xml files) that is needed later during runtime. The config
44   * information in this class is only available to the MyFaces core implementation classes (i.e. the myfaces source
45   * tree). See MyfacesConfig for config parameters that can be used for shared or component classes.
46   * 
47   * @author Manfred Geiler (latest modification by $Author: bommel $)
48   * @version $Revision: 1187700 $ $Date: 2011-10-22 07:19:37 -0500 (Sat, 22 Oct 2011) $
49   */
50  @SuppressWarnings("deprecation")
51  public class RuntimeConfig
52  {
53      //private static final Log log = LogFactory.getLog(RuntimeConfig.class);
54      private static final Logger log = Logger.getLogger(RuntimeConfig.class.getName());
55  
56      private static final String APPLICATION_MAP_PARAM_NAME = RuntimeConfig.class.getName();
57  
58      private final Collection<NavigationRule> _navigationRules = new ArrayList<NavigationRule>();
59      private final Map<String, ManagedBean> _managedBeans = new HashMap<String, ManagedBean>();
60      private boolean _navigationRulesChanged = false;
61      private final Map<String, ResourceBundle> _resourceBundles = new HashMap<String, ResourceBundle>();
62      private final Map<String, ManagedBean> _oldManagedBeans = new HashMap<String, ManagedBean>();
63      
64      private String _facesVersion;
65      
66      private List<ELResolver> facesConfigElResolvers;
67      private List<ELResolver> applicationElResolvers;
68  
69      private VariableResolver _variableResolver;
70      private PropertyResolver _propertyResolver;
71  
72      private ExpressionFactory _expressionFactory;
73  
74      private PropertyResolver _propertyResolverChainHead;
75  
76      private VariableResolver _variableResolverChainHead;
77      
78      private Comparator<ELResolver> _elResolverComparator;
79  
80      private final Map<String, org.apache.myfaces.config.element.Converter> _converterClassNameToConfigurationMap =
81          new ConcurrentHashMap<String, org.apache.myfaces.config.element.Converter>();
82      
83      private NamedEventManager _namedEventManager;
84  
85      public static RuntimeConfig getCurrentInstance(ExternalContext externalContext)
86      {
87          RuntimeConfig runtimeConfig = (RuntimeConfig) externalContext.getApplicationMap().get(
88                  APPLICATION_MAP_PARAM_NAME);
89          if (runtimeConfig == null)
90          {
91              runtimeConfig = new RuntimeConfig();
92              externalContext.getApplicationMap().put(APPLICATION_MAP_PARAM_NAME, runtimeConfig);
93          }
94          return runtimeConfig;
95      }
96  
97      public void purge()
98      {
99          _navigationRules.clear();
100         _oldManagedBeans.clear();
101         _oldManagedBeans.putAll(_managedBeans);
102         _managedBeans.clear();
103         _navigationRulesChanged = false;
104         _converterClassNameToConfigurationMap.clear();
105     }
106 
107     /**
108      * Return the navigation rules that can be used by the NavigationHandler implementation.
109      * 
110      * @return a Collection of {@link org.apache.myfaces.config.element.NavigationRule NavigationRule}s
111      */
112     public Collection<NavigationRule> getNavigationRules()
113     {
114         return Collections.unmodifiableCollection(_navigationRules);
115     }
116 
117     public void addNavigationRule(NavigationRule navigationRule)
118     {
119         _navigationRules.add(navigationRule);
120 
121         _navigationRulesChanged = true;
122     }
123 
124     public boolean isNavigationRulesChanged()
125     {
126         return _navigationRulesChanged;
127     }
128 
129     public void setNavigationRulesChanged(boolean navigationRulesChanged)
130     {
131         _navigationRulesChanged = navigationRulesChanged;
132     }
133 
134     /**
135      * Return the managed bean info that can be used by the VariableResolver implementation.
136      * 
137      * @return a {@link org.apache.myfaces.config.element.ManagedBean ManagedBean}
138      */
139     public ManagedBean getManagedBean(String name)
140     {
141         return _managedBeans.get(name);
142     }
143 
144     public Map<String, ManagedBean> getManagedBeans()
145     {
146         return Collections.unmodifiableMap(_managedBeans);
147     }
148 
149     public void addManagedBean(String name, ManagedBean managedBean)
150     {
151         _managedBeans.put(name, managedBean);
152         if(_oldManagedBeans!=null)
153             _oldManagedBeans.remove(name);
154     }
155 
156     
157     public final void addConverterConfiguration(final String converterClassName,
158             final org.apache.myfaces.config.element.Converter configuration)
159     {
160         checkNull(converterClassName, "converterClassName");
161         checkEmpty(converterClassName, "converterClassName");
162         checkNull(configuration, "configuration");
163 
164         _converterClassNameToConfigurationMap.put(converterClassName, configuration);
165     }
166     
167     public org.apache.myfaces.config.element.Converter getConverterConfiguration(String converterClassName)
168     {
169         return (org.apache.myfaces.config.element.Converter)_converterClassNameToConfigurationMap.get(converterClassName);
170     }
171     
172     private void checkNull(final Object param, final String paramName)
173     {
174         if (param == null)
175         {
176             throw new NullPointerException(paramName + " can not be null.");
177         }
178     }
179 
180     private void checkEmpty(final String param, final String paramName)
181     {
182         if (param.length() == 0)
183         {
184             throw new NullPointerException("String " + paramName + " can not be empty.");
185         }
186     }
187 
188     /**
189      * Return the resourcebundle which was configured in faces config by var name
190      * 
191      * @param name
192      *            the name of the resource bundle (content of var)
193      * @return the resource bundle or null if not found
194      */
195     public ResourceBundle getResourceBundle(String name)
196     {
197         return _resourceBundles.get(name);
198     }
199 
200     /**
201      * @return the resourceBundles
202      */
203     public Map<String, ResourceBundle> getResourceBundles()
204     {
205         return _resourceBundles;
206     }
207 
208     public void addResourceBundle(ResourceBundle bundle)
209     {
210         if (bundle == null)
211         {
212             throw new IllegalArgumentException("bundle must not be null");
213         }
214         String var = bundle.getVar();
215         if (_resourceBundles.containsKey(var) && log.isLoggable(Level.WARNING))
216         {
217             log.warning("Another resource bundle for var '" + var + "' with base name '"
218                     + _resourceBundles.get(var).getBaseName() + "' is already registered. '"
219                     + _resourceBundles.get(var).getBaseName() + "' will be replaced with '" + bundle.getBaseName()
220                     + "'.");
221         }
222         _resourceBundles.put(var, bundle);
223     }
224 
225     public void addFacesConfigElResolver(ELResolver resolver)
226     {
227         if (facesConfigElResolvers == null)
228         {
229             facesConfigElResolvers = new ArrayList<ELResolver>();
230         }
231         facesConfigElResolvers.add(resolver);
232     }
233 
234     public List<ELResolver> getFacesConfigElResolvers()
235     {
236         return facesConfigElResolvers;
237     }
238 
239     public void addApplicationElResolver(ELResolver resolver)
240     {
241         if (applicationElResolvers == null)
242         {
243             applicationElResolvers = new ArrayList<ELResolver>();
244         }
245         applicationElResolvers.add(resolver);
246     }
247 
248     public List<ELResolver> getApplicationElResolvers()
249     {
250         return applicationElResolvers;
251     }
252 
253     public void setVariableResolver(VariableResolver variableResolver)
254     {
255         _variableResolver = variableResolver;
256     }
257 
258     public VariableResolver getVariableResolver()
259     {
260         return _variableResolver;
261     }
262 
263     public void setPropertyResolver(PropertyResolver propertyResolver)
264     {
265         _propertyResolver = propertyResolver;
266     }
267 
268     public PropertyResolver getPropertyResolver()
269     {
270         return _propertyResolver;
271     }
272 
273     public ExpressionFactory getExpressionFactory()
274     {
275         return _expressionFactory;
276     }
277 
278     public void setExpressionFactory(ExpressionFactory expressionFactory)
279     {
280         _expressionFactory = expressionFactory;
281     }
282 
283     public void setPropertyResolverChainHead(PropertyResolver resolver)
284     {
285         _propertyResolverChainHead = resolver;
286     }
287 
288     public PropertyResolver getPropertyResolverChainHead()
289     {
290         return _propertyResolverChainHead;
291     }
292 
293     public void setVariableResolverChainHead(VariableResolver resolver)
294     {
295         _variableResolverChainHead = resolver;
296     }
297 
298     public VariableResolver getVariableResolverChainHead()
299     {
300         return _variableResolverChainHead;
301     }
302 
303     public Map<String, ManagedBean> getManagedBeansNotReaddedAfterPurge()
304     {
305         return _oldManagedBeans;
306     }
307 
308     public void resetManagedBeansNotReaddedAfterPurge()
309     {
310         _oldManagedBeans.clear();
311     }
312     
313     public String getFacesVersion ()
314     {
315         return _facesVersion;
316     }
317     
318     void setFacesVersion (String facesVersion)
319     {
320         _facesVersion = facesVersion;
321     }
322 
323     public NamedEventManager getNamedEventManager()
324     {
325         return _namedEventManager;
326     }
327 
328     public void setNamedEventManager(NamedEventManager namedEventManager)
329     {
330         this._namedEventManager = namedEventManager;
331     }
332 
333     public Comparator<ELResolver> getELResolverComparator()
334     {
335         return _elResolverComparator;
336     }
337     
338     public void setELResolverComparator(Comparator<ELResolver> elResolverComparator)
339     {
340         _elResolverComparator = elResolverComparator;
341     }
342 }