// 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.Composition.Data; using Apache.Avalon.Composition.Model; using Apache.Avalon.Meta; /// /// Summary description for SingletonLifestyleManager. /// public class SingletonLifestyleManager : AbstractLifestyleManager { private WeakReference m_reference; //------------------------------------------------------------------- // constructor //------------------------------------------------------------------- public SingletonLifestyleManager( IComponentModel model, IComponentFactory factory ) : base(model,factory) { } //------------------------------------------------------------------- // Commissionable //------------------------------------------------------------------- /// /// Commission the runtime handler. /// public override void Commission() { if( ComponentModel.ActivationPolicy == ActivationPolicy.Startup ) { RefreshReference(); } } /// /// Invokes the decommissioning phase. Once a handler is /// decommissioned it may be re-commissioned. /// public override void Decommission() { try { if( m_reference != null && m_reference.IsAlive ) { Finalize( m_reference.Target ); m_reference = null; } } catch(Exception) { // Ignores WeakReference exceptions } } //------------------------------------------------------------------- // Resolver //------------------------------------------------------------------- /// /// Resolve a object to a value relative to a supplied set of interface classes. /// /// the resolved object protected override object HandleResolve() { object instance = null; if( m_reference == null ) { return RefreshReference(); } else { instance = m_reference.Target; if( instance == null ) { return RefreshReference(); } else { return instance; } } } /// /// Release an object /// /// the object to be released protected override void HandleRelease( object instance ) { // continue with the current singleton reference } //------------------------------------------------------------------- // LifecycleManager //------------------------------------------------------------------- /// /// Release an object /// /// the object to be released public override void Finalize( object instance ) { ComponentFactory.Etherialize( instance ); m_reference = null; } //------------------------------------------------------------------- // implementation //------------------------------------------------------------------- private Object RefreshReference() { IComponentFactory factory = ComponentFactory; lock( factory ) { Object instance = factory.Incarnate(); m_reference = new WeakReference( instance ); return instance; } } } }