// Copyright 2004 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.
namespace Apache.Avalon.Activation.Default.Lifestyles
{
using System;
using Apache.Avalon.Framework;
using Apache.Avalon.Meta;
using Apache.Avalon.Composition.Model;
///
/// Summary description for AbstractLifestyleManager.
///
public abstract class AbstractLifestyleManager : ILifestyleManager
{
//-------------------------------------------------------------------
// immutable state
//-------------------------------------------------------------------
private IComponentModel m_model;
private IComponentFactory m_factory;
private ILogger m_logger;
// private final ReferenceQueue m_liberals = new ReferenceQueue();
//-------------------------------------------------------------------
// constructor
//-------------------------------------------------------------------
///
/// Creation of a new instance.
///
/// the component model
/// the component factory
public AbstractLifestyleManager( IComponentModel model, IComponentFactory factory )
{
m_factory = factory;
m_model = model;
m_logger = model.Logger;
}
~AbstractLifestyleManager()
{
Decommission();
}
#region ILifestyleManager Members
public virtual void Finalize(object instance)
{
lock( m_factory )
{
if( instance != null )
{
m_factory.Etherialize( instance );
}
}
}
#endregion
#region ICommissionable Members
public abstract void Commission();
public abstract void Decommission();
#endregion
#region IResolver Members
public void Release( object instance )
{
try
{
ApplyExtensionStages( instance, false );
}
catch( Exception e )
{
String error = "Ignoring error returned from release extension.";
Logger.Error( error, e );
}
HandleRelease( instance );
}
public object Resolve()
{
Object instance = HandleResolve();
return ApplyExtensionStages( instance, true );
}
#endregion
//-------------------------------------------------------------------
// implementation
//-------------------------------------------------------------------
protected abstract Object HandleResolve();
protected abstract void HandleRelease( Object instance );
protected ILogger Logger
{
get
{
return m_logger;
}
}
protected IComponentModel ComponentModel
{
get
{
return m_model;
}
}
protected IComponentFactory ComponentFactory
{
get
{
return m_factory;
}
}
private Object ApplyExtensionStages( object instance, bool flag )
{
StageDescriptor[] stages = m_model.TypeDescriptor.Stages;
for( int i=0; i