AbstractCachingViewResolver.java
上传用户:jiancairen
上传日期:2007-08-27
资源大小:26458k
文件大小:5k
源码类别:

Java编程

开发平台:

Java

  1. /*
  2.  * Copyright 2002-2004 the original author or authors.
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */ 
  16. package org.springframework.web.servlet.view;
  17. import java.util.Collections;
  18. import java.util.HashMap;
  19. import java.util.Locale;
  20. import java.util.Map;
  21. import org.springframework.context.ApplicationContextAware;
  22. import org.springframework.web.context.support.WebApplicationObjectSupport;
  23. import org.springframework.web.servlet.View;
  24. import org.springframework.web.servlet.ViewResolver;
  25. /**
  26.  * Convenient superclass for view resolvers.
  27.  * Caches views once resolved: This means that view resolution won't be a
  28.  * performance problem, no matter how costly initial view retrieval is.
  29.  *
  30.  * <p>View retrieval is deferred to subclasses via the <code>loadView</code>
  31.  * template method.
  32.  *
  33.  * @author Rod Johnson
  34.  * @author Juergen Hoeller
  35.  * @see #loadView
  36.  */
  37. public abstract class AbstractCachingViewResolver extends WebApplicationObjectSupport implements ViewResolver {
  38. /** View name --> View instance */
  39. private final Map viewMap = Collections.synchronizedMap(new HashMap());
  40. /** Whether we should cache views, once resolved */
  41. private boolean cache = true;
  42. /**
  43.  * Enable respectively disable caching. Disable this only for debugging
  44.  * and development. Default is for caching to be enabled.
  45.  * <p><b>Warning: Disabling caching can severely impact performance.</b>
  46.  * Tests indicate that turning caching off reduces performance by at least 20%.
  47.  * Increased object churn probably eventually makes the problem even worse.
  48.  */
  49. public void setCache(boolean cache) {
  50. this.cache = cache;
  51. }
  52. /**
  53.  * Return if caching is enabled.
  54.  */
  55. public boolean isCache() {
  56. return cache;
  57. }
  58. public View resolveViewName(String viewName, Locale locale) throws Exception {
  59. if (!this.cache) {
  60. logger.warn("View caching is SWITCHED OFF -- DEVELOPMENT SETTING ONLY: This can severely impair performance");
  61. return loadAndConfigureView(viewName, locale);
  62. }
  63. else {
  64. String cacheKey = getCacheKey(viewName, locale);
  65. // no synchronization, as we can live with occasional double caching
  66. View view = (View) this.viewMap.get(cacheKey);
  67. if (view == null) {
  68. // ask the subclass to load the View
  69. view = loadAndConfigureView(viewName, locale);
  70. this.viewMap.put(cacheKey, view);
  71. if (logger.isInfoEnabled()) {
  72. logger.info("Cached view '" + cacheKey + "'");
  73. }
  74. }
  75. return view;
  76. }
  77. }
  78. /**
  79.  * Provides functionality to clear the cache for a certain view.
  80.  * This can be handy in case developer are able to modify views
  81.  * (e.g. Velocity templates) at runtime after which you'd need to
  82.  * clear the cache for the specified view.
  83.  */
  84. public void removeFromCache(String viewName, Locale locale) {
  85. if (!this.cache) {
  86. logger.warn("View caching is SWITCHED OFF -- removal not necessary");
  87. }
  88. else {
  89. String cacheKey = getCacheKey(viewName, locale);
  90. if (viewMap.remove(cacheKey) == null) {
  91. // some debug output might be useful
  92. if (logger.isDebugEnabled()) {
  93. logger.debug("No cached instance for view " + cacheKey + " was found");
  94. }
  95. else {
  96. if (logger.isDebugEnabled()) {
  97. logger.debug("Cach for view " + cacheKey + " has been cleared");
  98. }
  99. }
  100. }
  101. }
  102. /**
  103.  * Load and configure the given View. Only invoked once per View.
  104.  * Delegates to the loadView template method for actual loading.
  105.  * <p>Sets the ApplicationContext on the View if necessary.
  106.  * @see #loadView
  107.  */
  108. private View loadAndConfigureView(String viewName, Locale locale) throws Exception {
  109. View view = loadView(viewName, locale);
  110. if (view instanceof ApplicationContextAware) {
  111. ((ApplicationContextAware) view).setApplicationContext(getApplicationContext());
  112. }
  113. return view;
  114. }
  115. /**
  116.  * Return the cache key for the given viewName and the given locale.
  117.  * Needs to regard the locale in general, as a different locale can lead to a
  118.  * different view! Can be overridden in subclasses.
  119.  */
  120. protected String getCacheKey(String viewName, Locale locale) {
  121. return viewName + "_" + locale;
  122. }
  123. /**
  124.  * Subclasses must implement this method. There need be no concern
  125.  * for efficiency, as this class will cache views.
  126.  * <p>Not all subclasses may support internationalization:
  127.  * A subclass that doesn't can simply ignore the locale parameter.
  128.  * <p>This method is not supposed to fully initialize the view (for example,
  129.  * ApplicationContextAware methods haven't been called yet). Clients should only
  130.  * be using resolveViewName, which does fully initialize the view objects found.
  131.  * @param viewName the name of the view to retrieve
  132.  * @param locale the Locale to retrieve the view for
  133.  * @return the View instance, or null if not found
  134.  * (optional, to allow for ViewResolver chaining)
  135.  * @throws Exception if the view couldn't be resolved
  136.  * @see #resolveViewName
  137.  */
  138. protected abstract View loadView(String viewName, Locale locale) throws Exception;
  139. }