Minor changes to improve testability of helpers
[castle.git] / MonoRail / Castle.MonoRail.Framework / Extensions / ExtensionManager.cs
blob882c640856dccce94b85771894674921fcdb3c2c
1 // Copyright 2004-2007 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.MonoRail.Framework
17 using System;
18 using System.ComponentModel;
19 using System.ComponentModel.Design;
21 /// <summary>
22 ///
23 /// </summary>
24 /// <param name="context"></param>
25 public delegate void ExtensionHandler(IRailsEngineContext context);
27 /// <summary>
28 /// MonoRail's extension manager.
29 /// It fires events related to MonoRail that can be used to add additional behaviour.
30 /// </summary>
31 public class ExtensionManager : MarshalByRefObject
33 private static readonly object ContextCreatedEvent = new object();
34 private static readonly object ContextDisposedEvent = new object();
35 private static readonly object ActionExceptionEvent = new object();
36 private static readonly object UnhandledExceptionEvent = new object();
37 private static readonly object AcquireSessionStateEvent = new object();
38 private static readonly object ReleaseSessionStateEvent = new object();
39 private static readonly object PreProcessEvent = new object();
40 private static readonly object PostProcessEvent = new object();
41 private static readonly object AuthorizeRequestEvent = new object();
42 private static readonly object AuthenticateRequestEvent = new object();
43 private static readonly object ResolveRequestCacheEvent = new object();
44 private static readonly object UpdateRequestCacheEvent = new object();
46 private EventHandlerList events;
48 private IServiceContainer serviceContainer;
50 /// <summary>
51 /// Initializes a new instance of the <see cref="ExtensionManager"/> class.
52 /// </summary>
53 /// <param name="serviceContainer">The service container.</param>
54 public ExtensionManager(IServiceContainer serviceContainer)
56 events = new EventHandlerList();
57 this.serviceContainer = serviceContainer;
60 /// <summary>
61 /// Gets the service container.
62 /// </summary>
63 /// <value>The service container.</value>
64 public IServiceContainer ServiceContainer
66 get { return serviceContainer; }
69 /// <summary>
70 /// Occurs when a context is created.
71 /// </summary>
72 public event ExtensionHandler ContextCreated
74 add { events.AddHandler(ContextCreatedEvent, value); }
75 remove { events.RemoveHandler(ContextCreatedEvent, value); }
78 /// <summary>
79 /// Occurs when a context is disposed.
80 /// </summary>
81 public event ExtensionHandler ContextDisposed
83 add { events.AddHandler(ContextDisposedEvent, value); }
84 remove { events.RemoveHandler(ContextDisposedEvent, value); }
87 /// <summary>
88 /// Occurs when an action throws an exception.
89 /// </summary>
90 public event ExtensionHandler ActionException
92 add { events.AddHandler(ActionExceptionEvent, value); }
93 remove { events.RemoveHandler(ActionExceptionEvent, value); }
96 /// <summary>
97 /// Occurs when an unhandled exception is thrown.
98 /// </summary>
99 public event ExtensionHandler UnhandledException
101 add { events.AddHandler(UnhandledExceptionEvent, value); }
102 remove { events.RemoveHandler(UnhandledExceptionEvent, value); }
105 /// <summary>
106 /// Occurs when a session is adquired.
107 /// </summary>
108 public event ExtensionHandler AcquireSessionState
110 add { events.AddHandler(AcquireSessionStateEvent, value); }
111 remove { events.RemoveHandler(AcquireSessionStateEvent, value); }
114 /// <summary>
115 /// Occurs when a session is released.
116 /// </summary>
117 public event ExtensionHandler ReleaseSessionState
119 add { events.AddHandler(ReleaseSessionStateEvent, value); }
120 remove { events.RemoveHandler(ReleaseSessionStateEvent, value); }
123 /// <summary>
124 /// Occurs before pre process a handler.
125 /// </summary>
126 public event ExtensionHandler PreProcess
128 add { events.AddHandler(PreProcessEvent, value); }
129 remove { events.RemoveHandler(PreProcessEvent, value); }
132 /// <summary>
133 /// Occurs after process a handler.
134 /// </summary>
135 public event ExtensionHandler PostProcess
137 add { events.AddHandler(PostProcessEvent, value); }
138 remove { events.RemoveHandler(PostProcessEvent, value); }
141 /// <summary>
142 /// Occurs when a request needs to authenticate.
143 /// </summary>
144 public event ExtensionHandler AuthenticateRequest
146 add { events.AddHandler(AuthenticateRequestEvent, value); }
147 remove { events.RemoveHandler(AuthenticateRequestEvent, value); }
150 /// <summary>
151 /// Occurs when a request needs to be authorized.
152 /// </summary>
153 public event ExtensionHandler AuthorizeRequest
155 add { events.AddHandler(AuthorizeRequestEvent, value); }
156 remove { events.RemoveHandler(AuthorizeRequestEvent, value); }
159 /// <summary>
160 /// Occurs upon request cache resolval.
161 /// </summary>
162 public event ExtensionHandler ResolveRequestCache
164 add { events.AddHandler(ResolveRequestCacheEvent, value); }
165 remove { events.RemoveHandler(ResolveRequestCacheEvent, value); }
168 /// <summary>
169 /// Occurs when a cache need to be updated.
170 /// </summary>
171 public event ExtensionHandler UpdateRequestCache
173 add { events.AddHandler(UpdateRequestCacheEvent, value); }
174 remove { events.RemoveHandler(UpdateRequestCacheEvent, value); }
177 internal void RaiseContextCreated(IRailsEngineContext context)
179 ExtensionHandler eventDelegate = (ExtensionHandler) events[ContextCreatedEvent];
180 if (eventDelegate != null) eventDelegate(context);
183 internal void RaiseContextDisposed(IRailsEngineContext context)
185 ExtensionHandler eventDelegate = (ExtensionHandler) events[ContextDisposedEvent];
186 if (eventDelegate != null) eventDelegate(context);
189 internal void RaisePostProcess(IRailsEngineContext context)
191 ExtensionHandler eventDelegate = (ExtensionHandler) events[PostProcessEvent];
192 if (eventDelegate != null) eventDelegate(context);
195 internal void RaisePreProcess(IRailsEngineContext context)
197 ExtensionHandler eventDelegate = (ExtensionHandler) events[PreProcessEvent];
198 if (eventDelegate != null) eventDelegate(context);
201 internal void RaiseReleaseRequestState(IRailsEngineContext context)
203 ExtensionHandler eventDelegate = (ExtensionHandler) events[ReleaseSessionStateEvent];
204 if (eventDelegate != null) eventDelegate(context);
207 internal void RaiseAcquireRequestState(IRailsEngineContext context)
209 ExtensionHandler eventDelegate = (ExtensionHandler) events[AcquireSessionStateEvent];
210 if (eventDelegate != null) eventDelegate(context);
213 internal void RaiseUnhandledError(IRailsEngineContext context)
215 ExtensionHandler eventDelegate = (ExtensionHandler) events[UnhandledExceptionEvent];
216 if (eventDelegate != null) eventDelegate(context);
219 internal void RaiseActionError(IRailsEngineContext context)
221 ExtensionHandler eventDelegate = (ExtensionHandler) events[ActionExceptionEvent];
222 if (eventDelegate != null) eventDelegate(context);
225 internal void RaiseAuthenticateRequest(IRailsEngineContext context)
227 ExtensionHandler eventDelegate = (ExtensionHandler) events[AuthenticateRequestEvent];
228 if (eventDelegate != null) eventDelegate(context);
231 internal void RaiseAuthorizeRequest(IRailsEngineContext context)
233 ExtensionHandler eventDelegate = (ExtensionHandler) events[AuthorizeRequestEvent];
234 if (eventDelegate != null) eventDelegate(context);
237 internal void RaiseResolveRequestCache(IRailsEngineContext context)
239 ExtensionHandler eventDelegate = (ExtensionHandler) events[UpdateRequestCacheEvent];
240 if (eventDelegate != null) eventDelegate(context);
243 internal void RaiseUpdateRequestCache(IRailsEngineContext context)
245 ExtensionHandler eventDelegate = (ExtensionHandler) events[UpdateRequestCacheEvent];
246 if (eventDelegate != null) eventDelegate(context);