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.context.support;
  17. import org.apache.commons.logging.Log;
  18. import org.apache.commons.logging.LogFactory;
  19. import org.springframework.beans.BeansException;
  20. import org.springframework.context.ApplicationContext;
  21. import org.springframework.context.ApplicationContextAware;
  22. import org.springframework.context.ApplicationContextException;
  23. /**
  24. * Convenient superclass for application objects that want to be aware of
  25. * the application context, e.g. for custom lookup of collaborating beans
  26. * or for context-specific resource access. It saves the application
  27. * context reference and provides an initialization callback method.
  28. * Furthermore, it offers numerous convenience methods for message lookup.
  29. *
  30. * <p>There is no requirement to subclass this class: It just makes things
  31. * a little easier if you need access to the context, e.g. for access to
  32. * file resources or to the message source. Note that many application
  33. * objects do not need to be aware of the application context at all,
  34. * as they can receive collaborating beans via bean references.
  35. *
  36. * <p>Many framework classes are derived from this class, particularly
  37. * within the web support.
  38. *
  39. * @author Rod Johnson
  40. * @author Juergen Hoeller
  41. */
  42. public abstract class ApplicationObjectSupport implements ApplicationContextAware {
  43. /** Logger that is available to subclasses */
  44. protected final Log logger = LogFactory.getLog(getClass());
  45. /** ApplicationContext this object runs in */
  46. private ApplicationContext applicationContext;
  47. /** MessageSourceAccessor for easy message access */
  48. private MessageSourceAccessor messageSourceAccessor;
  49. /**
  50. * Constructor for bean usage via subclassing.
  51. */
  52. public ApplicationObjectSupport() {
  53. }
  54. /**
  55. * Constructor for usage as helper to delegate to.
  56. * @param context ApplicationContext object to be used by this object
  57. */
  58. public ApplicationObjectSupport(ApplicationContext context) {
  59. this.applicationContext = context;
  60. }
  61. public final void setApplicationContext(ApplicationContext context) throws BeansException {
  62. if (this.applicationContext == null) {
  63. if (!requiredContextClass().isInstance(context)) {
  64. throw new ApplicationContextException("Invalid application context: needs to be of type '" +
  65. requiredContextClass().getName() + "'");
  66. }
  67. this.applicationContext = context;
  68. this.messageSourceAccessor = new MessageSourceAccessor(context);
  69. initApplicationContext();
  70. }
  71. else {
  72. // ignore reinitialization if same context passed in
  73. if (this.applicationContext != context) {
  74. throw new ApplicationContextException("Cannot reinitialize with different application context");
  75. }
  76. }
  77. }
  78. /**
  79. * Determine the context class that any context passed to
  80. * setApplicationContext must be an instance of.
  81. * Can be overridden in subclasses.
  82. */
  83. protected Class requiredContextClass() {
  84. return ApplicationContext.class;
  85. }
  86. /**
  87. * Subclasses can override this for custom initialization behavior.
  88. * Gets called by setApplicationContext() after setting the context instance.
  89. * <p>Note: Does </i>not</i> get called on reinitialization of the context.
  90. * @throws ApplicationContextException in case of initialization errors
  91. * @throws BeansException if thrown by application context methods
  92. */
  93. protected void initApplicationContext() throws BeansException {
  94. }
  95. /**
  96. * Return the ApplicationContext instance used by this object.
  97. */
  98. public final ApplicationContext getApplicationContext() {
  99. return applicationContext;
  100. }
  101. /**
  102. * Return a MessageSourceAccessor for the application context
  103. * used by this object, for easy message access.
  104. */
  105. protected final MessageSourceAccessor getMessageSourceAccessor() {
  106. return this.messageSourceAccessor;
  107. }
  108. }