1 // DefaultResourceProvider.cs
4 // Marek Habersack (mhabersack@novell.com)
6 // (C) 2009 Novell, Inc (http://novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections.Generic;
31 using System.Globalization;
33 using System.Resources;
34 using System.Reflection;
37 namespace System.Web.Compilation
39 sealed class DefaultResourceProvider : IResourceProvider
41 sealed class ResourceManagerCacheKey
43 readonly string _name;
44 readonly Assembly _asm;
46 public ResourceManagerCacheKey (string name, Assembly asm)
52 public override bool Equals (object obj)
54 if (!(obj is ResourceManagerCacheKey))
56 ResourceManagerCacheKey key = (ResourceManagerCacheKey) obj;
57 return key._asm == _asm && _name.Equals (key._name, StringComparison.Ordinal);
60 public override int GetHashCode ()
62 return _name.GetHashCode () + _asm.GetHashCode ();
67 static Dictionary <ResourceManagerCacheKey, ResourceManager> resourceManagerCache;
72 public IResourceReader ResourceReader {
78 public DefaultResourceProvider (string resource, bool isGlobal)
80 if (String.IsNullOrEmpty (resource))
81 throw new ArgumentNullException ("resource");
83 this.resource = resource;
84 this.isGlobal = isGlobal;
87 public object GetObject (string resourceKey, CultureInfo culture)
90 if (HttpContext.AppGlobalResourcesAssembly == null)
93 return GetResourceObject (resource, resourceKey, culture, HttpContext.AppGlobalResourcesAssembly);
96 string path = VirtualPathUtility.GetDirectory (resource);
97 Assembly asm = AppResourcesCompiler.GetCachedLocalResourcesAssembly (path);
99 AppResourcesCompiler ac = new AppResourcesCompiler (path);
102 throw new MissingManifestResourceException ("A resource object was not found at the specified virtualPath.");
105 path = Path.GetFileName (resource);
106 return GetResourceObject (path, resourceKey, culture, asm);
109 static object GetResourceObject (string classKey, string resourceKey, CultureInfo culture, Assembly assembly)
113 if (resourceManagerCache == null)
114 resourceManagerCache = new Dictionary <ResourceManagerCacheKey, ResourceManager> ();
116 ResourceManagerCacheKey key = new ResourceManagerCacheKey (classKey, assembly);
117 if (!resourceManagerCache.TryGetValue (key, out rm)) {
118 rm = new ResourceManager (classKey, assembly);
119 rm.IgnoreCase = true;
120 resourceManagerCache.Add (key, rm);
123 return rm.GetObject (resourceKey, culture);
124 } catch (MissingManifestResourceException) {
126 } catch (Exception ex) {
127 throw new HttpException ("Failed to retrieve the specified global resource object.", ex);