// 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.Composition.Model;
using Apache.Avalon.Composition.Model.Default;
///
/// Summary description for DefaultAppliance.
///
public class DefaultAppliance : AbstractAppliance
{
//-------------------------------------------------------------------
// immutable state
//-------------------------------------------------------------------
private IComponentModel m_model;
private ILifestyleManager m_lifestyle;
private DefaultState m_commissioned = new DefaultState();
private long m_delay = 0;
//-------------------------------------------------------------------
// constructor
//-------------------------------------------------------------------
public DefaultAppliance( IComponentModel model, ILifestyleManager lifestyle ) : base(model)
{
m_model = model;
m_lifestyle = lifestyle;
}
//-------------------------------------------------------------------
// Commissionable
//-------------------------------------------------------------------
///
/// Commission the appliance.
///
public override void Commission()
{
lock( m_commissioned )
{
if( m_commissioned.Enabled )
{
return;
}
try
{
m_delay = m_model.DeploymentTimeout;
m_lifestyle.Commission();
m_delay = 0;
m_commissioned.Enabled = true;
}
finally
{
m_delay = 0;
}
}
}
///
/// Decommission the appliance. Once an appliance is
/// decommissioned it may be re-commissioned.
///
public override void Decommission()
{
lock( m_commissioned )
{
if( !m_commissioned.Enabled )
{
return;
}
m_lifestyle.Decommission();
m_commissioned.Enabled = false;
}
}
//-------------------------------------------------------------------
// Resolver
//-------------------------------------------------------------------
///
/// Resolve a object to a value.
///
/// the resolved object
public override object Resolve()
{
if( ComponentModel.TypeDescriptor.Info.GetAttribute( "urn:activation:proxy", "false" ).Equals( "true" ) )
{
return Resolve( true );
}
else
{
return Resolve( false );
}
}
protected virtual object Resolve( bool proxy )
{
if( !proxy )
{
if( m_delay > 0 )
{
String error = "appliance.error.resolve.transient " + this.ToString() + " " + m_delay;
throw new TransientRuntimeException( error, m_delay );
}
else if( !m_commissioned.Enabled )
{
String error = "appliance.error.resolve.non-commission-state " + this.ToString();
throw new ApplicationException( error );
}
else
{
return m_lifestyle.Resolve();
}
}
throw new ArgumentException("proxy not supported");
}
///
/// Release an object
///
/// the object to be released
public override void Release( object instance )
{
if( null == instance ) return;
if( !m_commissioned.Enabled ) return;
m_lifestyle.Release( instance );
}
//-------------------------------------------------------------------
// implementation
//-------------------------------------------------------------------
///
/// Return the model backing the handler.
///
protected IComponentModel ComponentModel
{
get
{
return m_model;
}
}
//-------------------------------------------------------------------
// Object
//-------------------------------------------------------------------
public override String ToString()
{
return "appliance:" + ComponentModel.QualifiedName;
}
}
}