PageRenderTime 1159ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/1.0.1.x/Freetime.PluginManager/PluginManager.cs

#
C# | 251 lines | 223 code | 27 blank | 1 comment | 34 complexity | 5397931493002e0e01647297c4408dc9 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Xml;
  7. using System.Xml.Serialization;
  8. using Freetime.Base.Data;
  9. using Freetime.Base.Data.Entities;
  10. using Freetime.Base.Data.Collection;
  11. using Freetime.PluginManagement.Configuration;
  12. using Freetime.Configuration;
  13. namespace Freetime.PluginManagement
  14. {
  15. public class PluginManager : IPluginManager
  16. {
  17. private static IPluginManager __INSTANCE = null;
  18. private PluginManagerConfiguration Configuration { get; set; }
  19. public static IPluginManager Current
  20. {
  21. get
  22. {
  23. if (__INSTANCE == null)
  24. __INSTANCE = new PluginManager();
  25. return __INSTANCE;
  26. }
  27. }
  28. string IPluginManager.Name
  29. {
  30. get
  31. {
  32. return "Freetime Default Plugin Manager";
  33. }
  34. }
  35. internal PluginManager()
  36. {
  37. string configSection = ConfigurationManager.FreetimeConfig.PluginManagementConfigurationSection;
  38. if (!string.IsNullOrEmpty(configSection))
  39. {
  40. var config = System.Configuration.ConfigurationManager.GetSection(configSection);
  41. if (config == null)
  42. {
  43. //TODO throw proper exception
  44. throw new Exception("PluginManagementConfiguration Not Implemented");
  45. }
  46. Configuration = config as PluginManagerConfiguration;
  47. }
  48. else
  49. throw new Exception("PluginManagementConfiguration Not Implemented");
  50. SetRequiredAttributes();
  51. }
  52. public static void SetPluginManager(IPluginManager manager)
  53. {
  54. __INSTANCE = manager;
  55. }
  56. private void SetRequiredAttributes()
  57. {
  58. foreach (PluginManagerConfigurationAttribute attribute in Configuration.Attributes)
  59. {
  60. if (attribute.Key == "WebControllersConfig")
  61. LoadWebControllers(attribute.Value);
  62. else if (attribute.Key == "WebViewsConfig")
  63. LoadWebViews(attribute.Value);
  64. else if(attribute.Key == "WebPartialViewsConfig")
  65. LoadWebPartialViews(attribute.Value);
  66. else if (attribute.Key == "MasterPagesConfig")
  67. LoadWebMasterPages(attribute.Value);
  68. }
  69. }
  70. #region Controllers
  71. private WebControllerList m_controllerList = null;
  72. private static Dictionary<string, Type> m_controllerCache = null;
  73. private static Dictionary<string, Type> Controllers
  74. {
  75. get
  76. {
  77. if (m_controllerCache == null)
  78. m_controllerCache = new Dictionary<string, Type>();
  79. return m_controllerCache;
  80. }
  81. }
  82. Type IPluginManager.GetControllerType(string controllerName)
  83. {
  84. if (!Controllers.ContainsKey(controllerName))
  85. {
  86. var controllers = from controller in m_controllerList where controller.Name == controllerName select controller;
  87. if (controllers.Count() > 0)
  88. {
  89. WebController webController = controllers.ElementAt(0);
  90. if (!webController.IsActive)
  91. return null;
  92. Type controllerType = Type.GetType(string.Format("{0}, {1}", webController.ControllerType, webController.Assembly));
  93. Controllers.Add(controllerName, controllerType);
  94. }
  95. else
  96. return null;
  97. }
  98. return Controllers[controllerName];
  99. }
  100. public void LoadWebControllers(string xmlsource)
  101. {
  102. m_controllerList = GetWebControllers(xmlsource);
  103. }
  104. private WebControllerList GetWebControllers(string xmlsource)
  105. {
  106. Stream stream = null;
  107. try
  108. {
  109. XmlSerializer serializer = new XmlSerializer(typeof(WebControllerList));
  110. stream = new FileStream(xmlsource, FileMode.Open);
  111. WebControllerList list = serializer.Deserialize(stream) as WebControllerList;
  112. return list;
  113. }
  114. catch (Exception ex)
  115. {
  116. throw ex;
  117. }
  118. finally
  119. {
  120. stream.Close();
  121. }
  122. }
  123. #endregion
  124. #region Views
  125. private WebViewList m_viewList = null;
  126. public WebView GetWebView(string viewName)
  127. {
  128. var views = from view in m_viewList where view.Name == viewName && view.IsActive select view;
  129. if (views.Count() == 0)
  130. return null;
  131. return views.ElementAt(0);
  132. }
  133. private void LoadWebViews(string sourceXml)
  134. {
  135. m_viewList = GetWebViewList(sourceXml);
  136. }
  137. private WebViewList GetWebViewList(string xmlsource)
  138. {
  139. Stream stream = null;
  140. try
  141. {
  142. XmlSerializer serializer = new XmlSerializer(typeof(WebViewList));
  143. stream = new FileStream(xmlsource, FileMode.Open);
  144. WebViewList list = serializer.Deserialize(stream) as WebViewList;
  145. return list;
  146. }
  147. catch (Exception ex)
  148. {
  149. throw ex;
  150. }
  151. finally
  152. {
  153. stream.Close();
  154. }
  155. }
  156. #endregion
  157. #region PartialViews
  158. private WebPartialViewList m_partialViewList = null;
  159. public WebPartialView GetPartialView(string partialViewName)
  160. {
  161. var partials = from partial in m_partialViewList where partial.Name == partialViewName select partial;
  162. if (partials.Count() == 0)
  163. return null;
  164. return partials.ElementAt(0);
  165. }
  166. private void LoadWebPartialViews(string sourceXml)
  167. {
  168. m_partialViewList = GetWebPartialViewList(sourceXml);
  169. }
  170. private WebPartialViewList GetWebPartialViewList(string xmlsource)
  171. {
  172. Stream stream = null;
  173. try
  174. {
  175. XmlSerializer serializer = new XmlSerializer(typeof(WebPartialViewList));
  176. stream = new FileStream(xmlsource, FileMode.Open);
  177. WebPartialViewList list = serializer.Deserialize(stream) as WebPartialViewList;
  178. return list;
  179. }
  180. catch (Exception ex)
  181. {
  182. throw ex;
  183. }
  184. finally
  185. {
  186. stream.Close();
  187. }
  188. }
  189. #endregion
  190. #region MasterPages
  191. private WebMasterPageList m_masterPageList = null;
  192. public WebMasterPage GetWebMasterPage(string masterPageName)
  193. {
  194. var masters = from master in m_masterPageList where master.Name == masterPageName && master.IsActive select master;
  195. if (masters.Count() == 0)
  196. return null;
  197. return masters.ElementAt(0);
  198. }
  199. private void LoadWebMasterPages(string sourceXml)
  200. {
  201. m_masterPageList = GetMasterPageList(sourceXml);
  202. }
  203. private WebMasterPageList GetMasterPageList(string xmlsource)
  204. {
  205. Stream stream = null;
  206. try
  207. {
  208. XmlSerializer serializer = new XmlSerializer(typeof(WebMasterPageList));
  209. stream = new FileStream(xmlsource, FileMode.Open);
  210. WebMasterPageList list = serializer.Deserialize(stream) as WebMasterPageList;
  211. return list;
  212. }
  213. catch (Exception ex)
  214. {
  215. throw ex;
  216. }
  217. finally
  218. {
  219. stream.Close();
  220. }
  221. }
  222. #endregion
  223. }
  224. }