1 16 17 package org.springframework.web.servlet.view; 18 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.LinkedList ; 22 import java.util.List ; 23 import java.util.Locale ; 24 import java.util.Map ; 25 import java.util.MissingResourceException ; 26 import java.util.ResourceBundle ; 27 28 import org.springframework.beans.BeansException; 29 import org.springframework.beans.factory.BeanFactory; 30 import org.springframework.beans.factory.DisposableBean; 31 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 32 import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader; 33 import org.springframework.context.ConfigurableApplicationContext; 34 import org.springframework.core.Ordered; 35 import org.springframework.web.context.support.GenericWebApplicationContext; 36 import org.springframework.web.servlet.View; 37 38 64 public class ResourceBundleViewResolver extends AbstractCachingViewResolver implements Ordered, DisposableBean { 65 66 67 public final static String DEFAULT_BASENAME = "views"; 68 69 70 private int order = Integer.MAX_VALUE; 72 private String [] basenames = new String [] {DEFAULT_BASENAME}; 73 74 private ClassLoader bundleClassLoader = Thread.currentThread().getContextClassLoader(); 75 76 private String defaultParentView; 77 78 79 private final Map localeCache = new HashMap (); 80 81 82 private final Map bundleCache = new HashMap (); 83 84 85 public void setOrder(int order) { 86 this.order = order; 87 } 88 89 public int getOrder() { 90 return order; 91 } 92 93 104 public void setBasename(String basename) { 105 setBasenames(new String [] {basename}); 106 } 107 108 113 public void setBasenames(String [] basenames) { 114 this.basenames = basenames; 115 } 116 117 122 public void setBundleClassLoader(ClassLoader classLoader) { 123 this.bundleClassLoader = classLoader; 124 } 125 126 132 protected ClassLoader getBundleClassLoader() { 133 return bundleClassLoader; 134 } 135 136 150 public void setDefaultParentView(String defaultParentView) { 151 this.defaultParentView = defaultParentView; 152 } 153 154 155 protected View loadView(String viewName, Locale locale) throws Exception { 156 BeanFactory factory = initFactory(locale); 157 try { 158 return (View) factory.getBean(viewName, View.class); 159 } 160 catch (NoSuchBeanDefinitionException ex) { 161 return null; 163 } 164 } 165 166 172 protected synchronized BeanFactory initFactory(Locale locale) throws Exception { 173 if (isCache()) { 176 BeanFactory cachedFactory = (BeanFactory) this.localeCache.get(locale); 177 if (cachedFactory != null) { 178 return cachedFactory; 179 } 180 } 181 182 List bundles = new LinkedList (); 184 for (int i = 0; i < this.basenames.length; i++) { 185 ResourceBundle bundle = getBundle(this.basenames[i], locale); 186 bundles.add(bundle); 187 } 188 189 if (isCache()) { 192 BeanFactory cachedFactory = (BeanFactory) this.bundleCache.get(bundles); 193 if (cachedFactory != null) { 194 this.localeCache.put(locale, cachedFactory); 195 return cachedFactory; 196 } 197 } 198 199 GenericWebApplicationContext factory = new GenericWebApplicationContext(); 201 factory.setParent(getApplicationContext()); 202 factory.setServletContext(getServletContext()); 203 204 PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(factory); 206 reader.setDefaultParentBean(this.defaultParentView); 207 for (int i = 0; i < bundles.size(); i++) { 208 ResourceBundle bundle = (ResourceBundle ) bundles.get(i); 209 reader.registerBeanDefinitions(bundle); 210 } 211 212 factory.refresh(); 213 214 if (isCache()) { 216 this.localeCache.put(locale, factory); 217 this.bundleCache.put(bundles, factory); 218 } 219 220 return factory; 221 } 222 223 231 protected ResourceBundle getBundle(String basename, Locale locale) throws MissingResourceException { 232 return ResourceBundle.getBundle(basename, locale, getBundleClassLoader()); 233 } 234 235 236 239 public void destroy() throws BeansException { 240 for (Iterator it = this.bundleCache.values().iterator(); it.hasNext();) { 241 ConfigurableApplicationContext factory = (ConfigurableApplicationContext) it.next(); 242 factory.close(); 243 } 244 this.localeCache.clear(); 245 this.bundleCache.clear(); 246 } 247 248 } 249 | Popular Tags |