1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.ComponentModel
;
19 using System
.ComponentModel
.Design
;
24 /// <param name="context"></param>
25 public delegate void ExtensionHandler(IRailsEngineContext context
);
28 /// MonoRail's extension manager.
29 /// It fires events related to MonoRail that can be used to add additional behaviour.
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
;
51 /// Initializes a new instance of the <see cref="ExtensionManager"/> class.
53 /// <param name="serviceContainer">The service container.</param>
54 public ExtensionManager(IServiceContainer serviceContainer
)
56 events
= new EventHandlerList();
57 this.serviceContainer
= serviceContainer
;
61 /// Gets the service container.
63 /// <value>The service container.</value>
64 public IServiceContainer ServiceContainer
66 get { return serviceContainer; }
70 /// Occurs when a context is created.
72 public event ExtensionHandler ContextCreated
74 add { events.AddHandler(ContextCreatedEvent, value); }
75 remove { events.RemoveHandler(ContextCreatedEvent, value); }
79 /// Occurs when a context is disposed.
81 public event ExtensionHandler ContextDisposed
83 add { events.AddHandler(ContextDisposedEvent, value); }
84 remove { events.RemoveHandler(ContextDisposedEvent, value); }
88 /// Occurs when an action throws an exception.
90 public event ExtensionHandler ActionException
92 add { events.AddHandler(ActionExceptionEvent, value); }
93 remove { events.RemoveHandler(ActionExceptionEvent, value); }
97 /// Occurs when an unhandled exception is thrown.
99 public event ExtensionHandler UnhandledException
101 add { events.AddHandler(UnhandledExceptionEvent, value); }
102 remove { events.RemoveHandler(UnhandledExceptionEvent, value); }
106 /// Occurs when a session is adquired.
108 public event ExtensionHandler AcquireSessionState
110 add { events.AddHandler(AcquireSessionStateEvent, value); }
111 remove { events.RemoveHandler(AcquireSessionStateEvent, value); }
115 /// Occurs when a session is released.
117 public event ExtensionHandler ReleaseSessionState
119 add { events.AddHandler(ReleaseSessionStateEvent, value); }
120 remove { events.RemoveHandler(ReleaseSessionStateEvent, value); }
124 /// Occurs before pre process a handler.
126 public event ExtensionHandler PreProcess
128 add { events.AddHandler(PreProcessEvent, value); }
129 remove { events.RemoveHandler(PreProcessEvent, value); }
133 /// Occurs after process a handler.
135 public event ExtensionHandler PostProcess
137 add { events.AddHandler(PostProcessEvent, value); }
138 remove { events.RemoveHandler(PostProcessEvent, value); }
142 /// Occurs when a request needs to authenticate.
144 public event ExtensionHandler AuthenticateRequest
146 add { events.AddHandler(AuthenticateRequestEvent, value); }
147 remove { events.RemoveHandler(AuthenticateRequestEvent, value); }
151 /// Occurs when a request needs to be authorized.
153 public event ExtensionHandler AuthorizeRequest
155 add { events.AddHandler(AuthorizeRequestEvent, value); }
156 remove { events.RemoveHandler(AuthorizeRequestEvent, value); }
160 /// Occurs upon request cache resolval.
162 public event ExtensionHandler ResolveRequestCache
164 add { events.AddHandler(ResolveRequestCacheEvent, value); }
165 remove { events.RemoveHandler(ResolveRequestCacheEvent, value); }
169 /// Occurs when a cache need to be updated.
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
);