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
17 using System
.Collections
;
18 using System
.Reflection
;
19 using Castle
.Core
.Logging
;
20 using Castle
.MonoRail
.Framework
.Descriptors
;
23 /// Creates <see cref="ResourceDescriptor"/> from attributes
24 /// associated with the <see cref="IController"/>
26 public class DefaultResourceDescriptorProvider
: IResourceDescriptorProvider
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(DefaultResourceDescriptorProvider
));
53 /// Collects the resources.
55 /// <param name="memberInfo">The member info.</param>
56 /// <returns></returns>
57 public ResourceDescriptor
[] CollectResources(MemberInfo memberInfo
)
59 if (logger
.IsDebugEnabled
)
61 logger
.DebugFormat("Collecting resources information for {0}", memberInfo
.Name
);
64 object[] attributes
= memberInfo
.GetCustomAttributes(typeof(IResourceDescriptorBuilder
), true);
66 ArrayList descriptors
= new ArrayList();
68 foreach(IResourceDescriptorBuilder builder
in attributes
)
70 ResourceDescriptor
[] descs
= builder
.BuildResourceDescriptors();
72 if (logger
.IsDebugEnabled
)
74 foreach(ResourceDescriptor desc
in descs
)
76 logger
.DebugFormat("Collected resource {0} Assembly Name {1} Culture {2} ResName {3} ResType {4}",
77 desc
.Name
, desc
.AssemblyName
, desc
.CultureName
, desc
.ResourceName
, desc
.ResourceType
);
81 descriptors
.AddRange(descs
);
84 return (ResourceDescriptor
[]) descriptors
.ToArray(typeof(ResourceDescriptor
));