Minor changes to improve testability of helpers
[castle.git] / MonoRail / Castle.MonoRail.Framework / Services / DefaultResourceFactory.cs
blob5c1536509d66236622025c7d1bbca7ff56cc5fe9
1 // Copyright 2004-2007 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.Services
17 using System;
18 using System.Resources;
19 using System.Reflection;
20 using System.Globalization;
22 using Castle.Core;
23 using Castle.Core.Logging;
24 using Castle.MonoRail.Framework.Internal;
26 /// <summary>
27 /// Standard implementation of <see cref="IResourceFactory" />
28 /// </summary>
29 public class DefaultResourceFactory : IServiceEnabledComponent, IResourceFactory
31 /// <summary>
32 /// The logger instance
33 /// </summary>
34 private ILogger logger = NullLogger.Instance;
36 #region IServiceEnabledComponent implementation
38 /// <summary>
39 /// Invoked by the framework in order to give a chance to
40 /// obtain other services
41 /// </summary>
42 /// <param name="provider">The service proviver</param>
43 public void Service(IServiceProvider provider)
45 ILoggerFactory loggerFactory = (ILoggerFactory) provider.GetService(typeof(ILoggerFactory));
47 if (loggerFactory != null)
49 logger = loggerFactory.Create(typeof(DefaultResourceFactory));
53 #endregion
55 /// <summary>
56 /// Creates an implementation of <see cref="IResource"/>
57 /// based on the descriptor.
58 /// <seealso cref="ResourceManager"/>
59 /// <seealso cref="ResourceFacade"/>
60 /// </summary>
61 /// <param name="descriptor"></param>
62 /// <param name="appAssembly"></param>
63 /// <returns></returns>
64 public IResource Create(ResourceDescriptor descriptor, Assembly appAssembly)
66 Assembly assembly = this.ResolveAssembly(descriptor.AssemblyName, appAssembly);
67 CultureInfo cultureInfo = this.ResolveCulture(descriptor.CultureName);
69 if (logger.IsDebugEnabled)
71 logger.DebugFormat("Creating resource name {0}, assembly {1}, resource {2}",
72 descriptor.Name, descriptor.AssemblyName, descriptor.ResourceName);
75 ResourceManager manager = new ResourceManager(descriptor.ResourceName, assembly, descriptor.ResourceType);
76 return new ResourceFacade(manager, cultureInfo);
79 /// <summary>
80 /// Releases a resource
81 /// </summary>
82 /// <param name="resource"></param>
83 public void Release(IResource resource)
85 resource.Dispose();
88 /// <summary>
89 /// Resolves the culture by name.
90 /// </summary>
91 /// <param name="name">The name.</param>
92 /// <returns></returns>
93 private CultureInfo ResolveCulture(String name)
95 if (logger.IsDebugEnabled)
97 logger.DebugFormat("Resolving culture {0}", name);
100 if ("neutral".Equals(name))
102 return CultureInfo.InvariantCulture;
104 else if (name != null)
106 return CultureInfo.CreateSpecificCulture(name);
109 return CultureInfo.CurrentCulture;
112 /// <summary>
113 /// Resolves the assembly.
114 /// </summary>
115 /// <param name="name">The name.</param>
116 /// <param name="assembly">The assembly.</param>
117 /// <returns></returns>
118 private Assembly ResolveAssembly(String name, Assembly assembly)
120 if (name == null) return assembly;
122 if (logger.IsDebugEnabled)
124 logger.DebugFormat("Resolving assembly {0}", name);
129 return Assembly.Load(name);
131 catch(Exception ex)
133 logger.Error("Could not load assembly", ex);
135 throw;