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();
38 private readonly EventHandlerList events
;
39 private readonly IMonoRailServices serviceProvider
;
40 private readonly List
<IMonoRailExtension
> extensions
= new List
<IMonoRailExtension
>();
43 /// Initializes a new instance of the <see cref="ExtensionManager"/> class.
45 /// <param name="serviceContainer">The service container.</param>
46 public ExtensionManager(IMonoRailServices serviceContainer
)
48 events
= new EventHandlerList();
49 serviceProvider
= serviceContainer
;
53 /// Gets the service container.
55 /// <value>The service container.</value>
56 public IMonoRailServices ServiceProvider
58 get { return serviceProvider; }
62 /// Gets the extensions.
64 /// <value>The extensions.</value>
65 public List
<IMonoRailExtension
> Extensions
67 get { return extensions; }
71 /// Occurs when an action throws an exception.
73 public event ExtensionHandler ActionException
75 add { events.AddHandler(ActionExceptionEvent, value); }
76 remove { events.RemoveHandler(ActionExceptionEvent, value); }
80 /// Occurs when an unhandled exception is thrown.
82 public event ExtensionHandler UnhandledException
84 add { events.AddHandler(UnhandledExceptionEvent, value); }
85 remove { events.RemoveHandler(UnhandledExceptionEvent, value); }
89 /// Occurs when a session is adquired.
91 public event ExtensionHandler AcquireSessionState
93 add { events.AddHandler(AcquireSessionStateEvent, value); }
94 remove { events.RemoveHandler(AcquireSessionStateEvent, value); }
98 /// Occurs when a session is released.
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
);