/*
 * The Spring Framework is published under the terms
 * of the Apache Software License.
 */

package org.springframework.context.support;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationContextException;

/**
 * Convenient superclass for application objects that want to be aware of
 * the application context, e.g. for custom lookup of collaborating beans
 * or for context-specific resource access. It saves the application
 * context reference and provides an initialization callback method.
 * Furthermore, it offers numerous convenience methods for message lookup.
 *
 * <p>There is no requirement to subclass this class: It just makes things
 * a little easier if you need access to the context, e.g. for access to
 * file resources or to the message source. Note that many application
 * objects do not need to be aware of the application context at all,
 * as they can receive collaborating beans via bean references.
 *
 * <p>Many framework classes are derived from this class, particularly
 * within the web support.
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 */
public abstract class ApplicationObjectSupport implements ApplicationContextAware {
	
	/** Logger that is available to subclasses */
	protected final Log logger = LogFactory.getLog(getClass());
	
	/** ApplicationContext this object runs in */
	private ApplicationContext applicationContext;

	/** MessageSourceAccessor for easy message access */
	private MessageSourceAccessor messageSourceAccessor;

	/**
	 * Constructor for bean usage via subclassing.
	 */
	public ApplicationObjectSupport() {
	}

	/**
	 * Constructor for usage as helper to delegate to.
	 * @param context ApplicationContext object to be used by this object
	 */
	public ApplicationObjectSupport(ApplicationContext context) {
		this.applicationContext = context;
	}

	public final void setApplicationContext(ApplicationContext context) throws BeansException {
		// ignore reinitialization
		if (this.applicationContext == null) {
			if (!requiredContextClass().isInstance(context)) {
				throw new ApplicationContextException("Invalid application context: needs to be of type '" +
				                                      requiredContextClass().getName() + "'");
			}
			this.applicationContext = context;
			this.messageSourceAccessor = new MessageSourceAccessor(context);
			initApplicationContext();
		}
	}
	
	/**
	 * Determine the context class that any context passed to
	 * setApplicationContext must be an instance of.
	 * Can be overridden in subclasses.
	 */
	protected Class requiredContextClass() {
		return ApplicationContext.class;
	}

	/**
	 * Subclasses can override this for custom initialization behavior.
	 * Gets called by setApplicationContext() after setting the context instance.
	 * <p>Note: Does </i>not</i> get called on reinitialization of the context.
	 * @throws ApplicationContextException in case of initialization errors
	 * @throws BeansException if thrown by application context methods
	 */
	protected void initApplicationContext() throws BeansException {
	}

	/**
	 * Return the ApplicationContext instance used by this object.
	 */
	public final ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	/**
	 * Return a MessageSourceAccessor for the application context
	 * used by this object, for easy message access.
	 */
	protected final MessageSourceAccessor getMessageSourceAccessor() {
		return this.messageSourceAccessor;
	}

}

@@ -2163,37 +2163,8 @@
 n %7B%0A
-%09%09// ignore reinitialization%0A
 %09%09if
@@ -2578,16 +2578,236 @@
 ();%0A%09%09%7D%0A
+%09%09else %7B%0A%09%09%09// ignore reinitialization if same context passed in%0A%09%09%09if (this.applicationContext != context) %7B%0A%09%09%09%09throw new ApplicationContextException(%22Cannot reinitialize with different application context%22);%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%7D%0A%09%0A%09/*

check for same application context on reinitialization