Fixing the build, will not add an interceptor twice when it was already added by...
[castle.git] / InversionOfControl / Castle.MicroKernel / KernelEventSupport.cs
blob53ade21499d3ead2a24ba8d9dc6822b431b4b01a
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MicroKernel
17 using System;
18 using System.ComponentModel;
19 using System.Runtime.Serialization;
21 using Castle.Core;
23 /// <summary>
24 /// Summary description for KernelEventSupport.
25 /// </summary>
26 [Serializable]
27 public abstract class KernelEventSupport : MarshalByRefObject, IKernelEvents, ISerializable
29 private static readonly object HandlerRegisteredEvent = new object();
30 private static readonly object ComponentRegisteredEvent = new object();
31 private static readonly object ComponentUnregisteredEvent = new object();
32 private static readonly object ComponentCreatedEvent = new object();
33 private static readonly object ComponentDestroyedEvent = new object();
34 private static readonly object AddedAsChildKernelEvent = new object();
35 private static readonly object ComponentModelCreatedEvent = new object();
36 private static readonly object DependencyResolvingEvent = new object();
37 private static readonly object RemovedAsChildKernelEvent = new object();
39 [NonSerialized]
40 private EventHandlerList events;
42 public KernelEventSupport()
44 events = new EventHandlerList();
47 public KernelEventSupport(SerializationInfo info, StreamingContext context)
49 events = new EventHandlerList();
51 events[HandlerRegisteredEvent] = (Delegate)
52 info.GetValue("HandlerRegisteredEvent", typeof(Delegate));
55 public override object InitializeLifetimeService()
57 return null;
60 /// <summary>
61 /// Pending
62 /// </summary>
63 public event HandlerDelegate HandlerRegistered
65 add { events.AddHandler(HandlerRegisteredEvent, value); }
66 remove { events.RemoveHandler(HandlerRegisteredEvent, value); }
69 /// <summary>
70 /// Pending
71 /// </summary>
72 /// <value></value>
73 public event ComponentDataDelegate ComponentRegistered
75 add { events.AddHandler(ComponentRegisteredEvent, value); }
76 remove { events.RemoveHandler(ComponentRegisteredEvent, value); }
79 /// <summary>
80 /// Pending
81 /// </summary>
82 /// <value></value>
83 public event ComponentDataDelegate ComponentUnregistered
85 add { events.AddHandler(ComponentUnregisteredEvent, value); }
86 remove { events.RemoveHandler(ComponentUnregisteredEvent, value); }
89 /// <summary>
90 /// Pending
91 /// </summary>
92 /// <value></value>
93 public event ComponentInstanceDelegate ComponentCreated
95 add { events.AddHandler(ComponentCreatedEvent, value); }
96 remove { events.RemoveHandler(ComponentCreatedEvent, value); }
99 /// <summary>
100 /// Pending
101 /// </summary>
102 /// <value></value>
103 public event ComponentInstanceDelegate ComponentDestroyed
105 add { events.AddHandler(ComponentDestroyedEvent, value); }
106 remove { events.RemoveHandler(ComponentDestroyedEvent, value); }
109 /// <summary>
110 /// Pending
111 /// </summary>
112 /// <value></value>
113 public event EventHandler AddedAsChildKernel
115 add { events.AddHandler(AddedAsChildKernelEvent, value); }
116 remove { events.RemoveHandler(AddedAsChildKernelEvent, value); }
119 /// <summary>
120 /// Pending
121 /// </summary>
122 public event EventHandler RemovedAsChildKernel
124 add { events.AddHandler(RemovedAsChildKernelEvent, value); }
125 remove { events.RemoveHandler(RemovedAsChildKernelEvent, value); }
128 /// <summary>
129 /// Pending
130 /// </summary>
131 /// <value></value>
132 public event ComponentModelDelegate ComponentModelCreated
134 add { events.AddHandler(ComponentModelCreatedEvent, value); }
135 remove { events.RemoveHandler(ComponentModelCreatedEvent, value); }
138 public event DependencyDelegate DependencyResolving
140 add { events.AddHandler(DependencyResolvingEvent, value); }
141 remove { events.RemoveHandler(DependencyResolvingEvent, value); }
144 protected virtual void RaiseComponentRegistered(String key, IHandler handler)
146 ComponentDataDelegate eventDelegate = (ComponentDataDelegate) events[ComponentRegisteredEvent];
147 if (eventDelegate != null) eventDelegate(key, handler);
151 protected virtual void RaiseComponentUnregistered(String key, IHandler handler)
153 ComponentDataDelegate eventDelegate = (ComponentDataDelegate) events[ComponentUnregisteredEvent];
154 if (eventDelegate != null) eventDelegate(key, handler);
157 public virtual void RaiseComponentCreated(ComponentModel model, object instance)
159 ComponentInstanceDelegate eventDelegate = (ComponentInstanceDelegate) events[ComponentCreatedEvent];
160 if (eventDelegate != null) eventDelegate(model, instance);
163 public virtual void RaiseComponentDestroyed(ComponentModel model, object instance)
165 ComponentInstanceDelegate eventDelegate = (ComponentInstanceDelegate) events[ComponentDestroyedEvent];
166 if (eventDelegate != null) eventDelegate(model, instance);
169 protected virtual void RaiseAddedAsChildKernel()
171 EventHandler eventDelegate = (EventHandler) events[AddedAsChildKernelEvent];
172 if (eventDelegate != null) eventDelegate(this, EventArgs.Empty);
175 protected virtual void RaiseRemovedAsChildKernel()
177 EventHandler eventDelegate = (EventHandler) events[RemovedAsChildKernelEvent];
178 if (eventDelegate != null) eventDelegate(this, EventArgs.Empty);
181 protected virtual void RaiseComponentModelCreated(ComponentModel model)
183 ComponentModelDelegate eventDelegate = (ComponentModelDelegate) events[ComponentModelCreatedEvent];
184 if (eventDelegate != null) eventDelegate(model);
187 public virtual void RaiseHandlerRegistered(IHandler handler)
189 bool stateChanged = true;
191 while(stateChanged)
193 stateChanged = false;
194 HandlerDelegate eventDelegate = (HandlerDelegate) events[HandlerRegisteredEvent];
195 if (eventDelegate != null) eventDelegate(handler, ref stateChanged);
198 protected virtual void RaiseDependencyResolving(ComponentModel client, DependencyModel model, Object dependency)
200 DependencyDelegate eventDelegate = (DependencyDelegate) events[DependencyResolvingEvent];
201 if (eventDelegate != null) eventDelegate(client, model, dependency);
204 #region IDeserializationCallback Members
206 public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
208 info.AddValue("HandlerRegisteredEvent", events[HandlerRegisteredEvent]);
211 #endregion