static void VerifyAssemblyContainsResource(Assembly assembly, string resourceName, out WebResourceAttribute wra) { var rce = new ResourceCacheEntry { Assembly = assembly, ResourceName = resourceName }; WebResourceAttribute attr = null; if (!resourceCache.InsertOrGet((uint)rce.GetHashCode(), rce, false, () => CheckIfAssemblyContainsResource(assembly, resourceName, out attr))) { throw new InvalidOperationException(String.Format("Assembly '{0}' does not contain a Web resource with name '{1}'.", assembly.FullName, resourceName)); } wra = attr; }
public ScriptResourceInfo(WebResourceAttribute wra, ScriptResourceAttribute sra, Assembly assembly) : this() { _scriptName = wra.WebResource; _cdnPath = wra.CdnPath; _contentType = wra.ContentType; _performSubstitution = wra.PerformSubstitution; _loadSuccessExpression = wra.LoadSuccessExpression; _isDebug = !String.IsNullOrEmpty(_scriptName) && _scriptName.EndsWith(".debug.js", StringComparison.OrdinalIgnoreCase); if (sra != null) { _scriptResourceName = sra.StringResourceName; _typeName = sra.StringResourceClientTypeName; } if (!String.IsNullOrEmpty(_cdnPath)) { _cdnPath = AssemblyResourceLoader.FormatCdnUrl(assembly, _cdnPath); _cdnPathSecureConnection = AssemblyResourceLoader.FormatCdnUrl(assembly, wra.CdnPathSecureConnection); } }
public ScriptResourceInfo(WebResourceAttribute wra, ScriptResourceAttribute sra, Assembly assembly) : this() { _scriptName = wra.WebResource; _cdnPath = wra.CdnPath; _contentType = wra.ContentType; _performSubstitution = wra.PerformSubstitution; _loadSuccessExpression = wra.LoadSuccessExpression; _isDebug = !String.IsNullOrEmpty(_scriptName) && _scriptName.EndsWith(".debug.js", StringComparison.OrdinalIgnoreCase); if (sra != null) { _scriptResourceName = sra.StringResourceName; _typeName = sra.StringResourceClientTypeName; } if (!String.IsNullOrEmpty(_cdnPath)) { _cdnPath = AssemblyResourceLoader.FormatCdnUrl(assembly, _cdnPath); _cdnPathSecureConnection = AssemblyResourceLoader.FormatCdnUrl(assembly, wra.CdnPathSecureConnection); } }
static bool CheckIfAssemblyContainsResource(Assembly assembly, string resourceName, out WebResourceAttribute wra) { foreach (WebResourceAttribute attr in assembly.GetCustomAttributes(typeof(WebResourceAttribute), false)) { if (String.Compare(resourceName, attr.WebResource, StringComparison.Ordinal) == 0) { using (Stream rs = assembly.GetManifestResourceStream(resourceName)) { if (rs == null) { throw new InvalidOperationException( String.Format("Assembly '{0}' contains a Web resource with name '{1}' but does not contain an embedded resource with name '{1}'.", assembly.FullName, resourceName) ); } } wra = attr; return(true); } } wra = null; return(false); }
internal static string GetScriptName(string releaseName, bool isDebugMode, string [] supportedUICultures, Assembly assembly, out WebResourceAttribute wra) { if (assembly != null) { VerifyAssemblyContainsResource(assembly, releaseName, out wra); } else { wra = null; } if (!isDebugMode && (supportedUICultures == null || supportedUICultures.Length == 0)) { return(releaseName); } if (releaseName.Length < 3 || !releaseName.EndsWith(".js", StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException(String.Format("'{0}' is not a valid script path. The path must end in '.js'.", releaseName)); } StringBuilder sb = new StringBuilder(releaseName); sb.Length -= 3; if (isDebugMode) { sb.Append(".debug"); } string culture = Thread.CurrentThread.CurrentUICulture.Name; if (supportedUICultures != null && Array.IndexOf <string> (supportedUICultures, culture) >= 0) { sb.AppendFormat(".{0}", culture); } sb.Append(".js"); string ret = sb.ToString(); WebResourceAttribute debugWra; if (!CheckIfAssemblyContainsResource(assembly, ret, out debugWra)) { return(releaseName); } wra = debugWra; return(ret); }
static bool CheckIfAssemblyContainsResource (Assembly assembly, string resourceName, out WebResourceAttribute wra) { foreach (WebResourceAttribute attr in assembly.GetCustomAttributes (typeof (WebResourceAttribute), false)) { if (String.Compare (resourceName, attr.WebResource, StringComparison.Ordinal) == 0) { using (Stream rs = assembly.GetManifestResourceStream (resourceName)) { if (rs == null) throw new InvalidOperationException ( String.Format ("Assembly '{0}' contains a Web resource with name '{1}' but does not contain an embedded resource with name '{1}'.", assembly.FullName, resourceName) ); } wra = attr; return true; } } wra = null; return false; }
static void VerifyAssemblyContainsResource (Assembly assembly, string resourceName, out WebResourceAttribute wra) { var rce = new ResourceCacheEntry { Assembly = assembly, ResourceName = resourceName }; WebResourceAttribute attr = null; if (!resourceCache.InsertOrGet ((uint)rce.GetHashCode (), rce, false, () => CheckIfAssemblyContainsResource (assembly, resourceName, out attr))) throw new InvalidOperationException (String.Format ("Assembly '{0}' does not contain a Web resource with name '{1}'.", assembly.FullName, resourceName)); wra = attr; }
internal static string GetScriptName (string releaseName, bool isDebugMode, string [] supportedUICultures, Assembly assembly, out WebResourceAttribute wra) { if (assembly != null) VerifyAssemblyContainsResource (assembly, releaseName, out wra); else wra = null; if (!isDebugMode && (supportedUICultures == null || supportedUICultures.Length == 0)) return releaseName; if (releaseName.Length < 3 || !releaseName.EndsWith (".js", StringComparison.OrdinalIgnoreCase)) throw new InvalidOperationException (String.Format ("'{0}' is not a valid script path. The path must end in '.js'.", releaseName)); StringBuilder sb = new StringBuilder (releaseName); sb.Length -= 3; if (isDebugMode) sb.Append (".debug"); string culture = Thread.CurrentThread.CurrentUICulture.Name; if (supportedUICultures != null && Array.IndexOf<string> (supportedUICultures, culture) >= 0) sb.AppendFormat (".{0}", culture); sb.Append (".js"); string ret = sb.ToString (); WebResourceAttribute debugWra; if (!CheckIfAssemblyContainsResource (assembly, ret, out debugWra)) return releaseName; wra = debugWra; return ret; }
public static ScriptResourceInfo GetInstance(Assembly assembly, string resourceName) { // The first time this API is called, check for attributes that point to the same script if (!_duplicateScriptAttributesChecked.Contains(assembly)) { Dictionary <string, bool> scripts = new Dictionary <string, bool>(); foreach (ScriptResourceAttribute attr in assembly.GetCustomAttributes(typeof(ScriptResourceAttribute), false)) { string scriptName = attr.ScriptName; if (scripts.ContainsKey(scriptName)) { throw new InvalidOperationException( String.Format(CultureInfo.CurrentCulture, AtlasWeb.ScriptResourceHandler_DuplicateScriptResources, scriptName, assembly.GetName())); } scripts.Add(scriptName, true); } _duplicateScriptAttributesChecked[assembly] = true; } Tuple <Assembly, string> cacheKey = new Tuple <Assembly, string>(assembly, resourceName); ScriptResourceInfo resourceInfo = (ScriptResourceInfo)_scriptCache[cacheKey]; if (resourceInfo == null) { WebResourceAttribute webResourceAttribute = null; ScriptResourceAttribute scriptResourceAttribute = null; // look for a WebResourceAttribute with that name object[] attrs = assembly.GetCustomAttributes(typeof(WebResourceAttribute), false); foreach (WebResourceAttribute wra in attrs) { if (String.Equals(wra.WebResource, resourceName, StringComparison.Ordinal)) { webResourceAttribute = wra; break; } } if (webResourceAttribute != null) { // look for a script resource attribute with that name attrs = assembly.GetCustomAttributes(typeof(ScriptResourceAttribute), false); foreach (ScriptResourceAttribute sra in attrs) { if (String.Equals(sra.ScriptName, resourceName, StringComparison.Ordinal)) { scriptResourceAttribute = sra; break; } } resourceInfo = new ScriptResourceInfo(webResourceAttribute, scriptResourceAttribute, assembly); } else { resourceInfo = ScriptResourceInfo.Empty; } // Cache the results so we don't have to do this again _scriptCache[cacheKey] = resourceInfo; } return(resourceInfo); }