Merged branch back to the trunk. Build is passing with no changes.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Extensions / ExtensionManager.cs
blob8d8ffcfec8d6f9f8d54147d3379865f3e4ebc0f5
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.Collections.Generic;
19 using System.ComponentModel;
21 /// <summary>
22 ///
23 /// </summary>
24 /// <param name="context"></param>
25 public delegate void ExtensionHandler(IEngineContext 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 ActionExceptionEvent = new object();
34 private static readonly object UnhandledExceptionEvent = new object();
35 private static readonly object AcquireSessionStateEvent = new object();
36 private static readonly object ReleaseSessionStateEvent = new object();
38 private readonly EventHandlerList events;
39 private readonly IMonoRailServices serviceProvider;
40 private readonly List<IMonoRailExtension> extensions = new List<IMonoRailExtension>();
42 /// <summary>
43 /// Initializes a new instance of the <see cref="ExtensionManager"/> class.
44 /// </summary>
45 /// <param name="serviceContainer">The service container.</param>
46 public ExtensionManager(IMonoRailServices serviceContainer)
48 events = new EventHandlerList();
49 serviceProvider = serviceContainer;
52 /// <summary>
53 /// Gets the service container.
54 /// </summary>
55 /// <value>The service container.</value>
56 public IMonoRailServices ServiceProvider
58 get { return serviceProvider; }
61 /// <summary>
62 /// Gets the extensions.
63 /// </summary>
64 /// <value>The extensions.</value>
65 public List<IMonoRailExtension> Extensions
67 get { return extensions; }
70 /// <summary>
71 /// Occurs when an action throws an exception.
72 /// </summary>
73 public event ExtensionHandler ActionException
75 add { events.AddHandler(ActionExceptionEvent, value); }
76 remove { events.RemoveHandler(ActionExceptionEvent, value); }
79 /// <summary>
80 /// Occurs when an unhandled exception is thrown.
81 /// </summary>
82 public event ExtensionHandler UnhandledException
84 add { events.AddHandler(UnhandledExceptionEvent, value); }
85 remove { events.RemoveHandler(UnhandledExceptionEvent, value); }
88 /// <summary>
89 /// Occurs when a session is adquired.
90 /// </summary>
91 public event ExtensionHandler AcquireSessionState
93 add { events.AddHandler(AcquireSessionStateEvent, value); }
94 remove { events.RemoveHandler(AcquireSessionStateEvent, value); }
97 /// <summary>
98 /// Occurs when a session is released.
99 /// </summary>
100 public event ExtensionHandler ReleaseSessionState
102 add { events.AddHandler(ReleaseSessionStateEvent, value); }
103 remove { events.RemoveHandler(ReleaseSessionStateEvent, value); }
106 internal void RaiseReleaseRequestState(IEngineContext context)
108 ExtensionHandler eventDelegate = (ExtensionHandler) events[ReleaseSessionStateEvent];
109 if (eventDelegate != null) eventDelegate(context);
112 internal void RaiseAcquireRequestState(IEngineContext context)
114 ExtensionHandler eventDelegate = (ExtensionHandler) events[AcquireSessionStateEvent];
115 if (eventDelegate != null) eventDelegate(context);
118 internal void RaiseUnhandledError(IEngineContext context)
120 ExtensionHandler eventDelegate = (ExtensionHandler) events[UnhandledExceptionEvent];
121 if (eventDelegate != null) eventDelegate(context);
124 internal void RaiseActionError(IEngineContext context)
126 ExtensionHandler eventDelegate = (ExtensionHandler) events[ActionExceptionEvent];
127 if (eventDelegate != null) eventDelegate(context);