Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework / Resources / DefaultResourceFactory.cs
blob95e481da73786789a537e262be1bd5c841b769c8
1 // Copyright 2004-2008 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.Resources
17 using System;
18 using System.Globalization;
19 using System.Reflection;
20 using System.Resources;
21 using Castle.Core.Logging;
22 using Core;
23 using Descriptors;
25 /// <summary>
26 /// Standard implementation of <see cref="IResourceFactory" />
27 /// </summary>
28 public class DefaultResourceFactory : IServiceEnabledComponent, IResourceFactory
30 /// <summary>
31 /// The logger instance
32 /// </summary>
33 private ILogger logger = NullLogger.Instance;
35 #region IServiceEnabledComponent implementation
37 /// <summary>
38 /// Invoked by the framework in order to give a chance to
39 /// obtain other services
40 /// </summary>
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));
52 #endregion
54 /// <summary>
55 /// Creates an implementation of <see cref="IResource"/>
56 /// based on the descriptor.
57 /// <seealso cref="ResourceManager"/>
58 /// <seealso cref="ResourceFacade"/>
59 /// </summary>
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);
78 /// <summary>
79 /// Resolves the culture by name.
80 /// </summary>
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;
102 /// <summary>
103 /// Resolves the assembly.
104 /// </summary>
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);
121 catch(Exception ex)
123 logger.Error("Could not load assembly", ex);
125 throw;