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 org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.myfaces.application.ApplicationImpl;
24  import org.apache.myfaces.config.element.ManagedBean;
25  import org.apache.myfaces.config.element.NavigationRule;
26  
27  import javax.faces.context.ExternalContext;
28  import java.util.*;
29  
30  /**
31   * Holds all configuration information (from the faces-config xml files) that
32   * is needed later during runtime.
33   * The config information in this class is only available to the MyFaces core
34   * implementation classes (i.e. the myfaces source tree). See MyfacesConfig
35   * for config parameters that can be used for shared or component classes.
36   *
37   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
38   * @version $Revision: 1000673 $ $Date: 2010-09-23 19:40:36 -0500 (Thu, 23 Sep 2010) $
39   */
40  public class RuntimeConfig
41  {
42      private static final Log log = LogFactory.getLog(RuntimeConfig.class);
43      
44      private static final String APPLICATION_MAP_PARAM_NAME = RuntimeConfig.class.getName();
45  
46      private Collection _navigationRules = new ArrayList();
47      private Map _managedBeans = new HashMap();
48      private Map _oldManagedBeans = new HashMap();
49      private Map _managedBeansPerLocation = new HashMap();
50      private boolean _navigationRulesChanged=false;
51      private Map _converterClassNameToConfigurationMap = Collections.synchronizedMap(new HashMap());
52  
53  
54      public static RuntimeConfig getCurrentInstance(ExternalContext externalContext)
55      {
56          RuntimeConfig runtimeConfig
57                  = (RuntimeConfig)externalContext.getApplicationMap().get(APPLICATION_MAP_PARAM_NAME);
58          if (runtimeConfig == null)
59          {
60              runtimeConfig = new RuntimeConfig();
61              externalContext.getApplicationMap().put(APPLICATION_MAP_PARAM_NAME, runtimeConfig);
62          }
63          return runtimeConfig;
64      }
65  
66      public void purge(){
67          _navigationRules = new ArrayList();
68          _oldManagedBeans = _managedBeans;
69          _managedBeans = new HashMap();
70          _managedBeansPerLocation = new HashMap();
71          _navigationRulesChanged = false;
72          _converterClassNameToConfigurationMap = Collections.synchronizedMap(new HashMap());
73      }
74  
75      /**
76       * Return the navigation rules that can be used by the NavigationHandler implementation.
77       * @return a Collection of {@link org.apache.myfaces.config.element.NavigationRule NavigationRule}s
78       */
79      public Collection getNavigationRules()
80      {
81          return _navigationRules == null ?
82                  null : Collections.unmodifiableCollection(_navigationRules);
83      }
84  
85      public Map getManagedBeans()
86      {
87          return _managedBeans == null ?
88                  null : Collections.unmodifiableMap(_managedBeans);
89      }
90      
91      public void addNavigationRule(NavigationRule navigationRule)
92      {
93          _navigationRules.add(navigationRule);
94  
95          _navigationRulesChanged = true;
96      }
97  
98      public boolean isNavigationRulesChanged()
99      {
100         return _navigationRulesChanged;
101     }
102 
103     public void setNavigationRulesChanged(boolean navigationRulesChanged)
104     {
105         _navigationRulesChanged = navigationRulesChanged;
106     }
107 
108     /**
109      * Return the managed bean info that can be used by the VariableResolver implementation.
110      * @return a {@link org.apache.myfaces.config.element.ManagedBean ManagedBean}
111      */
112     public ManagedBean getManagedBean(String name)
113     {
114         return (ManagedBean)_managedBeans.get(name);
115     }
116 
117     /**
118      * Return the managed bean info that can be used by the VariableResolver implementation.
119      * Here, the full list of managed-beans is returned - if a managed bean
120      * was registered more than once. The getConfigLocation()
121      * method of the managed-bean will indicate in which config file
122      * it was registered originally.
123      *
124      * @return a {@link org.apache.myfaces.config.element.ManagedBean ManagedBean}
125      */
126     public List getManagedBeans(String name)
127     {
128         List li = (List) _managedBeansPerLocation.get(name);
129         return li==null?null:Collections.unmodifiableList(li);
130     }
131 
132     public void addManagedBean(String name, ManagedBean managedBean)
133     {
134         _managedBeans.put(name, managedBean);
135         if(_oldManagedBeans!=null)
136             _oldManagedBeans.remove(name);
137 
138         List li = (List) _managedBeansPerLocation.get(name);
139 
140         if(li == null) {
141             li = new ArrayList();
142             _managedBeansPerLocation.put(name,li);
143         }
144 
145         li.add(managedBean);
146     }
147 
148     public Map getManagedBeansNotReaddedAfterPurge() {
149         return _oldManagedBeans;
150     }
151 
152     public void resetManagedBeansNotReaddedAfterPurge() {
153         _oldManagedBeans = null;
154     }
155     
156     public void addConverterConfiguration(String converterClassName,
157             org.apache.myfaces.config.impl.digester.elements.Converter configuration)
158     {
159         if ((converterClassName == null) || (converterClassName.length() == 0))
160         {
161         log.error("addConverterConfiguration: converterClassName = null is not allowed");
162             throw new NullPointerException("addConverterConfiguration: converterClassName = null is not allowed");
163         }
164         if ((configuration == null))
165         {
166         log.error("addConverterConfiguration: configuration = null is not allowed");
167             throw new NullPointerException("addConverterConfiguration: configuration = null is not allowed");
168         }
169         
170         _converterClassNameToConfigurationMap.put(converterClassName, configuration);
171     }
172     
173     public org.apache.myfaces.config.impl.digester.elements.Converter getConverterConfiguration(String converterClassName)
174     {
175         return (org.apache.myfaces.config.impl.digester.elements.Converter)_converterClassNameToConfigurationMap.get(converterClassName);
176     }
177 }