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.context.support;
  17. import javax.servlet.ServletContext;
  18. import org.springframework.beans.BeansException;
  19. import org.springframework.context.support.StaticApplicationContext;
  20. import org.springframework.ui.context.Theme;
  21. import org.springframework.ui.context.ThemeSource;
  22. import org.springframework.ui.context.support.UiApplicationContextUtils;
  23. import org.springframework.web.context.ConfigurableWebApplicationContext;
  24. /**
  25. * Static WebApplicationContext implementation for testing.
  26. * Not for use in production applications.
  27. *
  28. * <p>In addition to the special beans detected by AbstractApplicationContext,
  29. * this class detects a ThemeSource bean in the context, with the name
  30. * "themeSource".
  31. *
  32. * @author Rod Johnson
  33. * @author Juergen Hoeller
  34. * @see org.springframework.ui.context.ThemeSource
  35. */
  36. public class StaticWebApplicationContext extends StaticApplicationContext
  37. implements ConfigurableWebApplicationContext {
  38. private ServletContext servletContext;
  39. private String namespace;
  40. private ThemeSource themeSource;
  41. public void setServletContext(ServletContext servletContext) {
  42. this.servletContext = servletContext;
  43. }
  44. public ServletContext getServletContext() {
  45. return servletContext;
  46. }
  47. public void setNamespace(String namespace) {
  48. this.namespace = namespace;
  49. }
  50. protected String getNamespace() {
  51. return this.namespace;
  52. }
  53. public void setConfigLocations(String[] configLocations) {
  54. throw new UnsupportedOperationException("StaticWebApplicationContext does not support configLocations");
  55. }
  56. public void refresh() throws BeansException {
  57. if (this.namespace != null) {
  58. setDisplayName("StaticWebApplicationContext for namespace '" + this.namespace + "'");
  59. }
  60. else {
  61. setDisplayName("Root StaticWebApplicationContext");
  62. }
  63. super.refresh();
  64. }
  65. /**
  66. * Initialize the theme capability.
  67. */
  68. protected void onRefresh() {
  69. this.themeSource = UiApplicationContextUtils.initThemeSource(this);
  70. }
  71. public Theme getTheme(String themeName) {
  72. return this.themeSource.getTheme(themeName);
  73. }
  74. }