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 loadView template method.
  31. *
  32. * @author Rod Johnson
  33. * @author Juergen Hoeller
  34. * @see #loadView
  35. */
  36. public abstract class AbstractCachingViewResolver extends WebApplicationObjectSupport implements ViewResolver {
  37. /** View name --> View instance */
  38. private final Map viewMap = Collections.synchronizedMap(new HashMap());
  39. /** Whether we should cache views, once resolved */
  40. private boolean cache = true;
  41. /**
  42. * Enable respectively disable caching. Disable this only for debugging
  43. * and development. Default is for caching to be enabled.
  44. * <p><b>Warning: Disabling caching can severely impact performance.</b>
  45. * Tests indicate that turning caching off reduces performance by at least 20%.
  46. * Increased object churn probably eventually makes the problem even worse.
  47. */
  48. public void setCache(boolean cache) {
  49. this.cache = cache;
  50. }
  51. /**
  52. * Return if caching is enabled.
  53. */
  54. public boolean isCache() {
  55. return cache;
  56. }
  57. public View resolveViewName(String viewName, Locale locale) throws Exception {
  58. View view = null;
  59. if (!this.cache) {
  60. logger.warn("View caching is SWITCHED OFF -- DEVELOPMENT SETTING ONLY: This can severely impair performance");
  61. view = loadAndConfigureView(viewName, locale);
  62. }
  63. else {
  64. String cacheKey = getCacheKey(viewName, locale);
  65. view = (View) this.viewMap.get(cacheKey);
  66. if (view == null) {
  67. // ask the subclass to load the View
  68. view = loadAndConfigureView(viewName, locale);
  69. this.viewMap.put(cacheKey, view);
  70. logger.info("Cached view '" + cacheKey + "'");
  71. }
  72. }
  73. return view;
  74. }
  75. /**
  76. * Load and configure the given View. Only invoked once per View.
  77. * Delegates to the loadView template method for actual loading.
  78. * <p>Sets the ApplicationContext on the View if necessary.
  79. * @see #loadView
  80. */
  81. private View loadAndConfigureView(String viewName, Locale locale) throws Exception {
  82. View view = loadView(viewName, locale);
  83. if (view instanceof ApplicationContextAware) {
  84. ((ApplicationContextAware) view).setApplicationContext(getApplicationContext());
  85. }
  86. return view;
  87. }
  88. /**
  89. * Return the cache key for the given viewName and the given locale.
  90. * Needs to regard the locale in general, as a different locale can lead to a
  91. * different view! Can be overridden in subclasses.
  92. */
  93. protected String getCacheKey(String viewName, Locale locale) {
  94. return viewName + "_" + locale;
  95. }
  96. /**
  97. * Subclasses must implement this method. There need be no concern
  98. * for efficiency, as this class will cache views.
  99. * <p>Not all subclasses may support internationalization:
  100. * A subclass that doesn't can simply ignore the locale parameter.
  101. * <p>This method is not supposed to fully initialize the view (for example,
  102. * ApplicationContextAware methods haven't been called yet). Clients should only
  103. * be using resolveViewName, which does fully initialize the view objects found.
  104. * @param viewName the name of the view to retrieve
  105. * @param locale the Locale to retrieve the view for
  106. * @return the View instance, or null if not found
  107. * (optional, to allow for ViewResolver chaining)
  108. * @throws Exception if the view couldn't be resolved
  109. * @see #resolveViewName
  110. */
  111. protected abstract View loadView(String viewName, Locale locale) throws Exception;
  112. }