ymikulski 2002/08/19 11:43:45
Added: csframework/src/cs/Util ContainerUtil.cs
Log:
no message
Revision Changes Path
1.1
jakarta-avalon-excalibur/csframework/src/cs/Util/ContainerUtil.cs
Index: ContainerUtil.cs
===================================================================
///
/// Copyright (C) The Apache Software Foundation. All rights reserved.
///
/// This software is published under the terms of the Apache Software License
/// version 1.1, a copy of which has been included with this distribution in
/// the LICENSE.txt file.
///
using System;
using Apache.Avalon.Activity;
using Apache.Avalon.Logger;
using Apache.Avalon.Context;
using Apache.Avalon.Service;
using Apache.Avalon.Configuration;
using Apache.Avalon.Parameter;
namespace Apache.Avalon.Util
{
/// <summary>
/// Utility class that makes it easier to transfer
/// a component throught it's lifecycle stages.
/// </summary>
public class ContainerUtil
{
/// <summary>
/// Runs specified object through shutdown lifecycle stages
/// (Stop and Dispose).
/// </summary>
/// <param name="component">The Component to shutdown.</param>
/// <exception cref="Exception">If there is a problem stoppping
object.</exception>
public static void Shutdown(object component)
{
Stop(component);
Dispose(component);
}
/// <summary>
/// Supplies specified object with Logger if it implements the
/// <see cref="ILogEnabled"/> interface.
/// </summary>
/// <param name="component"></param>
/// <param name="logger">The Logger to enable component with.</param>
/// <exception cref="ArgumentException">
/// If the component is <see cref="ILogEnabled"/> but <see
cref="ILogger"/> is null.
/// </exception>
/// <remarks>
/// The Logger may be null in which case
/// the specified component must not implement <see
cref="ILogEnabled"/>.
/// </remarks>
public static void EnableLogging(object component, ILogger logger)
{
if (component is ILogEnabled)
{
if (logger == null)
{
throw new ArgumentException("logger is null");
}
((ILogEnabled) component).EnableLogging(logger);
}
}
/// <summary>
/// Supplies specified object with a <see cref="IContext"/> object if
it implements the
/// <see cref="IContextualizable"/> interface.
/// </summary>
/// <param name="component">The Component to contextualize.</param>
/// <param name="context">The Context to use for component.</param>
/// <exception cref="ContextException">
/// If there is a problem contextualizing object.
/// </exception>
/// <exception cref="ArgumentException">
/// If the component is <see cref="IContextualizable"/> but context is
null.
/// </exception>
/// <remarks>
/// The Context may be null in which case the specified component must
not
/// implement <see cref="IContextualizable"/>.
/// </remarks>
public static void Contextualize(object component, IContext context)
{
if (component is IContextualizable)
{
if (context == null)
{
throw new ArgumentException("context is null");
}
((IContextualizable) component).Contextualize(context);
}
}
/// <summary>
/// Supplies specified component with <see cref="IServiceManager"/>
/// if it implements the <see cref="IServiceable"/> interface.
/// </summary>
/// <param name="component">The Component to service.</param>
/// <param name="serviceManager">
/// The <see cref="IServiceManager"/> object to use for component.
/// </param>
/// <exception cref="ArgumentException">
/// If the object is <see cref="IServicable"/> but
/// <see cref="IServiceManager"/> is null.
/// </exception>
/// <exception cref="ServiceException">
/// If there is a problem servicing component.
/// </exception>
/// <remarks>
/// The Service manager may be null in
/// which case the specified component must not
/// implement <see cref="IServiceable"/>.
/// </remarks>
public static void Service(object component, IServiceManager
serviceManager)
{
if (component is IServiceable)
{
if (serviceManager == null)
{
throw new ArgumentException("ServiceManager is
null");
}
((IServiceable) component).Service(serviceManager);
}
}
/// <summary>
/// Configures specified component if it implements the
/// <see cref="IConfigurable"/> interface.
/// </summary>
/// <param name="component">The Component to configure.</param>
/// <param name="configuration">
/// The Configuration object to use during the configuration.
/// </param>
/// <exception cref="ArgumentException">
/// If the component is <see cref="IConfigurable"/> but
/// configuration is null.
/// </exception>
/// <exception cref="ConfigurationException">
/// If there is a problem configuring component,
/// or the component is <see cref="IConfigurable"/> but configuration
is null.
/// </exception>
/// <remarks>
/// The Configuration may be null in which case
/// the specified component must not implement <see
cref="IConfigurable"/>.
/// </remarks>
public static void Configure(object component, IConfiguration
configuration)
{
if (component is IConfigurable)
{
if (configuration == null)
{
throw new ArgumentException("configuration is
null");
}
((IConfigurable)
configuration).Configure(configuration);
}
}
/// <summary>
/// Parameterizes specified object if it implements the
/// <see cref="IParameterizable"/> interface.
/// </summary>
/// <param name="component">The Component to parameterize.</param>
/// <param name="parameters">
/// The Parameters object to use during parameterization.
/// </param>
/// <exception cref="ParameterException">
/// If there is a problem parameterizing component.
/// </exception>
/// <excepion cref="ArgumentException">
/// If the component is <see cref="IParameterizable"/> but
/// parameters is null.
/// </excepion>
/// <remarks>
/// The Parameters may be null in which case
/// the specified component must not implement <see
cref="IParameterizable"/>.
/// </remarks>
public static void Parameterize(object component, Parameters
parameters)
{
if (component is IParameterizable)
{
if (parameters == null)
{
throw new ArgumentException("parameters is
null");
}
((IParameterizable)
component).Parameterize(parameters);
}
}
/// <summary>
/// Initializes specified component if it implements the
/// <see cref="IInitializable"/> interface.
/// </summary>
/// <param name="component">
/// The Component to initialize.
/// </param>
/// <exception cref="Exception">
/// If there is a problem initializing component.
/// </exception>
public static void Initialize(object component)
{
if (component is IInitializable)
{
( (IInitializable) component).Initialize();
}
}
/// <summary>
/// Starts specified component if it implements the
/// <see cref="IStartable"/> interface.
/// </summary>
/// <param name="component">The Component to start.</param>
/// <exception cref="Exception">
/// If there is a problem starting component.
/// </exception>
public static void Start(object component)
{
if (component is IStartable)
{
( (IStartable) component).Start();
}
}
/// <summary>
/// Stops specified components if it implements the
/// <see cref="IStartable"/> interface.
/// </summary>
/// <param name="component">The Component to stop.</param>
/// <exception cref="Exception">
/// If there is a problem stoppping component.
/// </exception>
public static void Stop(object component)
{
if (component is IStartable)
{
( (IStartable) component).Stop();
}
}
/// <summary>
/// Disposes specified component if it implements the
/// <see cref="IDisposable"/> interface.
/// </summary>
/// <param name="component">The Component to dispose.</param>
/// <exception cref="Exception">
/// If there is a problem disposing component.
/// </exception>
public static void Dispose(object component)
{
if (component is System.IDisposable)
{
((System.IDisposable) component).Dispose();
}
if (component is Apache.Avalon.Activity.IDisposable)
{
((Apache.Avalon.Activity.IDisposable)
component).Dispose();
}
}
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>