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 asm = HttpContext.AppGlobalResourcesAssembly;
81 asm = GetLocalResourcesAssembly ();
82 path = Path.GetFileName (resource);
84 if (String.IsNullOrEmpty (path))
93 Stream ms = asm.GetManifestResourceStream (path);
97 return new ResourceReader (ms);
101 public DefaultResourceProvider (string resource, bool isGlobal)
103 if (String.IsNullOrEmpty (resource))
104 throw new ArgumentNullException ("resource");
106 this.resource = resource;
107 this.isGlobal = isGlobal;
110 public object GetObject (string resourceKey, CultureInfo culture)
112 if (String.IsNullOrEmpty (resourceKey))
115 ResourceManager rm = GetResourceManager ();
119 return rm.GetObject (resourceKey, culture);
122 Assembly GetLocalResourcesAssembly ()
127 path = VirtualPathUtility.GetDirectory (resource);
128 asm = AppResourcesCompiler.GetCachedLocalResourcesAssembly (path);
130 AppResourcesCompiler ac = new AppResourcesCompiler (path);
133 throw new MissingManifestResourceException ("A resource object was not found at the specified virtualPath.");
139 ResourceManager GetResourceManager ()
145 asm = HttpContext.AppGlobalResourcesAssembly;
148 asm = GetLocalResourcesAssembly ();
149 path = Path.GetFileName (resource);
151 if (String.IsNullOrEmpty (path))
160 if (resourceManagerCache == null)
161 resourceManagerCache = new Dictionary <ResourceManagerCacheKey, ResourceManager> ();
163 ResourceManagerCacheKey key = new ResourceManagerCacheKey (path, asm);
164 if (!resourceManagerCache.TryGetValue (key, out rm)) {
165 rm = new ResourceManager (path, asm);
166 rm.IgnoreCase = true;
167 resourceManagerCache.Add (key, rm);
171 } catch (MissingManifestResourceException) {
173 } catch (Exception ex) {
174 throw new HttpException ("Failed to retrieve the specified global resource object.", ex);