PageRenderTime 102ms CodeModel.GetById 40ms app.highlight 1ms RepoModel.GetById 59ms app.codeStats 0ms

/src/LinFu.AOP.Interfaces/ExceptionHandlerRegistry.cs

http://github.com/philiplaureano/LinFu
C# | 42 lines | 23 code | 4 blank | 15 comment | 3 complexity | 0c8e382a29d347ca66424d2f0b84c09a MD5 | raw file
 1namespace LinFu.AOP.Interfaces
 2{
 3    /// <summary>
 4    ///     Represents a class that stores <see cref="IExceptionHandler" /> instances in a central location.
 5    /// </summary>
 6    public static class ExceptionHandlerRegistry
 7    {
 8        private static IExceptionHandler _handler;
 9        private static readonly object _lock = new object();
10
11        /// <summary>
12        ///     Gets the <see cref="IExceptionHandler" /> instance that can handle the current exception.
13        /// </summary>
14        /// <param name="info">
15        ///     The <see cref="IExceptionHandlerInfo" /> instance that describes the context of the thrown
16        ///     exception.
17        /// </param>
18        /// <returns>An exception handler.</returns>
19        public static IExceptionHandler GetHandler(IExceptionHandlerInfo info)
20        {
21            if (_handler == null)
22                return null;
23
24            if (!_handler.CanCatch(info))
25                return null;
26
27            return _handler;
28        }
29
30        /// <summary>
31        ///     Sets the <see cref="IExceptionHandler" /> instance that can handle all thrown exceptions.
32        /// </summary>
33        /// <param name="handler">The exception handler.</param>
34        public static void SetHandler(IExceptionHandler handler)
35        {
36            lock (_lock)
37            {
38                _handler = handler;
39            }
40        }
41    }
42}