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 Castle
.Core
.Logging
;
20 using Castle
.MonoRail
.Framework
.Descriptors
;
23 /// Creates <see cref="HelperDescriptor"/> from attributes
24 /// associated with the <see cref="IController"/>
26 public class DefaultHelperDescriptorProvider
: IHelperDescriptorProvider
29 /// The logger instance
31 private ILogger logger
= NullLogger
.Instance
;
33 #region IMRServiceEnabled implementation
36 /// Invoked by the framework in order to give a chance to
37 /// obtain other services
39 /// <param name="provider">The service proviver</param>
40 public void Service(IMonoRailServices provider
)
42 ILoggerFactory loggerFactory
= (ILoggerFactory
) provider
.GetService(typeof(ILoggerFactory
));
44 if (loggerFactory
!= null)
46 logger
= loggerFactory
.Create(typeof(DefaultHelperDescriptorProvider
));
53 /// Implementors should collect the helper information
54 /// and return descriptors instances, or an empty array if none
57 /// <param name="controllerType">The controller type</param>
59 /// An array of <see cref="HelperDescriptor"/>
61 public HelperDescriptor
[] CollectHelpers(Type controllerType
)
63 if (logger
.IsDebugEnabled
)
65 logger
.DebugFormat("Collecting helpers for {0}", controllerType
);
68 object[] attributes
= controllerType
.GetCustomAttributes(typeof(IHelperDescriptorBuilder
), true);
70 ArrayList descriptors
= new ArrayList();
72 foreach(IHelperDescriptorBuilder builder
in attributes
)
74 HelperDescriptor
[] descs
= builder
.BuildHelperDescriptors();
76 if (logger
.IsDebugEnabled
)
78 foreach(HelperDescriptor desc
in descs
)
80 logger
.DebugFormat("Collected helper {0} with name {1}", desc
.HelperType
, desc
.Name
);
84 descriptors
.AddRange(descs
);
87 return (HelperDescriptor
[]) descriptors
.ToArray(typeof(HelperDescriptor
));