svn commit: rev 37340 - in avalon/trunk/central/laboratory/avalon-net: Castle/MicroKernel Castle/MicroKernel/Factory Castle/MicroKernel/Handler Castle/MicroKernel/Handler/Default Castle/MicroKernel/MicroKernelTest DynamicProxy Framework/AvalonFramework

[email protected] 1 Sep 2004 14:45:45 -0000
Newsgroups gmane.comp.jakarta.avalon.cvs
Message-ID <[email protected]>
Author: hammett
Date: Wed Sep  1 07:45:44 2004
New Revision: 37340

Modified:
   avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/   (props changed)
   avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/BaseKernel.cs
   avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Factory/IComponentFactory.cs
   avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/AbstractHandler.cs
   avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/Default/DefaultHandler.cs
   avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/Default/DefaultHandlerFactory.cs
   avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/Default/SimpleHandler.cs
   avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/Default/SimpleHandlerFactory.cs
   avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/IHandler.cs
   avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/IHandlerFactory.cs
   avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/IKernel.cs
   avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/IKernelEvents.cs
   avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/MicroKernelTest/AbstractHandlerTestCase.cs
   avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/MicroKernelTest/Apache.Avalon.Castle.MicroKernel.Test.csproj
   avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/MicroKernelTest/BaseKernelTestCase.cs
   avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/MicroKernelTest/KernelEventsTestCase.cs
   avalon/trunk/central/laboratory/avalon-net/DynamicProxy/   (props changed)
   avalon/trunk/central/laboratory/avalon-net/Framework/AvalonFramework/   (props changed)
Log:
Event Infrastructure done. 

Modified: avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/BaseKernel.cs
==============================================================================
--- avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/BaseKernel.cs	(original)
+++ avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/BaseKernel.cs	Wed Sep  1 07:45:44 2004
@@ -14,88 +14,119 @@
 
 namespace Apache.Avalon.Castle.MicroKernel
 {
-	using System;
-	using System.Collections;
+    using System;
+    using System.Collections;
     using System.ComponentModel;
 
-	using Apache.Avalon.Castle.MicroKernel.Model;
-	using Apache.Avalon.Castle.MicroKernel.Subsystems.Lookup.Default;
-
-	/// <summary>
-	/// Base implementation of <see cref="IKernel"/>
-	/// </summary>
-	public class BaseKernel : IKernel, IDisposable
-	{
+    using Apache.Avalon.Framework;
+    using Apache.Avalon.Castle.MicroKernel.Model;
+    using Apache.Avalon.Castle.MicroKernel.Handler.Default;
+    using Apache.Avalon.Castle.MicroKernel.Lifestyle.Default;
+    using Apache.Avalon.Castle.MicroKernel.Model.Default;
+    using Apache.Avalon.Castle.MicroKernel.Interceptor;
+    using Apache.Avalon.Castle.MicroKernel.Interceptor.Default;
+
+    /// <summary>
+    /// Base implementation of <see cref="IKernel"/>
+    /// </summary>
+    public class BaseKernel : IKernel, IDisposable
+    {
         private static readonly object ComponentRegisteredEvent = new object();
-        private static readonly object ComponentInterceptionEvent = new object();
-        private static readonly object ComponentCreatedEvent = new object();
-        private static readonly object ComponentDestroyedEvent = new object();
+        private static readonly object ComponentUnregisteredEvent = new object();
+        private static readonly object ComponentWrapEvent = new object();
+        private static readonly object ComponentUnWrapEvent = new object();
+        private static readonly object ComponentReadyEvent = new object();
+        private static readonly object ComponentReleasedEvent = new object();
+        private static readonly object ComponentModelConstructedEvent = new object();
 
         protected EventHandlerList m_events;
 
         protected IList m_componentsInstances = new ArrayList();
 
-        protected Hashtable m_components;
+        protected Hashtable m_key2Handler;
 
-		protected Hashtable m_services;
+        protected Hashtable m_service2Key;
 
-		protected Hashtable m_subsystems;
+        protected Hashtable m_subsystems;
 
-		protected Hashtable m_dependencyToSatisfy;
+        protected Hashtable m_dependencyToSatisfy;
 
-		protected IHandlerFactory m_handlerFactory;
+        protected IHandlerFactory m_handlerFactory;
 
-		protected IComponentModelBuilder m_componentModelBuilder;
+        protected IComponentModelBuilder m_componentModelBuilder;
 
-		protected ILifestyleManagerFactory m_lifestyleManagerFactory;
+        protected ILifestyleManagerFactory m_lifestyleManagerFactory;
 
-		/// <summary>
-		/// 
-		/// </summary>
-		public BaseKernel()
-		{
+        protected IInterceptedComponentBuilder m_interceptedComponentBuilder;
+
+        /// <summary>
+        /// 
+        /// </summary>
+        public BaseKernel()
+        {
             m_events = new EventHandlerList();
-            m_services = new Hashtable();
-			m_components = new Hashtable(CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
-			m_subsystems = new Hashtable();
-			m_handlerFactory = new Handler.Default.SimpleHandlerFactory();
-			m_dependencyToSatisfy = new Hashtable();
-			m_componentModelBuilder = new Model.Default.DefaultComponentModelBuilder( this );
-			m_lifestyleManagerFactory = new Lifestyle.Default.SimpleLifestyleManagerFactory();
-
-			InitializeSubsystems();
-		}
-
-		#region IKernel Members
-
-		/// <summary>
-		/// Adds a component to kernel.
-		/// </summary>
-		/// <param name="key">The unique key that identifies the component</param>
-		/// <param name="service">The service exposed by this component</param>
-		/// <param name="implementation">The actual implementation</param>
-		public void AddComponent( String key, Type service, Type implementation )
-		{
-			AssertUtil.ArgumentNotNull( key, "key" );
-			AssertUtil.ArgumentNotNull( service, "service" );
-			AssertUtil.ArgumentNotNull( implementation, "implementation" );
-			AssertUtil.ArgumentMustBeInterface( service, "service" );
-			AssertUtil.ArgumentMustNotBeInterface( implementation, "implementation" );
-			AssertUtil.ArgumentMustNotBeAbstract( implementation, "implementation" );
-			
+            m_key2Handler = new Hashtable(CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
+            m_service2Key = new Hashtable();
+            m_subsystems = new Hashtable();
+            m_handlerFactory = new SimpleHandlerFactory();
+            m_dependencyToSatisfy = new Hashtable();
+            m_componentModelBuilder = new DefaultComponentModelBuilder(this);
+            m_lifestyleManagerFactory = new SimpleLifestyleManagerFactory();
+            m_interceptedComponentBuilder = new DefaultInterceptedComponentBuilder();
+
+            InitializeSubsystems();
+        }
+
+        #region IKernel Members
+
+        /// <summary>
+        /// Adds a component to kernel.
+        /// </summary>
+        /// <param name="key">The unique key that identifies the component</param>
+        /// <param name="service">The service exposed by this component</param>
+        /// <param name="implementation">The actual implementation</param>
+        public void AddComponent(String key, Type service, Type implementation)
+        {
+            AssertUtil.ArgumentNotNull(key, "key");
+            AssertUtil.ArgumentNotNull(service, "service");
+            AssertUtil.ArgumentNotNull(implementation, "implementation");
+            AssertUtil.ArgumentMustBeInterface(service, "service");
+            AssertUtil.ArgumentMustNotBeInterface(implementation, "implementation");
+            AssertUtil.ArgumentMustNotBeAbstract(implementation, "implementation");
+
             if (!service.IsAssignableFrom(implementation))
             {
-				throw new ArgumentException("The specified implementation does not implement the service interface");
-			}
+                throw new ArgumentException("The specified implementation does not implement the service interface");
+            }
 
-			IComponentModel model = ModelBuilder.BuildModel( key, service, implementation );
+            IComponentModel model = ModelBuilder.BuildModel(key, service, implementation);
+            OnModelConstructed( model, key );
 
-			IHandler handler = HandlerFactory.CreateHandler( model );
-			handler.Init ( this );
+            IHandler handler = HandlerFactory.CreateHandler(model);
+            handler.Init(this);
 
-			m_components[ key ] = handler;
+            m_key2Handler[ key ] = handler;
+            OnComponentRegistered(model, key, handler);
+        }
 
-            OnNewHandler( model, key, service, implementation, handler);
+        /// <summary>
+        /// Pending.
+        /// </summary>
+        /// <param name="key"></param>
+        public void RemoveComponent(String key)
+        {
+            AssertUtil.ArgumentNotNull(key, "key");
+
+            IHandler handler = this[key];
+
+            if ( handler != null )
+            {
+                OnComponentUnregistered(handler.ComponentModel, key, handler);
+
+                m_key2Handler.Remove( key );
+
+                HandlerFactory.ReleaseHandler( handler );
+            }
         }
 
         /// <summary>
@@ -112,166 +143,201 @@
         /// Pending
         /// </summary>
         /// <value></value>
-        public event InterceptionDelegate ComponentInterception
+        public event ComponentDataDelegate ComponentUnregistered
         {
-            add { m_events.AddHandler(ComponentInterceptionEvent, value); }
-            remove { m_events.RemoveHandler(ComponentInterceptionEvent, value); }
+            add { m_events.AddHandler(ComponentUnregisteredEvent, value); }
+            remove { m_events.RemoveHandler(ComponentUnregisteredEvent, value); }
         }
 
         /// <summary>
         /// Pending
         /// </summary>
         /// <value></value>
-        public event ComponentInstanceDelegate ComponentCreated
+        public event WrapDelegate ComponentWrap
         {
-            add { m_events.AddHandler(ComponentCreatedEvent, value); }
-            remove { m_events.RemoveHandler(ComponentCreatedEvent, value); }
+            add { m_events.AddHandler(ComponentWrapEvent, value); }
+            remove { m_events.RemoveHandler(ComponentWrapEvent, value); }
         }
 
         /// <summary>
         /// Pending
         /// </summary>
         /// <value></value>
-        public event ComponentInstanceDelegate ComponentDestroyed
+        public event UnWrapDelegate ComponentUnWrap
+        {
+            add { m_events.AddHandler(ComponentUnWrapEvent, value); }
+            remove { m_events.RemoveHandler(ComponentUnWrapEvent, value); }
+        }
+
+        /// <summary>
+        /// Pending
+        /// </summary>
+        /// <value></value>
+        public event ComponentInstanceDelegate ComponentReady
+        {
+            add { m_events.AddHandler(ComponentReadyEvent, value); }
+            remove { m_events.RemoveHandler(ComponentReadyEvent, value); }
+        }
+
+        /// <summary>
+        /// Pending
+        /// </summary>
+        /// <value></value>
+        public event ComponentInstanceDelegate ComponentReleased
+        {
+            add { m_events.AddHandler(ComponentReleasedEvent, value); }
+            remove { m_events.RemoveHandler(ComponentReleasedEvent, value); }
+        }
+
+        /// <summary>
+        /// Pending
+        /// </summary>
+        /// <value></value>
+        public event ComponentModelDelegate ComponentModelConstructed
+        {
+            add { m_events.AddHandler(ComponentModelConstructedEvent, value); }
+            remove { m_events.RemoveHandler(ComponentModelConstructedEvent, value); }
+        }
+
+        /// <summary>
+        /// Adds a subsystem.
+        /// </summary>
+        /// <param name="key">Name of this subsystem</param>
+        /// <param name="system">Subsystem implementation</param>
+        public void AddSubsystem(String key, IKernelSubsystem system)
+        {
+            AssertUtil.ArgumentNotNull(key, "key");
+            AssertUtil.ArgumentNotNull(system, "system");
+
+            system.Init(this);
+
+            m_subsystems[ key ] = system;
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        public IHandler this[String key]
+        {
+            get { return (IHandler) m_key2Handler[ key ]; }
+        }
+
+        public IHandler GetHandler(String key, object criteria)
+        {
+            // TODO: IHandler GetHandler( String key, object criteria )
+            return null;
+        }
+
+        public IHandlerFactory HandlerFactory
+        {
+            get { return m_handlerFactory; }
+            set
+            {
+                AssertUtil.ArgumentNotNull(value, "value");
+                m_handlerFactory = value;
+            }
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        public ILifestyleManagerFactory LifestyleManagerFactory
+        {
+            get { return m_lifestyleManagerFactory; }
+            set
+            {
+                AssertUtil.ArgumentNotNull(value, "value");
+                m_lifestyleManagerFactory = value;
+            }
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        public IInterceptedComponentBuilder InterceptedComponentBuilder
+        {
+            get { return m_interceptedComponentBuilder; }
+            set
+            {
+                AssertUtil.ArgumentNotNull(value, "value");
+                m_interceptedComponentBuilder = value;
+            }
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        public IComponentModelBuilder ModelBuilder
+        {
+            get { return m_componentModelBuilder; }
+            set
+            {
+                AssertUtil.ArgumentNotNull(value, "value");
+                m_componentModelBuilder = value;
+            }
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="service"></param>
+        /// <returns></returns>
+        public bool HasService(Type service)
+        {
+            return m_service2Key.Contains(service);
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="service"></param>
+        /// <param name="depDelegate"></param>
+        public void AddDependencyListener(Type service, DependencyListenerDelegate depDelegate)
         {
-            add { m_events.AddHandler(ComponentDestroyedEvent, value); }
-            remove { m_events.RemoveHandler(ComponentDestroyedEvent, value); }
+            lock (m_dependencyToSatisfy)
+            {
+                Delegate del = m_dependencyToSatisfy[ service ] as Delegate;
+
+                if (del == null)
+                {
+                    m_dependencyToSatisfy[ service ] = depDelegate;
+                }
+                else
+                {
+                    del = Delegate.Combine(del, depDelegate);
+                    m_dependencyToSatisfy[ service ] = del;
+                }
+            }
         }
 
         /// <summary>
-		/// Adds a subsystem.
-		/// </summary>
-		/// <param name="key">Name of this subsystem</param>
-		/// <param name="system">Subsystem implementation</param>
-		public void AddSubsystem( String key, IKernelSubsystem system )
-		{
-			AssertUtil.ArgumentNotNull( key, "key" );
-			AssertUtil.ArgumentNotNull( system, "system" );
-
-			system.Init( this );
-
-			m_subsystems[ key ] = system;
-		}
-
-		/// <summary>
-		/// 
-		/// </summary>
-		public IHandler this [ String key ]
-		{
-			get
-			{
-				return (IHandler) m_components[ key ];
-			}
-		}
-
-		public IHandler GetHandler( String key, object criteria )
-		{
-			// TODO: IHandler GetHandler( String key, object criteria )
-			return null;
-		}
-
-		public IHandlerFactory HandlerFactory
-		{
-			get
-			{
-				return m_handlerFactory;
-			}
-			set
-			{
-				AssertUtil.ArgumentNotNull( value, "value" );
-				m_handlerFactory = value;
-			}
-		}
-
-		/// <summary>
-		/// 
-		/// </summary>
-		public ILifestyleManagerFactory LifestyleManagerFactory
-		{
-			get
-			{
-				return m_lifestyleManagerFactory;
-			}
-			set
-			{
-				AssertUtil.ArgumentNotNull( value, "value" );
-				m_lifestyleManagerFactory = value;
-			}
-		}
-
-		public IComponentModelBuilder ModelBuilder
-		{
-			get
-			{
-				return m_componentModelBuilder;
-			}
-			set
-			{
-				AssertUtil.ArgumentNotNull( value, "value" );
-				m_componentModelBuilder = value;
-			}
-		}
-
-		/// <summary>
-		/// 
-		/// </summary>
-		/// <param name="service"></param>
-		/// <returns></returns>
-		public bool HasService( Type service )
-		{
-			return m_services.Contains( service );
-		}
-
-		/// <summary>
-		/// 
-		/// </summary>
-		/// <param name="service"></param>
-		/// <param name="depDelegate"></param>
-		public void AddDependencyListener( Type service, DependencyListenerDelegate depDelegate )
-		{
-			lock(m_dependencyToSatisfy)
-			{
-				Delegate del = m_dependencyToSatisfy[ service ] as Delegate;
-
-				if (del == null)
-				{
-					m_dependencyToSatisfy[ service ] = depDelegate;
-				}
-				else
-				{
-					del = Delegate.Combine( del, depDelegate );
-					m_dependencyToSatisfy[ service ] = del;
-				}
-			}
-		}
-
-		/// <summary>
-		/// 
-		/// </summary>
-		/// <param name="service"></param>
-		/// <returns></returns>
-		public IHandler GetHandlerForService( Type service )
-		{
-			return (IHandler) m_services[ service ];
-		}
-
-		/// <summary>
-		/// Returns a registered subsystem;
-		/// </summary>
-		/// <param name="key">Key used when registered subsystem</param>
-		/// <returns>Subsystem implementation</returns>
-		public IKernelSubsystem GetSubsystem( String key )
-		{
-			return (IKernelSubsystem) m_subsystems[ key ];
-		}
+        /// 
+        /// </summary>
+        /// <param name="service"></param>
+        /// <returns></returns>
+        public IHandler GetHandlerForService(Type service)
+        {
+            String key = (String) m_service2Key[ service ];
+            return key == null ? null : (IHandler) m_key2Handler[ key ];
+        }
+
+        /// <summary>
+        /// Returns a registered subsystem;
+        /// </summary>
+        /// <param name="key">Key used when registered subsystem</param>
+        /// <returns>Subsystem implementation</returns>
+        public IKernelSubsystem GetSubsystem(String key)
+        {
+            return (IKernelSubsystem) m_subsystems[ key ];
+        }
 
-		#endregion
+        #endregion
 
         #region IDisposable Members
 
         public void Dispose()
         {
-            foreach (PairHandlerComponent pair in m_componentsInstances)
+            foreach(PairHandlerComponent pair in m_componentsInstances)
             {
                 pair.Handler.Release(pair.Instance);
             }
@@ -282,45 +348,180 @@
 
         /// <summary>
         /// 
-		/// </summary>
-		protected virtual void InitializeSubsystems()
-		{
+        /// </summary>
+        protected virtual void InitializeSubsystems()
+        {
             // Examples:
-			// AddSubsystem( KernelConstants.LOOKUP, new LookupCriteriaMatcher() );
-			// AddSubsystem( KernelConstants.EVENTS, new EventManager() );
-		}
+            // AddSubsystem( KernelConstants.LOOKUP, new LookupCriteriaMatcher() );
+            // AddSubsystem( KernelConstants.EVENTS, new EventManager() );
+        }
 
-        protected virtual void RaiseComponentAdded(IComponentModel model, String key, IHandler handler)
+        #region RaiseEvents 
+
+        protected virtual void RaiseComponentRegistered(IComponentModel model, String key, IHandler handler)
         {
             ComponentDataDelegate eventDelegate = (ComponentDataDelegate) m_events[ComponentRegisteredEvent];
-            
+
+            if (eventDelegate != null)
+            {
+                eventDelegate(model, key, handler);
+            }
+        }
+
+        protected virtual void RaiseComponentUnregistered(IComponentModel model, String key, IHandler handler)
+        {
+            ComponentDataDelegate eventDelegate = (ComponentDataDelegate) m_events[ComponentUnregisteredEvent];
+
             if (eventDelegate != null)
             {
                 eventDelegate(model, key, handler);
             }
         }
 
-        private void OnNewHandler( IComponentModel model, String key, Type service, Type implementation, IHandler handler )
-		{
-			m_services[ service ] = handler;
+        protected virtual void RaiseModelConstructed(IComponentModel model, String key)
+        {
+            ComponentModelDelegate eventDelegate = (ComponentModelDelegate) m_events[ComponentModelConstructedEvent];
+
+            if (eventDelegate != null)
+            {
+                eventDelegate(model, key);
+            }
+        }
+
+        public virtual void RaiseComponentReadyEvent( IHandler handler, object instance )
+        {
+            ComponentInstanceDelegate eventDelegate = (ComponentInstanceDelegate) m_events[ComponentReadyEvent];
+
+            if (eventDelegate != null)
+            {
+                IComponentModel model = handler.ComponentModel;
+                String key = (String) m_service2Key[ model.Service ];
+
+                eventDelegate(model, key, handler, instance);
+            }
+        }
+
+        public virtual void RaiseComponentReleasedEvent( IHandler handler, object instance )
+        {
+            ComponentInstanceDelegate eventDelegate = (ComponentInstanceDelegate) m_events[ComponentReleasedEvent];
 
-            RaiseComponentAdded( model, key, handler );
+            if (eventDelegate != null)
+            {
+                IComponentModel model = handler.ComponentModel;
+                String key = (String) m_service2Key[ model.Service ];
 
-            if (model.ActivationPolicy == Apache.Avalon.Framework.Activation.Start)
+                eventDelegate(model, key, handler, instance);
+            }
+        }
+
+        public virtual object RaiseWrapEvent( IHandler handler, object instance )
+        {
+            WrapDelegate eventDelegate = (WrapDelegate) m_events[ComponentWrapEvent];
+
+            if (eventDelegate != null)
+            {
+                IComponentModel model = handler.ComponentModel;
+                String key = (String) m_service2Key[ model.Service ];
+                IInterceptedComponent wrapper = new InterceptedComponentWrapper( m_interceptedComponentBuilder /*, instance, model.Service*/ );
+
+                eventDelegate(model, key, handler, wrapper);
+            }
+
+            return instance;
+        }
+
+        public virtual object RaiseUnWrapEvent( IHandler handler, object instance )
+        {
+            UnWrapDelegate eventDelegate = (UnWrapDelegate) m_events[ComponentUnWrapEvent];
+
+            if (eventDelegate != null)
+            {
+                IComponentModel model = handler.ComponentModel;
+                String key = (String) m_service2Key[ model.Service ];
+
+                instance = eventDelegate(model, key, handler, instance);
+            }
+
+            return instance;
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        internal class InterceptedComponentWrapper : IInterceptedComponent
+        {
+            private IInterceptedComponentBuilder m_interceptedComponentBuilder;
+
+            public InterceptedComponentWrapper( IInterceptedComponentBuilder interceptedComponentBuilder )
+            {
+                m_interceptedComponentBuilder = interceptedComponentBuilder;
+            }
+
+            public object ProxiedInstance
+            {
+                get { throw new NotImplementedException(); }
+            }
+
+            public void Add(IInterceptor interceptor)
+            {
+                throw new NotImplementedException();
+            }
+
+            public IInterceptor InterceptorChain
+            {
+                get { throw new NotImplementedException(); }
+            }
+        }
+
+        #endregion
+
+        /// <summary>
+        /// Starts the component if the activation policy for 
+        /// the component is 'Start'
+        /// </summary>
+        /// <param name="model">Component model</param>
+        /// <param name="handler">Handler responsible for the component</param>
+        protected virtual void StartComponent(IComponentModel model, IHandler handler)
+        {
+            if (model.ActivationPolicy == Activation.Start)
             {
                 object instance = handler.Resolve();
-                
-                m_componentsInstances.Add( new PairHandlerComponent(handler, instance) );
+
+                m_componentsInstances.Add(new PairHandlerComponent(handler, instance));
             }
         }
-	}
 
+        private void OnModelConstructed(IComponentModel model, String key)
+        {
+            RaiseModelConstructed(model, key);
+        }
+
+        private void OnComponentRegistered(IComponentModel model, String key, IHandler handler)
+        {
+            m_service2Key[ model.Service ] = key;
+
+            RaiseComponentRegistered(model, key, handler);
+
+            StartComponent( model, handler );
+        }
+
+        private void OnComponentUnregistered(IComponentModel model, String key, IHandler handler)
+        {
+            m_service2Key.Remove( model.Service );
+
+            RaiseComponentUnregistered(model, key, handler);
+        }
+    }
+
+    /// <summary>
+    /// 
+    /// </summary>
     internal class PairHandlerComponent
     {
         private IHandler m_handler;
         private object m_instance;
 
-        public PairHandlerComponent( IHandler handler, object instance )
+        public PairHandlerComponent(IHandler handler, object instance)
         {
             m_handler = handler;
             m_instance = instance;
@@ -336,4 +537,4 @@
             get { return m_instance; }
         }
     }
-}
+}
\ No newline at end of file

Modified: avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Factory/IComponentFactory.cs
==============================================================================
--- avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Factory/IComponentFactory.cs	(original)
+++ avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Factory/IComponentFactory.cs	Wed Sep  1 07:45:44 2004
@@ -14,15 +14,12 @@
 
 namespace Apache.Avalon.Castle.MicroKernel
 {
-	using System;
-	using System.Reflection;
-
 	/// <summary>
 	/// Summary description for IComponentFactory.
 	/// </summary>
 	public interface IComponentFactory
 	{
-		Object Incarnate( );
+		object Incarnate( );
 
 		void Etherialize( object instance );
 	}

Modified: avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/AbstractHandler.cs
==============================================================================
--- avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/AbstractHandler.cs	(original)
+++ avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/AbstractHandler.cs	Wed Sep  1 07:45:44 2004
@@ -1,182 +1,182 @@
-// 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 IKernel 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="model"></param>
-		public AbstractHandler( IComponentModel model )
-		{
-			AssertUtil.ArgumentNotNull( model, "model" );
-
-			m_componentModel = model;
-		}
-
-		#region IHandler Members
-
-		public virtual void Init( IKernel kernel )
-		{
-			m_kernel = kernel;
-		}
-
-		public virtual object Resolve()
-		{
-			if (ActualState == State.WaitingDependency)
-			{
-				throw new HandlerException("Can't Resolve component. It has dependencies to be satisfied.");
-			}
-
-			try
-			{
-				object instance = m_lifestyleManager.Resolve();
-
-                // TODO: Proxy
-                // instance = RaiseInterceptorEvent( instance );
-
-                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 )
-		{
-			RaiseComponentCreatedEvent( instance );
-
-			if (!HasInstance( instance, false ))
-			{
-				// WeakReference reference = new WeakReference( instance );
-				// m_instances.Add( reference );
-				m_instances.Add( instance );
-			}
-		}
-
-		protected virtual void UnregisterInstance( object instance )
-		{
-            RaiseComponentDestroyedEvent( instance );
-
-            if (m_instances.Count == 0)
-			{
-				return;
-			}
-
-			HasInstance( instance, true );
-		}
-
-		protected virtual bool HasInstance( object instance, bool removeIfFound )
-		{
-			// foreach( WeakReference reference in m_instances )
-			foreach( object storedInstance in m_instances )
-			{
-				// if (reference.Target == null)
-				// {
-				//	m_instances.Remove( reference );
-				// }
-
-				if ( Object.ReferenceEquals( instance, storedInstance /*reference.Target*/ ) )
-				{
-					if (removeIfFound)
-					{
-						m_instances.Remove( instance );
-					}
-
-					return true;
-				}
-			}
-
-			return false;
-		}
-
-		protected virtual void RaiseComponentCreatedEvent( object instance )
-		{
-            ComponentInstanceDelegate createdEvent = m_kernel.ComponentCreated;
-            
-            if (createdEvent == null)
+ // 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 IKernel m_kernel;
+
+        protected IComponentModel m_componentModel;
+
+        protected State m_state = State.Valid;
+
+        protected IList m_dependencies = new ArrayList();
+
+        protected Hashtable m_serv2handler = new Hashtable();
+
+        protected ILifestyleManager m_lifestyleManager;
+
+        private IList m_instances = new ArrayList();
+
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="model"></param>
+        public AbstractHandler(IComponentModel model)
+        {
+            AssertUtil.ArgumentNotNull(model, "model");
+
+            m_componentModel = model;
+        }
+
+        #region IHandler Members
+
+        public virtual void Init(IKernel kernel)
+        {
+            m_kernel = kernel;
+        }
+
+        public virtual IComponentModel ComponentModel
+        {
+            get { return m_componentModel; }
+        }
+
+        public virtual object Resolve()
+        {
+            if (ActualState == State.WaitingDependency)
             {
-                return;
+                throw new HandlerException(
+                    "Can't Resolve component. " +
+                        "It has dependencies which still to be satisfied.");
             }
 
-            createdEvent( m_componentModel, this, instance );
+            try
+            {
+                object instance = m_lifestyleManager.Resolve();
+
+                instance = RaiseWrapEvent(instance);
+
+                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))
+            {
+                instance = RaiseUnWrapEvent(instance);
+                UnregisterInstance(instance);
+                m_lifestyleManager.Release(instance);
+            }
         }
 
-        protected virtual void RaiseComponentDestroyedEvent(object instance)
+        public virtual bool IsOwner(object instance)
         {
-            ComponentInstanceDelegate destroyedEvent = m_kernel.ComponentDestroyed;
+            return HasInstance(instance, false);
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        public virtual State ActualState
+        {
+            get { return m_state; }
+        }
 
-            if (destroyedEvent == null)
+        #endregion
+
+        protected virtual void RegisterInstance(object instance)
+        {
+            if (!HasInstance(instance, false))
+            {
+                // WeakReference reference = new WeakReference( instance );
+                // m_instances.Add( reference );
+                m_instances.Add(instance);
+            }
+
+            RaiseComponentReadyEvent(instance);
+        }
+
+        protected virtual void UnregisterInstance(object instance)
+        {
+            RaiseComponentReleasedEvent(instance);
+
+            if (m_instances.Count == 0)
             {
                 return;
             }
 
-            destroyedEvent(m_componentModel, this, instance);
+            HasInstance(instance, true);
+        }
+
+        protected virtual bool HasInstance(object instance, bool removeIfFound)
+        {
+            // foreach( WeakReference reference in m_instances )
+            foreach(object storedInstance in m_instances)
+            {
+                // if (reference.Target == null)
+                // {
+                //	m_instances.Remove( reference );
+                // }
+
+                if (Object.ReferenceEquals(instance, storedInstance /*reference.Target*/))
+                {
+                    if (removeIfFound)
+                    {
+                        m_instances.Remove(instance);
+                    }
+
+                    return true;
+                }
+            }
+
+            return false;
+        }
+
+        protected virtual object RaiseWrapEvent(object instance)
+        {
+            return m_kernel.RaiseWrapEvent(this, instance);
+        }
+
+        protected virtual object RaiseUnWrapEvent(object instance)
+        {
+            return m_kernel.RaiseUnWrapEvent(this, instance);
+        }
+
+        protected virtual void RaiseComponentReadyEvent(object instance)
+        {
+            m_kernel.RaiseComponentReadyEvent(this, instance);
+        }
+
+        protected virtual void RaiseComponentReleasedEvent(object instance)
+        {
+            m_kernel.RaiseComponentReleasedEvent(this, instance);
         }
-    }
-}
+    }
+}
\ No newline at end of file

Modified: avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/Default/DefaultHandler.cs
==============================================================================
--- avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/Default/DefaultHandler.cs	(original)
+++ avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/Default/DefaultHandler.cs	Wed Sep  1 07:45:44 2004
@@ -14,8 +14,6 @@
 
 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;

Modified: avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/Default/DefaultHandlerFactory.cs
==============================================================================
--- avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/Default/DefaultHandlerFactory.cs	(original)
+++ avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/Default/DefaultHandlerFactory.cs	Wed Sep  1 07:45:44 2004
@@ -34,6 +34,10 @@
 			return new DefaultHandler( model );
 		}
 
+        public virtual void ReleaseHandler( IHandler handler )
+        {
+        }
+
 		#endregion
 	}
 }

Modified: avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/Default/SimpleHandler.cs
==============================================================================
--- avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/Default/SimpleHandler.cs	(original)
+++ avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/Default/SimpleHandler.cs	Wed Sep  1 07:45:44 2004
@@ -15,8 +15,6 @@
 namespace Apache.Avalon.Castle.MicroKernel.Handler.Default
 {
 	using System;
-	using System.Collections;
-	using System.Reflection;
 
 	using Apache.Avalon.Castle.MicroKernel.Model;
 
@@ -28,8 +26,7 @@
 		/// <summary>
 		/// 
 		/// </summary>
-		/// <param name="service"></param>
-		/// <param name="implementation"></param>
+		/// <param name="model"></param>
 		public SimpleHandler( IComponentModel model ) : base( model )
 		{
 		}
@@ -101,8 +98,6 @@
 		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( 

Modified: avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/Default/SimpleHandlerFactory.cs
==============================================================================
--- avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/Default/SimpleHandlerFactory.cs	(original)
+++ avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/Default/SimpleHandlerFactory.cs	Wed Sep  1 07:45:44 2004
@@ -34,6 +34,10 @@
 			return new SimpleHandler( model );
 		}
 
+        public virtual void ReleaseHandler( IHandler handler )
+        {
+        }
+
 		#endregion
 	}
 }

Modified: avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/IHandler.cs
==============================================================================
--- avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/IHandler.cs	(original)
+++ avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/IHandler.cs	Wed Sep  1 07:45:44 2004
@@ -1,4 +1,4 @@
- // Copyright 2004 The Apache Software Foundation
+// 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.
@@ -14,7 +14,7 @@
 
 namespace Apache.Avalon.Castle.MicroKernel
 {
-	using System;
+    using Apache.Avalon.Castle.MicroKernel.Model;
 
 	/// <summary>
 	/// Summary description for IHandler.
@@ -38,5 +38,10 @@
 		/// <param name="instance"></param>
 		/// <returns></returns>
 		bool IsOwner(object instance);
+
+        /// <summary>
+        /// 
+        /// </summary>
+	    IComponentModel ComponentModel { get; }
 	}
 }

Modified: avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/IHandlerFactory.cs
==============================================================================
--- avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/IHandlerFactory.cs	(original)
+++ avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/Handler/IHandlerFactory.cs	Wed Sep  1 07:45:44 2004
@@ -24,5 +24,7 @@
 	public interface IHandlerFactory
 	{
 		IHandler CreateHandler( IComponentModel model );
+
+        void ReleaseHandler( IHandler handler );
 	}
 }

Modified: avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/IKernel.cs
==============================================================================
--- avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/IKernel.cs	(original)
+++ avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/IKernel.cs	Wed Sep  1 07:45:44 2004
@@ -17,6 +17,7 @@
 	using System;
 
 	using Apache.Avalon.Castle.MicroKernel.Model;
+    using Apache.Avalon.Castle.MicroKernel.Interceptor;
 
 	public delegate void DependencyListenerDelegate( Type service, IHandler handler );
 
@@ -33,6 +34,12 @@
 		/// <param name="implementation">The actual implementation</param>
 		void AddComponent(String key, Type service, Type implementation);
 
+		/// <summary>
+		/// 
+		/// </summary>
+		/// <param name="key"></param>
+		void RemoveComponent(String key);
+
         /// <summary>
 		/// IComponentModel instance builder.
 		/// </summary>
@@ -99,5 +106,10 @@
 		/// <param name="key">Key used when registered subsystem</param>
 		/// <returns>Subsystem implementation</returns>
 		IKernelSubsystem GetSubsystem(String key);
+
+        /// <summary>
+        /// 
+        /// </summary>
+        IInterceptedComponentBuilder InterceptedComponentBuilder  { get; set; }
 	}
 }

Modified: avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/IKernelEvents.cs
==============================================================================
--- avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/IKernelEvents.cs	(original)
+++ avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/IKernelEvents.cs	Wed Sep  1 07:45:44 2004
@@ -21,9 +21,13 @@
 
     public delegate void ComponentDataDelegate( IComponentModel model, String key, IHandler handler );
 
-	public delegate void InterceptionDelegate( IComponentModel model, IHandler handler, IInterceptedComponent interceptedComponent );
+	public delegate void WrapDelegate( IComponentModel model, String key, IHandler handler, IInterceptedComponent interceptedComponent );
 
-	public delegate void ComponentInstanceDelegate( IComponentModel model, IHandler handler, object instance );
+    public delegate object UnWrapDelegate( IComponentModel model, String key, IHandler handler, object instance );
+
+	public delegate void ComponentInstanceDelegate( IComponentModel model, String key, IHandler handler, object instance );
+
+	public delegate void ComponentModelDelegate( IComponentModel model, String key );
 
     /// <summary>
     /// 
@@ -35,19 +39,64 @@
         /// </summary>
         event ComponentDataDelegate ComponentRegistered;
 
+		/// <summary>
+		/// 
+		/// </summary>
+		event ComponentDataDelegate ComponentUnregistered;
+
+		/// <summary>
+		/// 
+		/// </summary>
+		event WrapDelegate ComponentWrap;
+
+		/// <summary>
+		/// 
+		/// </summary>
+		event UnWrapDelegate ComponentUnWrap;
+
+		/// <summary>
+		/// 
+		/// </summary>
+		event ComponentInstanceDelegate ComponentReady;
+
+		/// <summary>
+		/// 
+		/// </summary>
+		event ComponentInstanceDelegate ComponentReleased;
+
+		/// <summary>
+		/// 
+		/// </summary>
+		event ComponentModelDelegate ComponentModelConstructed;
+
         /// <summary>
         /// 
         /// </summary>
-        event InterceptionDelegate ComponentInterception;
+        /// <param name="instance"></param>
+        /// <param name="handler"></param>
+        /// <returns></returns>
+        object RaiseWrapEvent( IHandler handler, object instance );
 
         /// <summary>
-		/// 
-		/// </summary>
-        event ComponentInstanceDelegate ComponentCreated;
+        /// 
+        /// </summary>
+        /// <param name="instance"></param>
+        /// <param name="handler"></param>
+        /// <returns></returns>
+        object RaiseUnWrapEvent( IHandler handler, object instance );
 
         /// <summary>
-		/// 
-		/// </summary>
-		event ComponentInstanceDelegate ComponentDestroyed;
-	}
+        /// 
+        /// </summary>
+        /// <param name="instance"></param>
+        /// <param name="handler"></param>
+        void RaiseComponentReadyEvent( IHandler handler, object instance );
+
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="instance"></param>
+        /// <param name="handler"></param>
+        void RaiseComponentReleasedEvent( IHandler handler, object instance );
+    }
 }

Modified: avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/MicroKernelTest/AbstractHandlerTestCase.cs
==============================================================================
--- avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/MicroKernelTest/AbstractHandlerTestCase.cs	(original)
+++ avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/MicroKernelTest/AbstractHandlerTestCase.cs	Wed Sep  1 07:45:44 2004
@@ -94,7 +94,7 @@
 			public override object Resolve()
 			{
 				object instance = new object();
-				base.RegisterInstance( ref instance );
+				base.RegisterInstance( instance );
 				return instance;
 			}
 		
@@ -139,7 +139,7 @@
 
 			public override object Resolve()
 			{
-				base.RegisterInstance( ref instance );
+				base.RegisterInstance( instance );
 				return instance;
 			}
 		

Modified: avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/MicroKernelTest/Apache.Avalon.Castle.MicroKernel.Test.csproj
==============================================================================
--- avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/MicroKernelTest/Apache.Avalon.Castle.MicroKernel.Test.csproj	(original)
+++ avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/MicroKernelTest/Apache.Avalon.Castle.MicroKernel.Test.csproj	Wed Sep  1 07:45:44 2004
@@ -144,6 +144,11 @@
                     BuildAction = "Compile"
                 />
                 <File
+                    RelPath = "KernelEventsTestCase.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
                     RelPath = "LoggerManagerTestCase.cs"
                     SubType = "Code"
                     BuildAction = "Compile"

Modified: avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/MicroKernelTest/BaseKernelTestCase.cs
==============================================================================
--- avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/MicroKernelTest/BaseKernelTestCase.cs	(original)
+++ avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/MicroKernelTest/BaseKernelTestCase.cs	Wed Sep  1 07:45:44 2004
@@ -14,10 +14,6 @@
 
 namespace Apache.Avalon.Castle.MicroKernel.Test
 {
-	using System;
-	using System.Reflection;
-	using System.Collections;
-
 	using NUnit.Framework;
 
 	using Apache.Avalon.Castle.MicroKernel;
@@ -49,6 +45,24 @@
 
 			handler.Release( service );
 		}
+
+        [Test]
+        public void AddAndRemove()
+        {
+            BaseKernel container = new BaseKernel();
+            container.AddComponent( "a", typeof(IMailService), typeof(SimpleMailService) );
+
+            AssertNotNull( container.GetHandlerForService( typeof(IMailService) ) );
+            AssertNotNull( container[ "a" ] );
+
+            AssertNull( container.GetHandlerForService( typeof(IMailMarketingService) ) );
+            AssertNull( container[ "b" ] );
+
+            container.RemoveComponent( "a" );
+
+            AssertNull( container.GetHandlerForService( typeof(IMailService) ) );
+            AssertNull( container[ "a" ] );
+        }
 
 		[Test]
 		public void ComponentDependingOnLogger()

Modified: avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/MicroKernelTest/KernelEventsTestCase.cs
==============================================================================
--- avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/MicroKernelTest/KernelEventsTestCase.cs	(original)
+++ avalon/trunk/central/laboratory/avalon-net/Castle/MicroKernel/MicroKernelTest/KernelEventsTestCase.cs	Wed Sep  1 07:45:44 2004
@@ -14,89 +14,197 @@
 
 namespace Apache.Avalon.Castle.MicroKernel.Test
 {
-	using System;
+    using System;
 
-	using NUnit.Framework;
+    using NUnit.Framework;
 
-	using Apache.Avalon.Castle.MicroKernel;
+    using Apache.Avalon.Castle.MicroKernel;
     using Apache.Avalon.Castle.MicroKernel.Model;
-    using Apache.Avalon.Castle.MicroKernel.Test.Components;
+    using Apache.Avalon.Castle.MicroKernel.Test.Components;
 
 	/// <summary>
-    /// Summary description for KernelEventsTestCase.
-    /// </summary>
+	/// Summary description for KernelEventsTestCase.
+	/// </summary>
 	[TestFixture]
-	public class KernelEventsTestCase : Assertion
+    public class KernelEventsTestCase : Assertion
 	{
-		private bool m_componentAdded = false;
-		private bool m_componentCreated = false;
-		private bool m_componentDestroyed = false;
-
-		[Test]
-		public void TestComponentAddedEvent()
-		{
-			IKernel kernel = new DefaultAvalonKernel();
-
-            kernel.ComponentAdded += new ComponentDataDelegate(OnComponentAdded);
-
-            kernel.AddComponent( "key", typeof(IMailService), typeof(SimpleMailService) );
-
-			Assert( m_componentAdded );
-		}
-
-        public void OnComponentAdded(IComponentModel model, String key, Type service, Type implementation, IHandler handler)
+        private bool m_registered = false;
+        private bool m_unregistered = false;
+        private bool m_modelConstructed = false;
+        private bool m_wrap = false;
+        private bool m_unwrap= false;
+        private bool m_ready = false;
+        private bool m_released = false;
+
+        [Test]
+        public void RegisteredAndUnregistered()
+        {
+            BaseKernel container = new BaseKernel();
+
+            container.ComponentRegistered += new ComponentDataDelegate(Registered);
+            container.ComponentUnregistered += new ComponentDataDelegate(Unregistered);
+
+            Assert(!m_registered);
+            Assert(!m_unregistered);
+
+            container.AddComponent( "a", typeof(IMailService), typeof(SimpleMailService) );
+
+            Assert(m_registered);
+            Assert(!m_unregistered);
+
+            m_registered = false;
+
+            container.RemoveComponent( "a" );
+
+            Assert(!m_registered);
+            Assert(m_unregistered);
+        }
+
+        [Test]
+        public void ModelConstructed()
+        {
+            BaseKernel container = new BaseKernel();
+
+            container.ComponentModelConstructed += new ComponentModelDelegate(ComponentModelConstructed);
+
+            Assert(!m_modelConstructed);
+
+            container.AddComponent( "a", typeof(IMailService), typeof(SimpleMailService) );
+
+            Assert(m_modelConstructed);
+
+            m_modelConstructed = false;
+
+            container.RemoveComponent( "a" );
+
+            Assert(!m_modelConstructed);
+        }
+
+        [Test]
+        public void WrapAndUnwrap()
         {
-			m_componentAdded = true;
-		}
-
-        /*
-		[Test]
-		public void TestComponentCreatedEvent()
-		{
-			IKernel kernel = new DefaultAvalonKernel();
-
-            kernel.ComponentCreated += new KernelDelegate(OnComponentCreated);
+            BaseKernel container = new BaseKernel();
+
+            container.ComponentWrap += new WrapDelegate(ComponentWrap);
+            container.ComponentUnWrap += new UnWrapDelegate(ComponentUnWrap);
+
+            container.AddComponent( "a", typeof(IMailService), typeof(SimpleMailService) );
+
+            Assert(!m_wrap);
+            Assert(!m_unwrap);
+
+            IHandler handler = container[ "a" ];
+
+            IMailService service = handler.Resolve() as IMailService;
+
+            Assert(m_wrap);
+            Assert(!m_unwrap);
+            m_wrap = false;
+
+            service.Send("hammett at apache dot org", "johndoe at yahoo dot org", "Aloha!", "What's up?");
+
+            handler.Release( service );
+
+            Assert(!m_wrap);
+            Assert(m_unwrap);
+        }
+
+        [Test]
+        public void ReadyAndRelease()
+        {
+            BaseKernel container = new BaseKernel();
+
+            container.ComponentReady += new ComponentInstanceDelegate(ComponentReady);
+            container.ComponentReleased += new ComponentInstanceDelegate(ComponentReleased);
+
+            container.AddComponent( "a", typeof(IMailService), typeof(SimpleMailService) );
+
+            Assert(!m_ready);
+            Assert(!m_released);
+
+            IHandler handler = container[ "a" ];
+
+            IMailService service = handler.Resolve() as IMailService;
+
+            Assert(m_ready);
+            Assert(!m_released);
+            m_ready = false;
+
+            service.Send("hammett at apache dot org", "johndoe at yahoo dot org", "Aloha!", "What's up?");
+
+            handler.Release( service );
+
+            Assert(!m_ready);
+            Assert(m_released);
+        }
+
+        private void Registered(IComponentModel model, String key, IHandler handler)
+        {
+            AssertNotNull( model );
+            AssertNotNull( key );
+            AssertNotNull( handler );
+            AssertEquals( "a", key );
 
-            kernel.AddComponent( "key", typeof(IMailService), typeof(SimpleMailService) );
+            m_registered = true;
+        }
 
-			Assert( !m_componentCreated );
+        private void Unregistered(IComponentModel model, String key, IHandler handler)
+        {
+            AssertNotNull( model );
+            AssertNotNull( key );
+            AssertNotNull( handler );
+            AssertEquals( "a", key );
 
-			kernel[ "key" ].Resolve();
+            m_unregistered = true;
+        }
 
-			Assert( m_componentCreated );
-		}
+        private void ComponentModelConstructed(IComponentModel model, String key)
+        {
+            AssertNotNull( model );
+            AssertNotNull( key );
 
-    	public void OnComponentCreated( EventManagerData data )
-		{
-			m_componentCreated = true;
-		}*/
+            m_modelConstructed = true;
+        }
 
-        /*
-		[Test]
-		public void TestComponentDestroyedEvent()
-		{
-			IKernel kernel = new DefaultAvalonKernel();
+        private void ComponentWrap(IComponentModel model, String key, IHandler handler, Apache.Avalon.Castle.MicroKernel.Interceptor.IInterceptedComponent interceptedComponent)
+        {
+            AssertNotNull( model );
+            AssertNotNull( key );
+            AssertNotNull( handler );
 
-			IEventManager eventManager = (IEventManager) kernel.GetSubsystem( KernelConstants.EVENTS );
-			eventManager.ComponentDestroyed += new KernelDelegate(OnComponentDestroyed);
+            m_wrap = true;
+        }
 
-			kernel.AddComponent( "key", typeof(IMailService), typeof(SimpleMailService) );
+        private object ComponentUnWrap(IComponentModel model, String key, IHandler handler, object instance)
+        {
+            AssertNotNull( model );
+            AssertNotNull( key );
+            AssertNotNull( handler );
+            AssertNotNull( instance );
 
-			Assert( !m_componentDestroyed );
+            m_unwrap = true;
 
-			object instance = kernel[ "key" ].Resolve();
+            return instance;
+        }
 
-			Assert( !m_componentDestroyed );
+        private void ComponentReady(IComponentModel model, String key, IHandler handler, object instance)
+        {
+            AssertNotNull( model );
+            AssertNotNull( key );
+            AssertNotNull( handler );
+            AssertNotNull( instance );
 
-			kernel[ "key" ].Release( instance );
+            m_ready = true;
+        }
 
-			Assert( m_componentDestroyed );
-		}
+        private void ComponentReleased(IComponentModel model, String key, IHandler handler, object instance)
+        {
+            AssertNotNull( model );
+            AssertNotNull( key );
+            AssertNotNull( handler );
+            AssertNotNull( instance );
 
-		public void OnComponentDestroyed( EventManagerData data )
-		{
-			m_componentDestroyed = true;
-		}
-        */
+            m_released = true;
+        }
     }
 }