// Copyright 2003-2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
namespace Apache.Avalon.Framework
{
///
/// Utility class that makes it easier to transfer
/// a component through it's lifecycle stages.
///
public class ContainerUtil
{
///
/// Runs specified object through shutdown lifecycle stages
/// (Stop and Dispose).
///
/// The Component to shutdown.
/// If there is a problem stoppping object.
public static void Shutdown(object component)
{
Stop(component);
Dispose(component);
}
///
/// Supplies specified object with Logger if it implements the
/// interface.
///
/// The component instance
/// The Logger to enable component with.
///
/// If the component is but is null.
///
///
/// The Logger may be null in which case
/// the specified component must not implement .
///
public static void EnableLogging(object component, ILogger logger)
{
if (component is ILogEnabled)
{
if (logger == null)
{
throw new ArgumentNullException("logger is null");
}
((ILogEnabled) component).EnableLogging(logger);
}
}
///
/// Supplies specified object with Context if it implements the
/// interface.
///
/// The component instance
/// The context.
///
/// If the component is but is null.
///
///
///
///
public static void Contextualize(object component, IContext context)
{
if (component is IContextualizable)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
((IContextualizable) component).Contextualize(context);
}
}
///
/// Checks if the specified components supports IStartable
/// or IDisposable interfaces - meaning that it cares about Stop
/// or disposable phases.
///
/// The component instance
/// true if the component wants the shutdown phase.
public static bool ExpectsDispose(object component)
{
return (component is IStartable) || (component is IDisposable);
}
///
/// Supplies specified component with
/// if it implements the interface.
///
/// The Component to service.
///
/// The object to use for component.
///
///
/// If the object is but
/// is null.
///
///
/// If there is a problem servicing component.
///
///
/// The Service manager may be null in
/// which case the specified component must not
/// implement .
///
public static void Service(object component, ILookupManager lookupManager)
{
if (component is ILookupEnabled)
{
if (lookupManager == null)
{
throw new ArgumentNullException("LookupManager is null");
}
((ILookupEnabled) component).EnableLookups(lookupManager);
}
}
///
/// Configures specified component if it implements the
/// interface.
///
/// The Component to configure.
///
/// The Configuration object to use during the configuration.
///
///
/// If the component is but
/// configuration is null.
///
///
/// If there is a problem configuring component,
/// or the component is but configuration is null.
///
///
/// The Configuration may be null in which case
/// the specified component must not implement .
///
public static void Configure(object component, IConfiguration configuration)
{
if (component is IConfigurable)
{
if (configuration == null)
{
throw new ArgumentNullException("configuration is null");
}
((IConfigurable) component).Configure(configuration);
}
}
///
/// Initializes specified component if it implements the
/// interface.
///
///
/// The Component to initialize.
///
///
/// If there is a problem initializing component.
///
public static void Initialize(object component)
{
if (component is IInitializable)
{
( (IInitializable) component).Initialize();
}
}
///
/// Starts specified component if it implements the
/// interface.
///
/// The Component to start.
///
/// If there is a problem starting component.
///
public static void Start(object component)
{
if (component is IStartable)
{
( (IStartable) component).Start();
}
}
///
/// Stops specified components if it implements the
/// interface.
///
/// The Component to stop.
///
/// If there is a problem stoppping component.
///
public static void Stop(object component)
{
if (component is IStartable)
{
( (IStartable) component).Stop();
}
}
///
/// Disposes specified component if it implements the
/// interface.
///
/// The Component to dispose.
///
/// If there is a problem disposing component.
///
public static void Dispose(object component)
{
if (component is IDisposable)
{
((IDisposable) component).Dispose();
}
}
}
}