001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.myfaces.config;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Collections;
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: import javax.el.CompositeELResolver;
025: import javax.el.ELResolver;
026: import javax.el.ExpressionFactory;
027: import javax.faces.context.ExternalContext;
028: import javax.faces.el.PropertyResolver;
029: import javax.faces.el.VariableResolver;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.apache.myfaces.config.element.ManagedBean;
034: import org.apache.myfaces.config.element.NavigationRule;
035: import org.apache.myfaces.config.impl.digester.elements.ResourceBundle;
036:
037: /**
038: * Holds all configuration information (from the faces-config xml files) that is needed later during runtime. The config
039: * information in this class is only available to the MyFaces core implementation classes (i.e. the myfaces source
040: * tree). See MyfacesConfig for config parameters that can be used for shared or component classes.
041: *
042: * @author Manfred Geiler (latest modification by $Author: mbr $)
043: * @version $Revision: 527076 $ $Date: 2007-04-10 12:01:32 +0200 (Di, 10 Apr 2007) $
044: */
045: @SuppressWarnings("deprecation")
046: public class RuntimeConfig {
047: private static final Log log = LogFactory
048: .getLog(RuntimeConfig.class);
049:
050: private static final String APPLICATION_MAP_PARAM_NAME = RuntimeConfig.class
051: .getName();
052:
053: private final Collection<NavigationRule> _navigationRules = new ArrayList<NavigationRule>();
054: private final Map<String, ManagedBean> _managedBeans = new HashMap<String, ManagedBean>();
055: private boolean _navigationRulesChanged = false;
056: private final Map<String, ResourceBundle> _resourceBundles = new HashMap<String, ResourceBundle>();
057:
058: private CompositeELResolver facesConfigElResolvers;
059: private CompositeELResolver applicationElResolvers;
060:
061: private VariableResolver _variableResolver;
062: private PropertyResolver _propertyResolver;
063:
064: private ExpressionFactory _expressionFactory;
065:
066: private PropertyResolver _propertyResolverChainHead;
067:
068: private VariableResolver _variableResolverChainHead;
069:
070: public static RuntimeConfig getCurrentInstance(
071: ExternalContext externalContext) {
072: RuntimeConfig runtimeConfig = (RuntimeConfig) externalContext
073: .getApplicationMap().get(APPLICATION_MAP_PARAM_NAME);
074: if (runtimeConfig == null) {
075: runtimeConfig = new RuntimeConfig();
076: externalContext.getApplicationMap().put(
077: APPLICATION_MAP_PARAM_NAME, runtimeConfig);
078: }
079: return runtimeConfig;
080: }
081:
082: /**
083: * Return the navigation rules that can be used by the NavigationHandler implementation.
084: *
085: * @return a Collection of {@link org.apache.myfaces.config.element.NavigationRule NavigationRule}s
086: */
087: public Collection<NavigationRule> getNavigationRules() {
088: return Collections.unmodifiableCollection(_navigationRules);
089: }
090:
091: public void addNavigationRule(NavigationRule navigationRule) {
092: _navigationRules.add(navigationRule);
093:
094: _navigationRulesChanged = true;
095: }
096:
097: public boolean isNavigationRulesChanged() {
098: return _navigationRulesChanged;
099: }
100:
101: public void setNavigationRulesChanged(boolean navigationRulesChanged) {
102: _navigationRulesChanged = navigationRulesChanged;
103: }
104:
105: /**
106: * Return the managed bean info that can be used by the VariableResolver implementation.
107: *
108: * @return a {@link org.apache.myfaces.config.element.ManagedBean ManagedBean}
109: */
110: public ManagedBean getManagedBean(String name) {
111: return _managedBeans.get(name);
112: }
113:
114: public Map<String, ManagedBean> getManagedBeans() {
115: return Collections.unmodifiableMap(_managedBeans);
116: }
117:
118: public void addManagedBean(String name, ManagedBean managedBean) {
119: _managedBeans.put(name, managedBean);
120: }
121:
122: /**
123: * Return the resourcebundle which was configured in faces config by var name
124: *
125: * @param name
126: * the name of the resource bundle (content of var)
127: * @return the resource bundle or null if not found
128: */
129: public ResourceBundle getResourceBundle(String name) {
130: return _resourceBundles.get(name);
131: }
132:
133: /**
134: * @return the resourceBundles
135: */
136: public Map<String, ResourceBundle> getResourceBundles() {
137: return _resourceBundles;
138: }
139:
140: public void addResourceBundle(ResourceBundle bundle) {
141: if (bundle == null) {
142: throw new IllegalArgumentException(
143: "bundle must not be null");
144: }
145: String var = bundle.getVar();
146: if (_resourceBundles.containsKey(var) && log.isWarnEnabled()) {
147: log.warn("Another resource bundle for var '" + var
148: + "' with base name '"
149: + _resourceBundles.get(var).getBaseName()
150: + "' is already registered. '"
151: + _resourceBundles.get(var).getBaseName()
152: + "' will be replaced with '"
153: + bundle.getBaseName() + "'.");
154: }
155: _resourceBundles.put(var, bundle);
156: }
157:
158: public void addFacesConfigElResolver(ELResolver resolver) {
159: if (facesConfigElResolvers == null) {
160: facesConfigElResolvers = new org.apache.myfaces.el.CompositeELResolver();
161: }
162: facesConfigElResolvers.add(resolver);
163: }
164:
165: public ELResolver getFacesConfigElResolvers() {
166: return facesConfigElResolvers;
167: }
168:
169: public void addApplicationElResolver(ELResolver resolver) {
170: if (applicationElResolvers == null) {
171: applicationElResolvers = new org.apache.myfaces.el.CompositeELResolver();
172: }
173: applicationElResolvers.add(resolver);
174: }
175:
176: public ELResolver getApplicationElResolvers() {
177: return applicationElResolvers;
178: }
179:
180: public void setVariableResolver(VariableResolver variableResolver) {
181: _variableResolver = variableResolver;
182: }
183:
184: public VariableResolver getVariableResolver() {
185: return _variableResolver;
186: }
187:
188: public void setPropertyResolver(PropertyResolver propertyResolver) {
189: _propertyResolver = propertyResolver;
190: }
191:
192: public PropertyResolver getPropertyResolver() {
193: return _propertyResolver;
194: }
195:
196: public ExpressionFactory getExpressionFactory() {
197: return _expressionFactory;
198: }
199:
200: public void setExpressionFactory(ExpressionFactory expressionFactory) {
201: _expressionFactory = expressionFactory;
202: }
203:
204: public void setPropertyResolverChainHead(PropertyResolver resolver) {
205: _propertyResolverChainHead = resolver;
206: }
207:
208: public PropertyResolver getPropertyResolverChainHead() {
209: return _propertyResolverChainHead;
210: }
211:
212: public void setVariableResolverChainHead(VariableResolver resolver) {
213: _variableResolverChainHead = resolver;
214: }
215:
216: public VariableResolver getVariableResolverChainHead() {
217: return _variableResolverChainHead;
218: }
219: }
|