001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-util/tool-lib/src/java/org/sakaiproject/metaobj/utils/mvc/impl/beans/AddableResourceBundleViewResolver.java $
003: * $Id: AddableResourceBundleViewResolver.java 14230 2006-09-05 18:02:51Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.metaobj.utils.mvc.impl.beans;
021:
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Locale;
026: import java.util.Map;
027: import java.util.MissingResourceException;
028: import java.util.ResourceBundle;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.springframework.beans.BeansException;
033: import org.springframework.beans.factory.BeanFactory;
034: import org.springframework.beans.factory.config.ConfigurableBeanFactory;
035: import org.springframework.beans.factory.support.DefaultListableBeanFactory;
036: import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
037: import org.springframework.core.io.Resource;
038: import org.springframework.core.io.ResourceEditor;
039: import org.springframework.web.servlet.view.ResourceBundleViewResolver;
040:
041: public class AddableResourceBundleViewResolver extends
042: ResourceBundleViewResolver {
043: protected final transient Log logger = LogFactory
044: .getLog(getClass());
045:
046: private List baseNames;
047: private Map cachedFactories = new HashMap();
048: private String defaultParentView;
049:
050: public List getBaseNames() {
051: return baseNames;
052: }
053:
054: public void setBaseNames(List baseNames) {
055: this .baseNames = baseNames;
056: }
057:
058: /**
059: * Set the default parent for views defined in the ResourceBundle.
060: * This avoids repeated "yyy1.parent=xxx", "yyy2.parent=xxx" definitions
061: * in the bundle, especially if all defined views share the same parent.
062: * <p>The parent will typically define the view class and common attributes.
063: * Concrete views might simply consist of an URL definition then:
064: * a la "yyy1.url=/my.jsp", "yyy2.url=/your.jsp".
065: * <p>View definitions that define their own parent or carry their own
066: * class can still override this. Strictly speaking, the rule that a
067: * default parent setting does not apply to a bean definition that
068: * carries a class is there for backwards compatiblity reasons.
069: * It still matches the typical use case.
070: *
071: * @param defaultParentView the default parent view
072: */
073: public void setDefaultParentView(String defaultParentView) {
074: this .defaultParentView = defaultParentView;
075: }
076:
077: /**
078: * Initialize the BeanFactory from the ResourceBundle, for the given locale.
079: * Synchronized because of access by parallel threads.
080: */
081: protected synchronized BeanFactory initFactory(Locale locale)
082: throws MissingResourceException, BeansException {
083: BeanFactory parsedBundle = isCache() ? (BeanFactory) this .cachedFactories
084: .get(locale)
085: : null;
086: if (parsedBundle != null) {
087: return parsedBundle;
088: }
089:
090: DefaultListableBeanFactory factory = new DefaultListableBeanFactory(
091: getApplicationContext());
092: PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(
093: factory);
094: reader.setDefaultParentBean(this .defaultParentView);
095: for (Iterator i = baseNames.iterator(); i.hasNext();) {
096: ResourceBundle bundle = ResourceBundle.getBundle((String) i
097: .next(), locale, Thread.currentThread()
098: .getContextClassLoader());
099: reader.registerBeanDefinitions(bundle);
100: }
101: factory.registerCustomEditor(Resource.class,
102: new ResourceEditor(getApplicationContext()));
103:
104: if (isCache()) {
105: factory.preInstantiateSingletons();
106: this .cachedFactories.put(locale, factory);
107: }
108: return factory;
109: }
110:
111: public void destroy() throws BeansException {
112: for (Iterator it = this .cachedFactories.values().iterator(); it
113: .hasNext();) {
114: ConfigurableBeanFactory factory = (ConfigurableBeanFactory) it
115: .next();
116: factory.destroySingletons();
117: }
118: this.cachedFactories.clear();
119: }
120:
121: }
|