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
.Collections
.Generic
;
19 using System
.ComponentModel
;
24 /// <param name="context"></param>
25 public delegate void ExtensionHandler(IEngineContext 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 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();
37 private static readonly object PreProcessControllerEvent
= new object();
38 private static readonly object PostProcessControllerEvent
= new object();
40 private readonly EventHandlerList events
;
41 private readonly IMonoRailServices serviceProvider
;
42 private readonly List
<IMonoRailExtension
> extensions
= new List
<IMonoRailExtension
>();
45 /// Initializes a new instance of the <see cref="ExtensionManager"/> class.
47 /// <param name="serviceContainer">The service container.</param>
48 public ExtensionManager(IMonoRailServices serviceContainer
)
50 events
= new EventHandlerList();
51 serviceProvider
= serviceContainer
;
55 /// Gets the service container.
57 /// <value>The service container.</value>
58 public IMonoRailServices ServiceProvider
60 get { return serviceProvider; }
64 /// Gets the extensions.
66 /// <value>The extensions.</value>
67 public List
<IMonoRailExtension
> Extensions
69 get { return extensions; }
73 /// Occurs when an action throws an exception.
75 public event ExtensionHandler ActionException
77 add { events.AddHandler(ActionExceptionEvent, value); }
78 remove { events.RemoveHandler(ActionExceptionEvent, value); }
82 /// Occurs when an unhandled exception is thrown.
84 public event ExtensionHandler UnhandledException
86 add { events.AddHandler(UnhandledExceptionEvent, value); }
87 remove { events.RemoveHandler(UnhandledExceptionEvent, value); }
91 /// Occurs when a session is adquired.
93 public event ExtensionHandler AcquireSessionState
95 add { events.AddHandler(AcquireSessionStateEvent, value); }
96 remove { events.RemoveHandler(AcquireSessionStateEvent, value); }
100 /// Occurs when a session is released.
102 public event ExtensionHandler ReleaseSessionState
104 add { events.AddHandler(ReleaseSessionStateEvent, value); }
105 remove { events.RemoveHandler(ReleaseSessionStateEvent, value); }
109 /// Occurs before processing controller.
111 public event ExtensionHandler PreControllerProcess
113 add { events.AddHandler(PreProcessControllerEvent, value); }
114 remove { events.RemoveHandler(PreProcessControllerEvent, value); }
118 /// Occurs after processing controller.
120 public event ExtensionHandler PostControllerProcess
122 add { events.AddHandler(PostProcessControllerEvent, value); }
123 remove { events.RemoveHandler(PostProcessControllerEvent, value); }
126 internal void RaiseReleaseRequestState(IEngineContext context
)
128 ExtensionHandler eventDelegate
= (ExtensionHandler
) events
[ReleaseSessionStateEvent
];
129 if (eventDelegate
!= null) eventDelegate(context
);
132 internal void RaiseAcquireRequestState(IEngineContext context
)
134 ExtensionHandler eventDelegate
= (ExtensionHandler
) events
[AcquireSessionStateEvent
];
135 if (eventDelegate
!= null) eventDelegate(context
);
138 internal void RaiseUnhandledError(IEngineContext context
)
140 ExtensionHandler eventDelegate
= (ExtensionHandler
) events
[UnhandledExceptionEvent
];
141 if (eventDelegate
!= null) eventDelegate(context
);
144 internal void RaiseActionError(IEngineContext context
)
146 ExtensionHandler eventDelegate
= (ExtensionHandler
) events
[ActionExceptionEvent
];
147 if (eventDelegate
!= null) eventDelegate(context
);
150 internal void RaisePostProcessController(IEngineContext context
)
152 ExtensionHandler eventDelegate
= (ExtensionHandler
) events
[PostProcessControllerEvent
];
153 if (eventDelegate
!= null) eventDelegate(context
);
156 internal void RaisePreProcessController(IEngineContext context
)
158 ExtensionHandler eventDelegate
= (ExtensionHandler
) events
[PreProcessControllerEvent
];
159 if (eventDelegate
!= null) eventDelegate(context
);