1 // Copyright 2004-2008 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
.Providers
18 using System
.Collections
;
19 using System
.Reflection
;
20 using Castle
.Core
.Logging
;
21 using Castle
.MonoRail
.Framework
.Descriptors
;
24 /// Creates <see cref="RescueDescriptor"/> from attributes
25 /// associated with the <see cref="IController"/>
27 public class DefaultRescueDescriptorProvider
: IRescueDescriptorProvider
30 /// The logger instance
32 private ILogger logger
= NullLogger
.Instance
;
34 #region IMRServiceEnabled implementation
37 /// Invoked by the framework in order to give a chance to
38 /// obtain other services
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
));
54 /// Collects the rescues.
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
);
90 return (RescueDescriptor
[]) descriptors
.ToArray(typeof(RescueDescriptor
));
94 /// Implementors should collect the rescue information
95 /// and return descriptors instances, or an empty array if none
98 /// <param name="memberInfo">The action (MethodInfo)</param>
100 /// An array of <see cref="RescueDescriptor"/>
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
));