1 // Copyright 2004-2007 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
.Services
18 using System
.Resources
;
19 using System
.Reflection
;
20 using System
.Globalization
;
23 using Castle
.Core
.Logging
;
24 using Castle
.MonoRail
.Framework
.Internal
;
27 /// Standard implementation of <see cref="IResourceFactory" />
29 public class DefaultResourceFactory
: IServiceEnabledComponent
, IResourceFactory
32 /// The logger instance
34 private ILogger logger
= NullLogger
.Instance
;
36 #region IServiceEnabledComponent implementation
39 /// Invoked by the framework in order to give a chance to
40 /// obtain other services
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
));
56 /// Creates an implementation of <see cref="IResource"/>
57 /// based on the descriptor.
58 /// <seealso cref="ResourceManager"/>
59 /// <seealso cref="ResourceFacade"/>
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
);
80 /// Releases a resource
82 /// <param name="resource"></param>
83 public void Release(IResource resource
)
89 /// Resolves the culture by name.
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
;
113 /// Resolves the assembly.
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
);
133 logger
.Error("Could not load assembly", ex
);