com.interface21.context.support
Class AbstractApplicationContext

java.lang.Object
  |
  +--com.interface21.context.support.AbstractApplicationContext
All Implemented Interfaces:
ApplicationContext, BeanFactory, ListableBeanFactory, MessageSource
Direct Known Subclasses:
AbstractXmlApplicationContext, StaticApplicationContext

public abstract class AbstractApplicationContext
extends java.lang.Object
implements ApplicationContext

Partial implementation of ApplicationContext. Doesn't mandate the type of storage used for configuration, but implements common functionality.

This class uses the Template Method design pattern, requiring concrete subclasses to implement protected abstract methods.

The context options may be supplied as a bean in the default bean factory, with the name "contextOptions".

A message source may be supplied as a bean in the default bean factory, with the name "messageSource". Else, message resolution is delegated to the parent context.

Since:
January 21, 2001
Version:
$Revision: 1.22 $
Author:
Rod Johnson
See Also:
refreshBeanFactory(), getBeanFactory(), OPTIONS_BEAN_NAME, MESSAGE_SOURCE_BEAN_NAME

Field Summary
protected  org.apache.commons.logging.Log logger
          Log4j logger used by this class.
static java.lang.String MESSAGE_SOURCE_BEAN_NAME
          Name of the MessageSource bean in the factory.
static java.lang.String OPTIONS_BEAN_NAME
          Name of options bean in the factory.
 
Constructor Summary
AbstractApplicationContext()
          Create a new AbstractApplicationContext with no parent.
AbstractApplicationContext(ApplicationContext parent)
          Create a new AbstractApplicationContext with the given parent context.
 
Method Summary
protected  void addListener(ApplicationListener l)
          Add a listener.
protected  void configureManagedObject(java.lang.Object o)
          If the object is context-aware, give it a reference to this object.
 java.lang.String[] getAliases(java.lang.String name)
          Return the aliases for the given bean name, if defined.
 java.lang.Object getBean(java.lang.String name)
          Try to find the bean instance in the hierarchy.
 java.lang.Object getBean(java.lang.String name, java.lang.Class requiredType)
          Return an instance (possibly shared or independent) of the given bean name.
 int getBeanDefinitionCount()
          Return the number of beans defined in the factory
 java.lang.String[] getBeanDefinitionNames()
          Return the names of all beans defined in this factory
 java.lang.String[] getBeanDefinitionNames(java.lang.Class type)
          Return the names of beans matching the given object type (including subclasses).
protected abstract  ListableBeanFactory getBeanFactory()
          Unimplemented interface method.
 java.lang.String getDisplayName()
          Return a friendly name for context
 java.lang.String getMessage(MessageSourceResolvable resolvable, java.util.Locale locale)
          Using all the attributes contained within the MessageSourceResolvable arg that was passed in (except for the locale attribute), try to resolve the message from the MessageSource contained within the Context. NOTE: We must throw a NoSuchMessageException on this method since at the time of calling this method we aren't able to determine if the defaultMessage attribute is null or not.
 java.lang.String getMessage(java.lang.String code, java.lang.Object[] args, java.util.Locale locale)
          Try to resolve the message.
 java.lang.String getMessage(java.lang.String code, java.lang.Object[] args, java.lang.String defaultMessage, java.util.Locale locale)
          Try to resolve the message.Return default message if no message was found.
 ContextOptions getOptions()
          Return context options.
 ApplicationContext getParent()
          Return the parent context, or null if there is no parent, and this is the root of the context hierarchy.
 java.io.InputStream getResourceAsStream(java.lang.String location)
          This implementation supports fully qualified URLs and appropriate (file) paths, via getResourceByPath.
 java.lang.String getResourceBasePath()
          This implementation returns the working directory of the Java VM.
protected  java.io.InputStream getResourceByPath(java.lang.String path)
          Return input stream to the resource at the given (file) path.
 long getStartupDate()
          Return the timestamp when this context was first loaded
 boolean isSingleton(java.lang.String name)
          Is this bean a singleton?
protected  void onRefresh()
          Callback method which can be overridden to add context-specific refresh work.
 void publishEvent(ApplicationEvent event)
          Publish the given event to all listeners.
 void refresh()
          Load or reload configuration.
protected abstract  void refreshBeanFactory()
          Subclasses must implement this method to perform the actual configuration load.
 java.lang.Object removeSharedObject(java.lang.String key)
          Remove a shared object added with a call to shareObject().
protected  void setDisplayName(java.lang.String displayName)
          To avoid endless constructor chaining, only concrete classes take this in their constructor, and then invoke this method
protected  void setParent(ApplicationContext ac)
          Subclasses may call this to set parent after constructor.
 java.lang.Object sharedObject(java.lang.String key)
          Retrieve a shared object added with a call to shareObject().
 void shareObject(java.lang.String key, java.lang.Object o)
          Put an object available for sharing.
 java.lang.String toString()
          Show information about this context
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

OPTIONS_BEAN_NAME

public static final java.lang.String OPTIONS_BEAN_NAME
Name of options bean in the factory. If none is supplied, a default ContextOptions instance will be used.
See Also:
ContextOptions

MESSAGE_SOURCE_BEAN_NAME

public static final java.lang.String MESSAGE_SOURCE_BEAN_NAME
Name of the MessageSource bean in the factory. If none is supplied, message resolution is delegated to the parent.
See Also:
MessageSource

logger

protected final org.apache.commons.logging.Log logger
Log4j logger used by this class. Available to subclasses.
Constructor Detail

AbstractApplicationContext

public AbstractApplicationContext()
Create a new AbstractApplicationContext with no parent.

AbstractApplicationContext

public AbstractApplicationContext(ApplicationContext parent)
Create a new AbstractApplicationContext with the given parent context.
Parameters:
parent - parent context
Method Detail

getParent

public ApplicationContext getParent()
Return the parent context, or null if there is no parent, and this is the root of the context hierarchy.
Specified by:
getParent in interface ApplicationContext
Returns:
the parent context, or null if there is no parent

setParent

protected void setParent(ApplicationContext ac)
Subclasses may call this to set parent after constructor. Note that parent shouldn't be changed: it should only be set later if it isn't available when an object of this class is created.
Parameters:
ac - parent context

getDisplayName

public java.lang.String getDisplayName()
Return a friendly name for context
Specified by:
getDisplayName in interface ApplicationContext
Returns:
a display name for the context

setDisplayName

protected void setDisplayName(java.lang.String displayName)
To avoid endless constructor chaining, only concrete classes take this in their constructor, and then invoke this method

getStartupDate

public final long getStartupDate()
Return the timestamp when this context was first loaded
Specified by:
getStartupDate in interface ApplicationContext
Returns:
the timestamp (ms) when this context was first loaded

getOptions

public final ContextOptions getOptions()
Return context options. These control reloading etc.
Specified by:
getOptions in interface ApplicationContext
Returns:
context options

refresh

public final void refresh()
                   throws ApplicationContextException
Load or reload configuration.
Specified by:
refresh in interface ApplicationContext
Throws:
ApplicationContextException - if the configuration was invalid or couldn't be found, or if configuration has already been loaded and reloading is forbidden DYNAMIC CLASSLOADER ISSUE...subclass to get classloader!?

onRefresh

protected void onRefresh()
                  throws ApplicationContextException
Callback method which can be overridden to add context-specific refresh work.
Throws:
ApplicationContextException - in case of errors during refresh

configureManagedObject

protected void configureManagedObject(java.lang.Object o)
                               throws ApplicationContextException
If the object is context-aware, give it a reference to this object. Note that the implementation of the ApplicationContextAware interface must check itself that if it is already initialized resp. if it wants to perform reinitialization.
Parameters:
o - object to invoke the setApplicationContext() method on, if it implements the ApplicationContextAware interface

publishEvent

public final void publishEvent(ApplicationEvent event)
Publish the given event to all listeners.
Specified by:
publishEvent in interface ApplicationContext
Parameters:
event - event to publish. The event may be application-specific, or a standard framework event.

addListener

protected void addListener(ApplicationListener l)
Add a listener. Any beans that are listeners are automatically added.

getResourceAsStream

public final java.io.InputStream getResourceAsStream(java.lang.String location)
                                              throws java.io.IOException
This implementation supports fully qualified URLs and appropriate (file) paths, via getResourceByPath. Throws a FileNotFoundException if getResourceByPath returns null.
Specified by:
getResourceAsStream in interface ApplicationContext
See Also:
getResourceByPath(java.lang.String)

getResourceByPath

protected java.io.InputStream getResourceByPath(java.lang.String path)
                                         throws java.io.IOException
Return input stream to the resource at the given (file) path.

Default implementation supports file paths, either absolute or relative to the application's working directory. This should be appropriate for standalone implementations but can be overridden, e.g. for implementations targetted at a container.

Parameters:
path - path to the resource
Returns:
InputStream for the specified resource, can be null if not found (instead of throwing an exception)
Throws:
java.io.IOException - exception when opening the specified resource

getResourceBasePath

public java.lang.String getResourceBasePath()
This implementation returns the working directory of the Java VM. This should be appropriate for standalone implementations but can be overridden for implementations targetted at a container.
Specified by:
getResourceBasePath in interface ApplicationContext
Following copied from interface: com.interface21.context.ApplicationContext
Returns:
the resource base path (ending with a separator), or null

sharedObject

public java.lang.Object sharedObject(java.lang.String key)
Description copied from interface: ApplicationContext
Retrieve a shared object added with a call to shareObject().
Specified by:
sharedObject in interface ApplicationContext
Following copied from interface: com.interface21.context.ApplicationContext
Returns:
the object, or null if no object is known under this name (this is not an error).

shareObject

public void shareObject(java.lang.String key,
                        java.lang.Object o)
Description copied from interface: ApplicationContext
Put an object available for sharing. Note that this method is not synchronized. As with Java 2 collections, it's up to calling code to ensure thread safety. Also, this doesn't work in a cluster. It's analogous to putting something in a ServletContext.
Specified by:
shareObject in interface ApplicationContext
Following copied from interface: com.interface21.context.ApplicationContext
Parameters:
key - object key
o - object to put

removeSharedObject

public java.lang.Object removeSharedObject(java.lang.String key)
Description copied from interface: ApplicationContext
Remove a shared object added with a call to shareObject(). Does nothing if the object was null.
Specified by:
removeSharedObject in interface ApplicationContext
Following copied from interface: com.interface21.context.ApplicationContext
Parameters:
key - the object was added with
Returns:
the object if it was found, or null.

getMessage

public java.lang.String getMessage(java.lang.String code,
                                   java.lang.Object[] args,
                                   java.lang.String defaultMessage,
                                   java.util.Locale locale)
Try to resolve the message.Return default message if no message was found.
Specified by:
getMessage in interface MessageSource
Parameters:
code - code to lookup up, such as 'calculator.noRateSet'
locale - Locale in which to do lookup
args - Array of arguments that will be filled in for params within the message (params look like "{0}", "{1,date}", "{2,time}" within a message).
defaultMessage - String to return if the lookup fails
Returns:
a resolved message if the lookup is successful; otherwise return the default message passed as a parameter
See Also:
java.text.MessageFormat

getMessage

public java.lang.String getMessage(java.lang.String code,
                                   java.lang.Object[] args,
                                   java.util.Locale locale)
                            throws NoSuchMessageException
Try to resolve the message. Treat as an error if the message can't be found.
Specified by:
getMessage in interface MessageSource
Parameters:
code - code to lookup up, such as 'calculator.noRateSet'
locale - Locale in which to do lookup
args - Array of arguments that will be filled in for params within the message (params look like "{0}", "{1,date}", "{2,time}" within a message).
Returns:
message
Throws:
NoSuchMessageException - not found in any locale
See Also:
java.text.MessageFormat

getMessage

public java.lang.String getMessage(MessageSourceResolvable resolvable,
                                   java.util.Locale locale)
                            throws NoSuchMessageException
Using all the attributes contained within the MessageSourceResolvable arg that was passed in (except for the locale attribute), try to resolve the message from the MessageSource contained within the Context.

NOTE: We must throw a NoSuchMessageException on this method since at the time of calling this method we aren't able to determine if the defaultMessage attribute is null or not.

Specified by:
getMessage in interface MessageSource
Parameters:
resolvable - Value object storing 4 attributes required to properly resolve a message.
locale - Locale to be used as the "driver" to figuring out what message to return.
Returns:
message Resolved message.
Throws:
NoSuchMessageException - not found in any locale
See Also:
java.text.MessageFormat

getBean

public java.lang.Object getBean(java.lang.String name)
                         throws BeansException
Try to find the bean instance in the hierarchy.
Specified by:
getBean in interface BeanFactory
Following copied from interface: com.interface21.beans.factory.BeanFactory
Parameters:
name - name of the bean to return
Returns:
the instance of the bean
Throws:
NoSuchBeanDefinitionException - if there's no such bean definition

getBean

public java.lang.Object getBean(java.lang.String name,
                                java.lang.Class requiredType)
                         throws BeansException
Description copied from interface: BeanFactory
Return an instance (possibly shared or independent) of the given bean name. Provides a measure of type safety by throwing an exception if the bean is not of the required type. This method allows a bean factory to be used as a replacement for the Singleton or Prototype design pattern.
Note that callers should retain references to returned objects. There is no guarantee that this method will be implemented to be efficient. For example, it may be synchronized, or may need to run an RDBMS query.
Specified by:
getBean in interface BeanFactory
Following copied from interface: com.interface21.beans.factory.BeanFactory
Parameters:
name - name of the bean to return
requiredType - type the bean may match. Can be an interface or superclass of the actual class. For example, if the value is Object.class, this method will succeed whatever the class of the returned instance.
Returns:
the instance of the bean
Throws:
BeanNotOfRequiredTypeException - if the bean is not of the required type
NoSuchBeanDefinitionException - if there's no such bean definition

isSingleton

public boolean isSingleton(java.lang.String name)
                    throws NoSuchBeanDefinitionException
Description copied from interface: BeanFactory
Is this bean a singleton? That is, will getBean() always return the same object?
Specified by:
isSingleton in interface BeanFactory
Following copied from interface: com.interface21.beans.factory.BeanFactory
Parameters:
name - name of the bean to query
Returns:
is this bean a singleton
Throws:
NoSuchBeanDefinitionException - if there is no bean with the given name

getAliases

public java.lang.String[] getAliases(java.lang.String name)
Description copied from interface: BeanFactory
Return the aliases for the given bean name, if defined.
Specified by:
getAliases in interface BeanFactory
Following copied from interface: com.interface21.beans.factory.BeanFactory
Parameters:
name - the bean name to check for aliases
Returns:
the aliases, or an empty array if none

getBeanDefinitionCount

public int getBeanDefinitionCount()
Description copied from interface: ListableBeanFactory
Return the number of beans defined in the factory
Specified by:
getBeanDefinitionCount in interface ListableBeanFactory
Following copied from interface: com.interface21.beans.factory.ListableBeanFactory
Returns:
the number of beans defined in the factory

getBeanDefinitionNames

public java.lang.String[] getBeanDefinitionNames()
Description copied from interface: ListableBeanFactory
Return the names of all beans defined in this factory
Specified by:
getBeanDefinitionNames in interface ListableBeanFactory
Following copied from interface: com.interface21.beans.factory.ListableBeanFactory
Returns:
the names of all beans defined in this factory. Returns the empty String[], rather than null, if no beans are defined.

getBeanDefinitionNames

public java.lang.String[] getBeanDefinitionNames(java.lang.Class type)
Description copied from interface: ListableBeanFactory
Return the names of beans matching the given object type (including subclasses).
Specified by:
getBeanDefinitionNames in interface ListableBeanFactory
Following copied from interface: com.interface21.beans.factory.ListableBeanFactory
Parameters:
type - class or interface to match
Returns:
the names of beans matching the given object type (including subclasses). Never returns null.

toString

public java.lang.String toString()
Show information about this context
Overrides:
toString in class java.lang.Object

refreshBeanFactory

protected abstract void refreshBeanFactory()
                                    throws ApplicationContextException
Subclasses must implement this method to perform the actual configuration load.

getBeanFactory

protected abstract ListableBeanFactory getBeanFactory()
Unimplemented interface method. Subclasses must implement this efficiently, so that it can be called repeatedly without a performance penalty.
Returns:
this application context's default BeanFactory


Rod Johnson and Spring contributors 2001-2003.