avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hamm...@apache.org
Subject cvs commit: avalon-sandbox/avalon-net/Castle/MicroKernel/Handler/Default DefaultHandler.cs DefaultHandlerFactory.cs SimpleHandler.cs SimpleHandlerFactory.cs
Date Sat, 03 Apr 2004 23:18:17 GMT
hammett     2004/04/03 15:18:17

  Added:       avalon-net/Castle/MicroKernel/Handler AbstractHandler.cs
                        HandlerException.cs IHandler.cs IHandlerFactory.cs
                        State.cs
               avalon-net/Castle/MicroKernel/Handler/Default
                        DefaultHandler.cs DefaultHandlerFactory.cs
                        SimpleHandler.cs SimpleHandlerFactory.cs
  Log:
  MicroKernel for Avalon Castle - Improvements.
  
  Revision  Changes    Path
  1.1                  avalon-sandbox/avalon-net/Castle/MicroKernel/Handler/AbstractHandler.cs
  
  Index: AbstractHandler.cs
  ===================================================================
  // Copyright 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.
  
  namespace Apache.Avalon.Castle.MicroKernel.Handler
  {
  	using System;
  	using System.Collections;
  
  	using Apache.Avalon.Castle.MicroKernel.Model;
  
  	/// <summary>
  	/// Summary description for AbstractHandler.
  	/// </summary>
  	public abstract class AbstractHandler : IHandler
  	{
  		protected Kernel m_kernel;
  
  		protected IComponentModel m_componentModel;
  
  		protected State m_state = State.Valid;
  
  		protected ArrayList m_dependencies = new ArrayList();
  
  		protected Hashtable m_serv2handler = new Hashtable();
  
  		protected ILifestyleManager m_lifestyleManager;
  
  		private ArrayList m_instances = new ArrayList();
  
  		/// <summary>
  		/// 
  		/// </summary>
  		/// <param name="service"></param>
  		/// <param name="implementation"></param>
  		public AbstractHandler( IComponentModel model )
  		{
  			AssertUtil.ArgumentNotNull( model, "model" );
  
  			m_componentModel = model;
  		}
  
  		#region IHandler Members
  
  		public virtual void Init( Kernel kernel )
  		{
  			m_kernel = kernel;
  		}
  
  		public virtual object Resolve()
  		{
  			if (m_state == State.WaitingDependency)
  			{
  				throw new HandlerException("Can't Resolve component. It has dependencies to be satisfied.");
  			}
  
  			try
  			{
  				object instance = m_lifestyleManager.Resolve();
  
  				RegisterInstance( instance );
  
  				return instance;
  			}
  			catch(Exception ex)
  			{
  				throw new HandlerException("Exception while attempting to instantiate type", ex);
  			}
  		}
  
  		public virtual void Release( object instance )
  		{
  			if ( IsOwner(instance) )
  			{
  				UnregisterInstance( instance );
  				m_lifestyleManager.Release( instance );
  			}
  		}
  
  		public virtual bool IsOwner( object instance )
  		{
  			return HasInstance( instance, false );
  		}
  
  		/// <summary>
  		/// 
  		/// </summary>
  		public virtual State ActualState
  		{
  			get
  			{
  				return m_state;
  			}
  		}
  
  		#endregion
  
  		protected virtual void RegisterInstance( object instance )
  		{
  			if (!HasInstance( instance, false ))
  			{
  				WeakReference reference = new WeakReference( instance );
  
  				m_instances.Add( reference );
  			}
  		}
  
  		protected virtual void UnregisterInstance( object instance )
  		{
  			if (m_instances.Count == 0)
  			{
  				return;
  			}
  
  			HasInstance( instance, true );
  		}
  
  		protected virtual bool HasInstance( object instance, bool removeIfFound )
  		{
  			foreach( WeakReference reference in m_instances )
  			{
  				if (reference.Target == null)
  				{
  					m_instances.Remove( reference );
  				}
  
  				if ( Object.ReferenceEquals( instance, reference.Target ) )
  				{
  					if (removeIfFound)
  					{
  						m_instances.Remove( reference );
  					}
  
  					return true;
  				}
  			}
  
  			return false;
  		}
  	}
  }
  
  
  
  1.1                  avalon-sandbox/avalon-net/Castle/MicroKernel/Handler/HandlerException.cs
  
  Index: HandlerException.cs
  ===================================================================
  // Copyright 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.
  
  namespace Apache.Avalon.Castle.MicroKernel.Handler
  {
  	using System;
  
  	/// <summary>
  	/// Summary description for HandlerException.
  	/// </summary>
  	[Serializable]
  	public class HandlerException : System.Exception
  	{
  		public HandlerException( String message ) : base(message)
  		{
  		}
  
  		public HandlerException( String message, Exception ex ) : base(message, ex)
  		{
  		}
  	}
  }
  
  
  
  1.1                  avalon-sandbox/avalon-net/Castle/MicroKernel/Handler/IHandler.cs
  
  Index: IHandler.cs
  ===================================================================
  // Copyright 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.
  
  namespace Apache.Avalon.Castle.MicroKernel
  {
  	using System;
  
  	/// <summary>
  	/// Summary description for IHandler.
  	/// </summary>
  	public interface IHandler : IResolver
  	{
  		/// <summary>
  		/// 
  		/// </summary>
  		/// <param name="kernel"></param>
  		void Init( Kernel kernel );
  
  		/// <summary>
  		/// 
  		/// </summary>
  		State ActualState
  		{
  			get;
  		}
  
  		/// <summary>
  		/// 
  		/// </summary>
  		/// <param name="instance"></param>
  		/// <returns></returns>
  		bool IsOwner( object instance );
  	}
  }
  
  
  
  1.1                  avalon-sandbox/avalon-net/Castle/MicroKernel/Handler/IHandlerFactory.cs
  
  Index: IHandlerFactory.cs
  ===================================================================
  // Copyright 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.
  
  namespace Apache.Avalon.Castle.MicroKernel
  {
  	using System;
  
  	using Apache.Avalon.Castle.MicroKernel.Model;
  
  	/// <summary>
  	/// Summary description for IHandlerFactory.
  	/// </summary>
  	public interface IHandlerFactory
  	{
  		IHandler CreateHandler( IComponentModel model );
  	}
  }
  
  
  
  1.1                  avalon-sandbox/avalon-net/Castle/MicroKernel/Handler/State.cs
  
  Index: State.cs
  ===================================================================
  // Copyright 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.
  
  namespace Apache.Avalon.Castle.MicroKernel
  {
  	using System;
  
  	/// <summary>
  	/// Summary description for State.
  	/// </summary>
  	public enum State
  	{
  		/// <summary>
  		/// 
  		/// </summary>
  		Valid,
  		/// <summary>
  		/// 
  		/// </summary>
  		WaitingDependency
  	}
  }
  
  
  
  1.1                  avalon-sandbox/avalon-net/Castle/MicroKernel/Handler/Default/DefaultHandler.cs
  
  Index: DefaultHandler.cs
  ===================================================================
  // Copyright 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.
  
  namespace Apache.Avalon.Castle.MicroKernel.Handler.Default
  {
  	using System;
  
  	using Apache.Avalon.Castle.MicroKernel.Model;
  	using Apache.Avalon.Castle.MicroKernel.Concerns;
  	using Apache.Avalon.Castle.MicroKernel.Factory.Default;
  
  	/// <summary>
  	/// Summary description for DefaultHandler.
  	/// </summary>
  	public class DefaultHandler : SimpleHandler
  	{
  		public DefaultHandler( IComponentModel model ) : base( model )
  		{
  		}
  
  		protected override void CreateComponentFactoryAndLifestyleManager()
  		{
  			IComponentFactory innerFactory = new Factory.Default.SimpleComponentFactory( 
  				m_kernel.GetAspects(AspectPointCutFlags.Before), 
  				m_kernel.GetAspects(AspectPointCutFlags.After), 
  				m_componentModel, m_serv2handler);
  
  			if (m_kernel is AvalonKernel)
  			{
  				AvalonKernel kernel = (AvalonKernel) m_kernel;
  
  				IConcern commissionChain = kernel.Concerns.GetCommissionChain( kernel );
  				IConcern decommissionChain = kernel.Concerns.GetDecommissionChain( kernel );
  
  				ConcernChainComponentFactory factory = 
  					new ConcernChainComponentFactory( 
  						commissionChain, decommissionChain, 
  						m_componentModel, innerFactory );
  
  				innerFactory = factory;
  			}
  
  			m_lifestyleManager = 
  				m_kernel.LifestyleManagerFactory.Create( 
  					innerFactory, m_componentModel );
  		}
  
  	}
  }
  
  
  
  1.1                  avalon-sandbox/avalon-net/Castle/MicroKernel/Handler/Default/DefaultHandlerFactory.cs
  
  Index: DefaultHandlerFactory.cs
  ===================================================================
  // Copyright 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.
  
  namespace Apache.Avalon.Castle.MicroKernel.Handler.Default
  {
  	using System;
  
  	using Apache.Avalon.Castle.MicroKernel.Model;
  
  	/// <summary>
  	/// Summary description for DefaultHandlerFactory.
  	/// </summary>
  	public class DefaultHandlerFactory : IHandlerFactory
  	{
  		public DefaultHandlerFactory()
  		{
  		}
  
  		#region IHandlerFactory Members
  
  		public virtual IHandler CreateHandler( IComponentModel model )
  		{
  			return new DefaultHandler( model );
  		}
  
  		#endregion
  	}
  }
  
  
  
  1.1                  avalon-sandbox/avalon-net/Castle/MicroKernel/Handler/Default/SimpleHandler.cs
  
  Index: SimpleHandler.cs
  ===================================================================
  // Copyright 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.
  
  namespace Apache.Avalon.Castle.MicroKernel.Handler.Default
  {
  	using System;
  	using System.Collections;
  	using System.Reflection;
  
  	using Apache.Avalon.Castle.MicroKernel.Model;
  
  	/// <summary>
  	/// Summary description for SimpleHandler.
  	/// </summary>
  	public class SimpleHandler : AbstractHandler
  	{
  		/// <summary>
  		/// 
  		/// </summary>
  		/// <param name="service"></param>
  		/// <param name="implementation"></param>
  		public SimpleHandler( IComponentModel model ) : base( model )
  		{
  		}
  
  		#region IHandler Members
  
  		public override void Init( Kernel kernel )
  		{
  			base.Init(kernel);
  
  			// Now we check with the kernel if 
  			// we have the necessary implementations 
  			// for the services requested by the constructor
  
  			EnsureDependenciesCanBeSatisfied();
  			CreateComponentFactoryAndLifestyleManager();
  		}
  
  		#endregion
  
  		protected virtual void EnsureDependenciesCanBeSatisfied()
  		{
  			foreach(IDependencyModel dependency in m_componentModel.Dependencies )
  			{
  				AddDependency( dependency.Service );
  			}
  		}
  
  		protected virtual void AddDependency( Type service )
  		{
  			if (m_kernel.HasService( service ))
  			{
  				m_serv2handler[ service ] = m_kernel.GetHandlerForService( service );
  			}
  			else
  			{
  				// This is handler is considered invalid
  				// until dependencies are satisfied
  				m_state = State.WaitingDependency;
  				m_dependencies.Add( service );
  						
  				// Register ourself in the kernel
  				// to be notified if the dependency is satified
  				m_kernel.AddDependencyListener( 
  					service, 
  					new DependencyListenerDelegate(DependencySatisfied) );
  			}
  		}
  
  		/// <summary>
  		/// Delegate implementation invoked by kernel
  		/// when one of registered dependencies were satisfied by 
  		/// new components registered.
  		/// </summary>
  		/// <param name="service"></param>
  		/// <param name="handler"></param>
  		private void DependencySatisfied( Type service, IHandler handler )
  		{
  			m_serv2handler[ service ] = handler;
  
  			m_dependencies.Remove( service );
  
  			if (m_dependencies.Count == 0)
  			{
  				m_state = State.Valid;
  			}
  		}
  
  		protected virtual void CreateComponentFactoryAndLifestyleManager()
  		{
  			IComponentFactory factory = new Factory.Default.SimpleComponentFactory( 
  				m_kernel.GetAspects(AspectPointCutFlags.Before), 
  				m_kernel.GetAspects(AspectPointCutFlags.After), 
  				m_componentModel, m_serv2handler);
  
  			m_lifestyleManager = m_kernel.LifestyleManagerFactory.Create( 
  				factory, m_componentModel );
  		}
  	}
  }
  
  
  
  1.1                  avalon-sandbox/avalon-net/Castle/MicroKernel/Handler/Default/SimpleHandlerFactory.cs
  
  Index: SimpleHandlerFactory.cs
  ===================================================================
  // Copyright 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.
  
  namespace Apache.Avalon.Castle.MicroKernel.Handler.Default
  {
  	using System;
  
  	using Apache.Avalon.Castle.MicroKernel.Model;
  
  	/// <summary>
  	/// Summary description for SimpleHandlerFactory.
  	/// </summary>
  	public class SimpleHandlerFactory : IHandlerFactory
  	{
  		public SimpleHandlerFactory()
  		{
  		}
  
  		#region IHandlerFactory Members
  
  		public virtual IHandler CreateHandler( IComponentModel model )
  		{
  			return new SimpleHandler( model );
  		}
  
  		#endregion
  	}
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org


Mime
View raw message