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
.Resources
18 using System
.Globalization
;
19 using System
.Reflection
;
20 using System
.Resources
;
21 using Castle
.Core
.Logging
;
26 /// Standard implementation of <see cref="IResourceFactory" />
28 public class DefaultResourceFactory
: IServiceEnabledComponent
, IResourceFactory
31 /// The logger instance
33 private ILogger logger
= NullLogger
.Instance
;
35 #region IServiceEnabledComponent implementation
38 /// Invoked by the framework in order to give a chance to
39 /// obtain other services
41 /// <param name="provider">The service proviver</param>
42 public void Service(IServiceProvider provider
)
44 ILoggerFactory loggerFactory
= (ILoggerFactory
) provider
.GetService(typeof(ILoggerFactory
));
46 if (loggerFactory
!= null)
48 logger
= loggerFactory
.Create(typeof(DefaultResourceFactory
));
55 /// Creates an implementation of <see cref="IResource"/>
56 /// based on the descriptor.
57 /// <seealso cref="ResourceManager"/>
58 /// <seealso cref="ResourceFacade"/>
60 /// <param name="descriptor"></param>
61 /// <param name="appAssembly"></param>
62 /// <returns></returns>
63 public IResource
Create(ResourceDescriptor descriptor
, Assembly appAssembly
)
65 Assembly assembly
= ResolveAssembly(descriptor
.AssemblyName
, appAssembly
);
66 CultureInfo cultureInfo
= ResolveCulture(descriptor
.CultureName
);
68 if (logger
.IsDebugEnabled
)
70 logger
.DebugFormat("Creating resource name {0}, assembly {1}, resource {2}",
71 descriptor
.Name
, descriptor
.AssemblyName
, descriptor
.ResourceName
);
74 ResourceManager manager
= new ResourceManager(descriptor
.ResourceName
, assembly
, descriptor
.ResourceType
);
75 return new ResourceFacade(manager
, cultureInfo
);
79 /// Resolves the culture by name.
81 /// <param name="name">The name.</param>
82 /// <returns></returns>
83 private CultureInfo
ResolveCulture(String name
)
85 if (logger
.IsDebugEnabled
)
87 logger
.DebugFormat("Resolving culture {0}", name
);
90 if ("neutral".Equals(name
))
92 return CultureInfo
.InvariantCulture
;
94 else if (name
!= null)
96 return CultureInfo
.CreateSpecificCulture(name
);
99 return CultureInfo
.CurrentCulture
;
103 /// Resolves the assembly.
105 /// <param name="name">The name.</param>
106 /// <param name="assembly">The assembly.</param>
107 /// <returns></returns>
108 private Assembly
ResolveAssembly(String name
, Assembly assembly
)
110 if (name
== null) return assembly
;
112 if (logger
.IsDebugEnabled
)
114 logger
.DebugFormat("Resolving assembly {0}", name
);
119 return Assembly
.Load(name
);
123 logger
.Error("Could not load assembly", ex
);