// 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
{
using System;
using Apache.Avalon.Framework;
using Apache.Avalon.Meta;
using Apache.Avalon.Composition.Model;
///
/// Summary description for DefaultComponentFactory.
///
public class DefaultComponentFactory : IComponentFactory
{
//-------------------------------------------------------------------
// immutable state
//-------------------------------------------------------------------
private ISystemContext m_system;
private IComponentModel m_model;
private ILogger m_logger;
//-------------------------------------------------------------------
// constructor
//-------------------------------------------------------------------
///
/// Creation of a new component factory.
///
/// the system context
/// the component model
public DefaultComponentFactory( ISystemContext system, IComponentModel model )
{
m_system = system;
m_model = model;
m_logger = model.Logger.CreateChildLogger( "lifecycle" );
}
#region IComponentFactory Members
public object Incarnate()
{
Type type = m_model.DeploymentType;
ILogger logger = m_model.Logger;
IConfiguration config = m_model.Configuration;
// Parameters params = m_model.getParameters();
ILookupManager manager = new DefaultLookupManager( m_model );
IContext context = TargetContext;
// Object instance = instantiate(
// clazz, logger, config, params, context, manager );
Object instance = Activator.CreateInstance( type );
try
{
ContainerUtil.EnableLogging( instance, logger );
ApplyContext( instance, context );
ContainerUtil.Service( instance, manager );
ContainerUtil.Configure( instance, config );
// ContainerUtil.parameterize( instance, params );
//
// handle lifecycle extensions
//
ApplyCreateStage( instance, true );
//
// complete intialization
//
ContainerUtil.Initialize( instance );
ContainerUtil.Start( instance );
return instance;
}
catch( Exception e )
{
String error = "lifestyle.error.new " + m_model.QualifiedName;
throw new LifecycleException( error, e );
}
}
public void Etherialize(object instance)
{
if (instance == null)
{
return;
}
try
{
ApplyCreateStage( instance, false );
}
catch( Exception )
{
// will not happen
}
finally
{
try
{
ContainerUtil.Shutdown( instance );
}
catch( Exception e )
{
if( Logger.IsWarnEnabled )
{
String warning = "Ignoring component source shutdown error.";
Logger.Warn( warning, e );
}
}
}
}
#endregion
protected ILogger Logger
{
get
{
return m_logger;
}
}
private IContext TargetContext
{
get
{
IContextModel model = m_model.ContextModel;
if( null == model )
{
return null;
}
return model.Context;
}
}
private void ApplyCreateStage( Object instance, bool flag )
{
StageDescriptor[] stages = m_model.TypeDescriptor.Stages;
if( ( stages.Length > 0 ) && Logger.IsDebugEnabled )
{
Logger.Debug( "stage count: " + stages.Length );
}
for( int i=0; i