Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework / Providers / DefaultRescueDescriptorProvider.cs
blob95c51208cffc77e6d6d6be85da8196b13308f2d7
1 // Copyright 2004-2008 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.Providers
17 using System;
18 using System.Collections;
19 using System.Reflection;
20 using Castle.Core.Logging;
21 using Castle.MonoRail.Framework.Descriptors;
23 /// <summary>
24 /// Creates <see cref="RescueDescriptor"/> from attributes
25 /// associated with the <see cref="IController"/>
26 /// </summary>
27 public class DefaultRescueDescriptorProvider : IRescueDescriptorProvider
29 /// <summary>
30 /// The logger instance
31 /// </summary>
32 private ILogger logger = NullLogger.Instance;
34 #region IMRServiceEnabled implementation
36 /// <summary>
37 /// Invoked by the framework in order to give a chance to
38 /// obtain other services
39 /// </summary>
40 /// <param name="provider">The service proviver</param>
41 public void Service(IMonoRailServices provider)
43 ILoggerFactory loggerFactory = (ILoggerFactory) provider.GetService(typeof(ILoggerFactory));
45 if (loggerFactory != null)
47 logger = loggerFactory.Create(typeof(DefaultRescueDescriptorProvider));
51 #endregion
53 /// <summary>
54 /// Collects the rescues.
55 /// </summary>
56 /// <param name="type">The type.</param>
57 /// <returns></returns>
58 public RescueDescriptor[] CollectRescues(Type type)
60 if (logger.IsDebugEnabled)
62 logger.DebugFormat("Collecting rescue information for {0}", type.Name);
65 ArrayList descriptors = new ArrayList();
67 while(type != typeof(object))
69 object[] attributes = type.GetCustomAttributes(typeof(IRescueDescriptorBuilder), false);
71 foreach(IRescueDescriptorBuilder builder in attributes)
73 RescueDescriptor[] descs = builder.BuildRescueDescriptors();
75 if (logger.IsDebugEnabled)
77 foreach(RescueDescriptor desc in descs)
79 logger.DebugFormat("Collected rescue with view name {0} for exception type {1}",
80 desc.ViewName, desc.ExceptionType);
84 descriptors.AddRange(descs);
87 type = type.BaseType;
90 return (RescueDescriptor[]) descriptors.ToArray(typeof(RescueDescriptor));
93 /// <summary>
94 /// Implementors should collect the rescue information
95 /// and return descriptors instances, or an empty array if none
96 /// was found.
97 /// </summary>
98 /// <param name="memberInfo">The action (MethodInfo)</param>
99 /// <returns>
100 /// An array of <see cref="RescueDescriptor"/>
101 /// </returns>
102 public RescueDescriptor[] CollectRescues(MethodInfo memberInfo)
104 if (logger.IsDebugEnabled)
106 logger.DebugFormat("Collecting rescue information for {0}", memberInfo.Name);
109 object[] attributes = memberInfo.GetCustomAttributes(typeof(IRescueDescriptorBuilder), true);
111 ArrayList descriptors = new ArrayList();
113 foreach(IRescueDescriptorBuilder builder in attributes)
115 RescueDescriptor[] descs = builder.BuildRescueDescriptors();
117 if (logger.IsDebugEnabled)
119 foreach(RescueDescriptor desc in descs)
121 logger.DebugFormat("Collected rescue with view name {0} for exception type {1}",
122 desc.ViewName, desc.ExceptionType);
126 descriptors.AddRange(descs);
129 return (RescueDescriptor[]) descriptors.ToArray(typeof(RescueDescriptor));