Update of /cvsroot/springnet/Spring.Net/src/Spring/Spring.Context/Context/Support
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8357/src/Spring/Spring.Context/Context/Support
Added Files:
ApplicationObjectSupport.cs MessageSourceAccessor.cs
Log Message:
- Added MessageSourceAccessor
- Added ApplicationObjectSupport & tests
- Added CommonTypes class for common subclasses in test assembly & updated existing tests.
--- NEW FILE: MessageSourceAccessor.cs ---
using System;
using System.Globalization;
namespace Spring.Context.Support
{
/// <summary>
/// Helper class for easy access to messages from a MessageSource,
/// providing various overloaded getMessage methods.
///
/// Available from ApplicationObjectSupport, but also reusable
/// as a standalone helper to delegate to in application objects.
/// </summary>
/// <author>Juergen Hoeller</author>
/// <author>Griffin Caprio (.NET)</author>
public class MessageSourceAccessor
{
private IMessageSource _messageSource;
private CultureInfo _defaultCultureInfo;
/// <summary>
/// Default Constructor uses Current CultureInfo for all locale specific lookups
/// </summary>
/// <param name="messageSource">IMessageSource to use to locate messages</param>
public MessageSourceAccessor( IMessageSource messageSource ) : this( messageSource, CultureInfo.CurrentCulture ) {}
/// <summary>
/// Default Constructor
/// </summary>
/// <param name="messageSource">IMessageSource to use to locate messages</param>
/// <param name="cultureInfo">Culture Info to use for locale specific messages</param>
public MessageSourceAccessor( IMessageSource messageSource, CultureInfo cultureInfo )
{
_messageSource = messageSource;
_defaultCultureInfo = cultureInfo;
}
/// <summary>
/// Retrieve the message for the given code and the default Locale.
/// </summary>
/// <param name="code">code of the message</param>
/// <param name="defaultMessage">String to return if the lookup fails</param>
/// <returns>the message</returns>
public string GetMessage( string code, string defaultMessage )
{
return GetMessage( code, defaultMessage, _defaultCultureInfo );
}
/// <summary>
/// Retrieve the message for the given code and the given Locale.
/// </summary>
/// <param name="code">code of the message</param>
/// <param name="defaultMessage">String to return if the lookup fails</param>
/// <param name="cultureInfo">Culture Info to use</param>
/// <returns>the message</returns>
public string GetMessage( string code, string defaultMessage, CultureInfo cultureInfo )
{
return _messageSource.GetMessage( code, null, defaultMessage, cultureInfo );
}
/// <summary>
/// Retrieve the message for the given code and the default Locale.
/// </summary>
/// <param name="code">code of the message</param>
/// <param name="args">arguments for the message, or null if none</param>
/// <param name="defaultMessage">String to return if the lookup fails</param>
/// <returns>the message</returns>
public string GetMessage( string code, object[] args, string defaultMessage )
{
return GetMessage( code, args, defaultMessage, _defaultCultureInfo );
}
/// <summary>
/// Retrieve the message for the given code and the given Locale.
/// </summary>
/// <param name="code">code of the message</param>
/// <param name="args">arguments for the message, or null if none</param>
/// <param name="defaultMessage">String to return if the lookup fails</param>
/// <param name="cultureInfo">CultureInfo in which to do lookup</param>
/// <returns>the message</returns>
public string GetMessage( string code, object[] args, string defaultMessage, CultureInfo cultureInfo )
{
return _messageSource.GetMessage( code, args, defaultMessage, cultureInfo );
}
/// <summary>
/// Retrieve the message for the given code and the default Locale.
/// </summary>
/// <param name="code">code of the message</param>
/// <returns>the message</returns>
/// throws <exception cref="NoSuchMessageException">if not found</exception>
public string GetMessage( string code )
{
return GetMessage( code, _defaultCultureInfo );
}
/// <summary>
/// Retrieve the message for the given code and the given Locale.
/// </summary>
/// <param name="code">code of the message</param>
/// <param name="cultureInfo">CultureInfo in which to do lookup</param>
/// <returns>the message</returns>
/// throws <exception cref="NoSuchMessageException">if not found</exception>
public string GetMessage( string code, CultureInfo cultureInfo )
{
return _messageSource.GetMessage( code, null, cultureInfo );
}
/// <summary>
/// Retrieve the message for the given code and the default Locale.
/// </summary>
/// <param name="code">code of the message</param>
/// <param name="args">arguments for the message, or null if none</param>
/// <returns>the message</returns>
/// throws <exception cref="NoSuchMessageException">if not found</exception>
public string GetMessage( string code, object[] args )
{
return GetMessage( code, args, _defaultCultureInfo );
}
/// <summary>
/// Retrieve the message for the given code and the given Locale.
/// </summary>
/// <param name="code">code of the message</param>
/// <param name="args">arguments for the message, or null if none</param>
/// <param name="cultureInfo">CultureInfo in which to do lookup</param>
/// <returns>the message</returns>
/// throws <exception cref="NoSuchMessageException">if not found</exception>
public string GetMessage( string code, object[] args, CultureInfo cultureInfo )
{
return _messageSource.GetMessage( code, args, cultureInfo );
}
/// <summary>
/// Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance)
/// </summary>
/// <param name="resolvable">the MessageSourceResolvable</param>
/// <returns>the message</returns>
/// throws <exception cref="NoSuchMessageException">if not found</exception>
public string GetMessage( IMessageSourceResolvable resolvable )
{
return GetMessage( resolvable, _defaultCultureInfo );
}
/// <summary>
/// Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance) in the given Locale.
/// </summary>
/// <param name="resolvable">the MessageSourceResolvable</param>
/// <param name="cultureInfo">CultureInfo in which to do lookup</param>
/// <returns>the message</returns>
/// throws <exception cref="NoSuchMessageException">if not found</exception>
public string GetMessage( IMessageSourceResolvable resolvable, CultureInfo cultureInfo )
{
return _messageSource.GetMessage( resolvable, cultureInfo );
}
}
}
--- NEW FILE: ApplicationObjectSupport.cs ---
using System;
using Spring.Objects;
namespace Spring.Context.Support
{
/// <summary>
/// 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.
///
/// 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.
///
/// Many framework classes are derived from this class, particularly
/// within the web support.
/// </summary>
/// <author>Rod Johnson</author>
/// <author>Juergen Hoeller</author>
/// <author>Griffin Caprio (.NET)</author>
public abstract class ApplicationObjectSupport : IApplicationContextAware
{
/// <summary>
/// IApplicationContext that this object runs in
/// </summary>
private IApplicationContext _applicationContext;
/// <summary>
/// MessageSourceAccessor for easy message access
/// </summary>
private MessageSourceAccessor _messageSourceAccessor;
/// <summary>
/// Constructor for bean usage via subclassing
/// </summary>
public ApplicationObjectSupport()
{
}
/// <summary>
/// Constructor for usage as helper to delegate to.
/// </summary>
/// <param name="applicationContext">ApplicationContext object to be used by this object</param>
public ApplicationObjectSupport( IApplicationContext applicationContext )
{
_applicationContext = applicationContext;
}
/// <summary>
/// Determine the context class that any context passed to
/// setApplicationContext must be an instance of.
/// Can be overridden in subclasses.
/// </summary>
/// <returns>IApplicationContext type</returns>
protected virtual Type RequiredClass()
{
return typeof( IApplicationContext );
}
/// <summary>
/// Subclasses can override this for custom initialization behavior.
/// Gets called by setApplicationContext() after setting the context instance.
/// Note: Does not get called on reinitialization of the context.
/// </summary>
/// throws <exception cref="ApplicationContextException">ApplicationContextException</exception>in case of initialization errors
/// throws <exception cref="ObjectsException">ObjectsException</exception>if thrown by application context methods
protected virtual void initApplicationContext()
{
}
/// <summary>
/// Return a MessageSourceAccessor for the application context
/// used by this object, for easy message access.
/// </summary>
public MessageSourceAccessor MessageSourceAccessor
{
get { return _messageSourceAccessor; }
}
#region IApplicationContextAware Members
/// <summary>
/// Gets / Sets the ApplicationContext instance used by this object
///
/// Initialzes the context on a Set.
/// </summary>
/// throws <exception cref="ApplicationContextException">ApplicationContextException</exception> when passed an unexpected
/// IApplicationContext not defined by RequiredClass. Also, thrown when trying to re-init with a different IApplicationContext than originally
/// used.
public IApplicationContext ApplicationContext
{
get { return _applicationContext; }
set
{
if ( _applicationContext == null )
{
if ( value.GetType() != RequiredClass() )
{
throw new ApplicationContextException(
"Invalid application context: needs to by of type '" + RequiredClass().DeclaringType + "'");
}
_applicationContext = value;
_messageSourceAccessor = new MessageSourceAccessor( value );
initApplicationContext();
}
else
{
if ( _applicationContext != value )
{
throw new ApplicationContextException( "Cannot reinitialize with different application context" );
}
}
}
}
#endregion
}
}
|