PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/ASP.Net Client Dependency/Config/ClientDependencySettings.cs

#
C# | 280 lines | 179 code | 47 blank | 54 comment | 26 complexity | 62c3192b7738c3760ebf51d9c1428da9 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web.Configuration;
  5. using System.Configuration.Provider;
  6. using System.Web;
  7. using System.Configuration;
  8. using ClientDependency.Core.FileRegistration.Providers;
  9. using ClientDependency.Core.CompositeFiles.Providers;
  10. using ClientDependency.Core.Logging;
  11. using System.IO;
  12. namespace ClientDependency.Core.Config
  13. {
  14. public class ClientDependencySettings
  15. {
  16. /// <summary>
  17. /// used for singleton
  18. /// </summary>
  19. private static ClientDependencySettings _settings;
  20. private static readonly object Lock = new object();
  21. /// <summary>
  22. /// Default constructor, for use with a web context app
  23. /// </summary>
  24. internal ClientDependencySettings()
  25. {
  26. if (HttpContext.Current == null)
  27. {
  28. throw new InvalidOperationException(
  29. "HttpContext.Current must exist when using the empty constructor for ClientDependencySettings, otherwise use the alternative constructor");
  30. }
  31. LoadProviders((ClientDependencySection)ConfigurationManager.GetSection("clientDependency"), new HttpContextWrapper(HttpContext.Current));
  32. }
  33. internal ClientDependencySettings(FileInfo configFile, HttpContextBase ctx)
  34. {
  35. var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile.FullName };
  36. var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
  37. LoadProviders((ClientDependencySection)configuration.GetSection("clientDependency"), ctx);
  38. }
  39. /// <summary>
  40. /// Singleton, used for web apps
  41. /// </summary>
  42. public static ClientDependencySettings Instance
  43. {
  44. get
  45. {
  46. if (_settings == null)
  47. {
  48. lock(Lock)
  49. {
  50. //double check
  51. if (_settings == null)
  52. {
  53. _settings = new ClientDependencySettings();
  54. }
  55. }
  56. }
  57. return _settings;
  58. }
  59. }
  60. /// <summary>
  61. /// The file extensions of Client Dependencies that are file based as opposed to request based.
  62. /// Any file that doesn't have the extensions listed here will be request based, request based is
  63. /// more overhead for the server to process.
  64. /// </summary>
  65. /// <example>
  66. /// A request based JavaScript file may be a .ashx that dynamically creates JavaScript server side.
  67. /// </example>
  68. /// <remarks>
  69. /// If this is not explicitly set, then the extensions 'js' and 'css' are the defaults.
  70. /// </remarks>
  71. public List<string> FileBasedDependencyExtensionList { get; set; }
  72. public int Version { get; set; }
  73. public ILogger Logger { get; private set; }
  74. /// <summary>
  75. /// Returns the default MVC renderer
  76. /// </summary>
  77. public BaseRenderer DefaultMvcRenderer { get; private set; }
  78. /// <summary>
  79. /// Returns the MVC renderer provider collection
  80. /// </summary>
  81. public RendererCollection MvcRendererCollection { get; private set; }
  82. /// <summary>
  83. /// Returns the default file registration provider
  84. /// </summary>
  85. public WebFormsFileRegistrationProvider DefaultFileRegistrationProvider { get; private set; }
  86. /// <summary>
  87. /// Returns the file registration provider collection
  88. /// </summary>
  89. public FileRegistrationProviderCollection FileRegistrationProviderCollection { get; private set; }
  90. /// <summary>
  91. /// Returns the default composite file processing provider
  92. /// </summary>
  93. public BaseCompositeFileProcessingProvider DefaultCompositeFileProcessingProvider { get; private set; }
  94. /// <summary>
  95. /// Returns the composite file processing provider collection
  96. /// </summary>
  97. public CompositeFileProcessingProviderCollection CompositeFileProcessingProviderCollection { get; private set; }
  98. /// <summary>
  99. /// Returns the default file map provider
  100. /// </summary>
  101. public BaseFileMapProvider DefaultFileMapProvider { get; private set; }
  102. /// <summary>
  103. /// Returns the collection of file map providers
  104. /// </summary>
  105. public FileMapProviderCollection FileMapProviderCollection { get; private set; }
  106. public ClientDependencySection ConfigSection { get; private set; }
  107. public string CompositeFileHandlerPath { get; set; }
  108. internal void LoadProviders(ClientDependencySection section, HttpContextBase http)
  109. {
  110. ConfigSection = section;
  111. FileRegistrationProviderCollection = new FileRegistrationProviderCollection();
  112. CompositeFileProcessingProviderCollection = new CompositeFileProcessingProviderCollection();
  113. MvcRendererCollection = new RendererCollection();
  114. FileMapProviderCollection = new FileMapProviderCollection();
  115. // if there is no section found, then create one
  116. if (ConfigSection == null)
  117. {
  118. //create a new section with the default settings
  119. ConfigSection = new ClientDependencySection();
  120. }
  121. //need to check if it's an http path or a lambda path
  122. var path = ConfigSection.CompositeFileElement.CompositeFileHandlerPath;
  123. CompositeFileHandlerPath = path.StartsWith("~/")
  124. ? VirtualPathUtility.ToAbsolute(ConfigSection.CompositeFileElement.CompositeFileHandlerPath, http.Request.ApplicationPath)
  125. : ConfigSection.CompositeFileElement.CompositeFileHandlerPath;
  126. Version = ConfigSection.Version;
  127. FileBasedDependencyExtensionList = ConfigSection.FileBasedDependencyExtensionList.ToList();
  128. //load the providers from the config, if there isn't config sections then add default providers
  129. // and then load the defaults.
  130. LoadDefaultCompositeFileConfig(ConfigSection, http);
  131. DefaultCompositeFileProcessingProvider = CompositeFileProcessingProviderCollection[ConfigSection.CompositeFileElement.DefaultFileProcessingProvider];
  132. if (DefaultCompositeFileProcessingProvider == null)
  133. throw new ProviderException("Unable to load default composite file provider");
  134. LoadDefaultFileMapConfig(ConfigSection, http);
  135. DefaultFileMapProvider = FileMapProviderCollection[ConfigSection.CompositeFileElement.DefaultFileMapProvider];
  136. if (DefaultFileMapProvider == null)
  137. throw new ProviderException("Unable to load default file map provider");
  138. LoadDefaultMvcFileConfig(ConfigSection);
  139. DefaultMvcRenderer = MvcRendererCollection[ConfigSection.MvcElement.DefaultRenderer];
  140. if (DefaultMvcRenderer == null)
  141. throw new ProviderException("Unable to load default mvc renderer");
  142. LoadDefaultFileRegConfig(ConfigSection);
  143. DefaultFileRegistrationProvider = FileRegistrationProviderCollection[ConfigSection.FileRegistrationElement.DefaultProvider];
  144. if (DefaultFileRegistrationProvider == null)
  145. throw new ProviderException("Unable to load default file registration provider");
  146. if (string.IsNullOrEmpty(ConfigSection.LoggerType))
  147. {
  148. Logger = new NullLogger();
  149. }
  150. else
  151. {
  152. var t = Type.GetType(ConfigSection.LoggerType);
  153. if (!typeof(ILogger).IsAssignableFrom(t))
  154. {
  155. throw new ArgumentException("The loggerType '" + ConfigSection.LoggerType + "' does not inherit from ClientDependency.Core.Logging.ILogger");
  156. }
  157. Logger = (ILogger)Activator.CreateInstance(t);
  158. }
  159. }
  160. private void LoadDefaultFileRegConfig(ClientDependencySection section)
  161. {
  162. if (section.FileRegistrationElement.Providers.Count == 0)
  163. {
  164. //create new providers
  165. var php = new PageHeaderProvider();
  166. php.Initialize(PageHeaderProvider.DefaultName, null);
  167. FileRegistrationProviderCollection.Add(php);
  168. var csrp = new LazyLoadProvider();
  169. csrp.Initialize(LazyLoadProvider.DefaultName, null);
  170. FileRegistrationProviderCollection.Add(csrp);
  171. var lcp = new LoaderControlProvider();
  172. lcp.Initialize(LoaderControlProvider.DefaultName, null);
  173. FileRegistrationProviderCollection.Add(lcp);
  174. }
  175. else
  176. {
  177. ProvidersHelper.InstantiateProviders(section.FileRegistrationElement.Providers, FileRegistrationProviderCollection, typeof(BaseFileRegistrationProvider));
  178. }
  179. }
  180. private void LoadDefaultFileMapConfig(ClientDependencySection section, HttpContextBase http)
  181. {
  182. if (section.CompositeFileElement.FileMapProviders.Count == 0)
  183. {
  184. //if not specified, create default
  185. var fmp = new XmlFileMapper();
  186. fmp.Initialize(XmlFileMapper.DefaultName, null);
  187. fmp.Initialize(http);
  188. FileMapProviderCollection.Add(fmp);
  189. }
  190. else
  191. {
  192. ProvidersHelper.InstantiateProviders(section.CompositeFileElement.FileMapProviders, FileMapProviderCollection, typeof(BaseFileMapProvider));
  193. //since the BaseFileMapProvider is an IHttpProvider, we need to do the http init
  194. foreach (var p in FileMapProviderCollection.Cast<BaseFileMapProvider>())
  195. {
  196. p.Initialize(http);
  197. }
  198. }
  199. }
  200. private void LoadDefaultCompositeFileConfig(ClientDependencySection section, HttpContextBase http)
  201. {
  202. if (section.CompositeFileElement.FileProcessingProviders.Count == 0)
  203. {
  204. var cfpp = new CompositeFileProcessingProvider();
  205. cfpp.Initialize(CompositeFileProcessingProvider.DefaultName, null);
  206. cfpp.Initialize(http);
  207. CompositeFileProcessingProviderCollection.Add(cfpp);
  208. }
  209. else
  210. {
  211. ProvidersHelper.InstantiateProviders(section.CompositeFileElement.FileProcessingProviders, CompositeFileProcessingProviderCollection, typeof(BaseCompositeFileProcessingProvider));
  212. //since the BaseCompositeFileProcessingProvider is an IHttpProvider, we need to do the http init
  213. foreach(var p in CompositeFileProcessingProviderCollection.Cast<BaseCompositeFileProcessingProvider>())
  214. {
  215. p.Initialize(http);
  216. }
  217. }
  218. }
  219. private void LoadDefaultMvcFileConfig(ClientDependencySection section)
  220. {
  221. if (section.MvcElement.Renderers.Count == 0)
  222. {
  223. var mvc = new StandardRenderer();
  224. mvc.Initialize(StandardRenderer.DefaultName, null);
  225. MvcRendererCollection.Add(mvc);
  226. }
  227. else
  228. {
  229. ProvidersHelper.InstantiateProviders(section.MvcElement.Renderers, MvcRendererCollection, typeof(BaseRenderer));
  230. }
  231. }
  232. }
  233. }