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.ui.context.support;
  17. import java.util.Arrays;
  18. import org.apache.commons.logging.Log;
  19. import org.apache.commons.logging.LogFactory;
  20. import org.springframework.beans.factory.NoSuchBeanDefinitionException;
  21. import org.springframework.context.ApplicationContext;
  22. import org.springframework.ui.context.HierarchicalThemeSource;
  23. import org.springframework.ui.context.ThemeSource;
  24. /**
  25. * Utilities common to all UI application context implementations.
  26. * @author Jean-Pierre Pawlak
  27. * @author Juergen Hoeller
  28. * @since 17.06.2003
  29. */
  30. public abstract class UiApplicationContextUtils {
  31. /**
  32. * Name of the ThemeSource bean in the factory.
  33. * If none is supplied, theme resolution is delegated to the parent.
  34. * @see org.springframework.ui.context.ThemeSource
  35. */
  36. public static final String THEME_SOURCE_BEAN_NAME = "themeSource";
  37. private static final Log logger = LogFactory.getLog(UiApplicationContextUtils.class);
  38. /**
  39. * Initialize the ThemeSource for the given application context,
  40. * auto-detecting a bean with the name "themeSource". If no such
  41. * bean is found, a default (empty) ThemeSource will be used.
  42. * @param context current application context
  43. * @return the initialized theme source (will never be null)
  44. * @see #THEME_SOURCE_BEAN_NAME
  45. */
  46. public static ThemeSource initThemeSource(ApplicationContext context) {
  47. ThemeSource themeSource;
  48. try {
  49. themeSource = (ThemeSource) context.getBean(THEME_SOURCE_BEAN_NAME);
  50. // set parent theme source if applicable,
  51. // and if the theme source is defined in this context, not in a parent
  52. if (context.getParent() instanceof ThemeSource &&
  53. themeSource instanceof HierarchicalThemeSource &&
  54. Arrays.asList(context.getBeanDefinitionNames()).contains(THEME_SOURCE_BEAN_NAME)) {
  55. ((HierarchicalThemeSource) themeSource).setParentThemeSource((ThemeSource) context.getParent());
  56. }
  57. }
  58. catch (NoSuchBeanDefinitionException ex) {
  59. logger.info("No ThemeSource found for [" + context.getDisplayName() +
  60. "]: using ResourceBundleThemeSource");
  61. themeSource = new ResourceBundleThemeSource();
  62. }
  63. return themeSource;
  64. }
  65. }