PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/stackhash/Plugins/StackHashBugTrackerPlugInDemo/StackHashBugTrackerPlugInDemo/FaultDatabaseControl.cs

#
C# | 232 lines | 100 code | 38 blank | 94 comment | 2 complexity | 6e80bdb1f9a319b6694d82376366dffd MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections.Specialized;
  6. using System.Globalization;
  7. using System.Diagnostics.CodeAnalysis;
  8. using StackHashBugTrackerInterfaceV1;
  9. [assembly: CLSCompliant(true)]
  10. namespace StackHashBugTrackerPlugInDemo
  11. {
  12. /// <summary>
  13. /// StackHash creates one FaultDatabaseControl object when the plug-in is loaded.
  14. /// The Control object is responsible for managing Contexts.
  15. /// The Control object reports the default properties for a context and general diagnostics
  16. /// regarding the load of the plug-in.
  17. /// PlugInDefaultProperties and PlugInDiagnostics are not interpretted or persisted by StackHash.
  18. /// PlugInDefaultProperties offers a means for StackHash to present the settings to the StackHash client
  19. /// user when a plug-in is assigned to a StackHash profile.
  20. ///
  21. /// If an exception is thrown by ANY of the plug-in interface methods to properties then the plug-in
  22. /// is placed in a faulted state and no further calls will be placed to the DLL Add and Update
  23. /// methods. The StackHash profile will need to be deactivated and reactivated to continue reporting.
  24. /// In the event that the DLL does fault, any new report notifications will be stored by StackHash
  25. /// until the fault is fixed or a full manual sync report is initiated by the user.
  26. /// </summary>
  27. public class FaultDatabaseControl : MarshalByRefObject, IBugTrackerV1Control
  28. {
  29. #region Fields
  30. private static readonly string s_PlugInName = "FaultDatabase";
  31. private static readonly string s_PlugInDescription = "StackHash demo plug-in. Merely logs notifications to the StackHash diagnostics log.";
  32. private static Uri s_HelpUrl = new Uri("http://www.stackhash.com");
  33. private Exception m_LastException = null; // The last recorded exception.
  34. // Keep a track of the number of context instances that are active.
  35. private List<FaultDatabaseContext> m_ActiveContexts = new List<FaultDatabaseContext>();
  36. /// <summary>
  37. /// Define the property values here. You can define as many name value pairs as required by the plug-in.
  38. /// The StackHash client will display all of the properties that are returned by the plug-in and will
  39. /// persist the settings to the StackHash settings file.
  40. /// LogVerbose and EnableContext below are just examples of the use of the properties field.
  41. /// </summary>
  42. private NameValueCollection m_DefaultProperties = new NameValueCollection();
  43. private static readonly string s_LogVerboseProperty = "LogVerbose";
  44. private static readonly string s_EnableContextProperty = "EnableContext";
  45. /// <summary>
  46. /// Define the diagnostics values here. You can define as many name value pairs as required by the plug-in.
  47. /// The StackHash client will display all of the diagnostics that are returned by the plug-in.
  48. /// These values are not persisted by StackHash and cannot be changed by the user in the StackHash client.
  49. /// Examples are "LastException" and "NumberOfActiveContexts".
  50. /// </summary>
  51. private NameValueCollection m_Diagnostics = new NameValueCollection();
  52. private static readonly string s_LastExceptionDiagnostic = "LastException";
  53. private static readonly string s_NumberOfActiveContextsDiagnostic = "NumberOfActiveContexts";
  54. #endregion
  55. #region Constructors
  56. /// <summary>
  57. /// A public default constructor must be defined.
  58. /// </summary>
  59. public FaultDatabaseControl()
  60. {
  61. m_DefaultProperties[s_LogVerboseProperty] = "0"; // 0 = Normal logging, 1 = Verbose logging.
  62. m_DefaultProperties[s_EnableContextProperty] = "1"; // 0 = Enable the plug-in, 1 = Disable plug-in.
  63. // You can use the static LogMessage to log a message to the StackHash service log file which is written to
  64. // c:\ProgramData\StackHash on Vista and Win7 and to c:\Documents and Settings\All Users\Application Data\StackHash
  65. // on XP systems.
  66. BugTrackerTrace.LogMessage(BugTrackerTraceSeverity.Information, s_PlugInName, "Control Interface created");
  67. }
  68. #endregion Constructors
  69. #region Properties
  70. /// <summary>
  71. /// This must be unique across all plug-ins. A good name would be a well known abbreviation for the
  72. /// bug tracking system that this DLL interfaces to. e.g. Fogbugz, Jira etc... If you have, or may have,
  73. /// multiple DLLs that deal with the same bug tracking system then choose a more unique name.
  74. /// </summary>
  75. public string PlugInName
  76. {
  77. get
  78. {
  79. return s_PlugInName;
  80. }
  81. }
  82. /// <summary>
  83. /// A description of the plug-ins purpose.
  84. /// </summary>
  85. public String PlugInDescription
  86. {
  87. get
  88. {
  89. return s_PlugInDescription;
  90. }
  91. }
  92. /// <summary>
  93. /// The help Url will be displayed in the StackHash client so that users of the plug-in can get more
  94. /// help related to the plug-in usage, properties and the meaning of diagnostics.
  95. /// </summary>
  96. public Uri HelpUrl
  97. {
  98. get
  99. {
  100. return s_HelpUrl;
  101. }
  102. }
  103. /// <summary>
  104. /// Indicate here whether the plug-in requires feedback to update the bug reference field in StackHash.
  105. /// You should return false here if the plug-in does not change the bug reference in StackHash. If this
  106. /// is set to true, the EventUpdated and EventAdded can choose to return a bug reference which is stored
  107. /// in the StackHash database along with the event data.
  108. /// Attempting to assign more than one plug-in to a context, both of which set this field to true will
  109. /// result in failure of the profile to activate. i.e. only one plug-in can be loaded that sets the bug
  110. /// reference at any time.
  111. /// </summary>
  112. [SuppressMessage("Microsoft.Naming", "CA1702")]
  113. public bool PlugInSetsBugReference
  114. {
  115. get
  116. {
  117. return true;
  118. }
  119. }
  120. /// <summary>
  121. /// Default settings for the plug-in. The StackHash client will initially use these settings
  122. /// when assigning a plug-in for use by a service profile.
  123. /// The defaults can be any string values. These values will be potentially changed by the user at the
  124. /// StackHash client so endeavour to make the settings intuitive and easy to change.
  125. /// When it comes to using properties, you want to allow upper/lower case or short variants. e.g. False, false, F, 0
  126. /// all might mean the same thing.
  127. /// Note that StackHash does not interpret or otherwise use plug-in property settings except to display them in the
  128. /// StackHash client and to persist them for use after a service restart.
  129. /// </summary>
  130. public NameValueCollection PlugInDefaultProperties
  131. {
  132. get
  133. {
  134. return m_DefaultProperties;
  135. }
  136. }
  137. /// <summary>
  138. /// The plug-in diagnostics are specific to the plug-in but are displayed at the StackHash client
  139. /// to aid plug-in development.
  140. /// The diagnostics are not changeable by the user, they merely report the situation in the DLL at
  141. /// present.
  142. /// The PlugInDiagnostics should report general global information about the state of the plug-in,
  143. /// e.g. the number of Context's that are active and any failure information.
  144. /// Examples might be "LastException" and "NumberOfActiveContexts".
  145. /// StackHash does not persist diagnostics.
  146. /// </summary>
  147. public NameValueCollection PlugInDiagnostics
  148. {
  149. get
  150. {
  151. // Add other diagnostics here.
  152. if (m_LastException == null)
  153. m_Diagnostics[s_LastExceptionDiagnostic] = "No Error";
  154. else
  155. m_Diagnostics[s_LastExceptionDiagnostic] = m_LastException.ToString();
  156. m_Diagnostics[s_NumberOfActiveContextsDiagnostic] = m_ActiveContexts.Count.ToString(CultureInfo.InvariantCulture);
  157. return m_Diagnostics;
  158. }
  159. }
  160. #endregion Properties
  161. #region Methods
  162. /// <summary>
  163. /// Called by the StackHash client to create a context. The StackHash client will not directly
  164. /// instantiate an instance of the Context class. This gives the plug-in the opportunity to
  165. /// reject instance creation.
  166. /// A context will have its own settings.
  167. /// The context will be created by StackHash when the StackHash profile is activated. This will be
  168. /// on service startup or may happen when the user manually activates the profile at the StackHash
  169. /// client.
  170. /// </summary>
  171. /// <returns>Reference to the created instance.</returns>
  172. public IBugTrackerV1Context CreateContext()
  173. {
  174. BugTrackerTrace.LogMessage(BugTrackerTraceSeverity.Information, s_PlugInName, "Creating context");
  175. FaultDatabaseContext newContext = new FaultDatabaseContext(this);
  176. m_ActiveContexts.Add(newContext);
  177. return newContext;
  178. }
  179. /// <summary>
  180. /// Releases a context that is no longer required.
  181. /// The context will be released by StackHash when the StackHash profile is deactivated. This will be
  182. /// on service closedown or may happen when the user manually deactivates the profile at the StackHash
  183. /// client.
  184. /// Once a context has been released, StackHash will no longer attempt to invoke it.
  185. /// </summary>
  186. /// <param name="context">The context to release.</param>
  187. public void ReleaseContext(IBugTrackerV1Context context)
  188. {
  189. BugTrackerTrace.LogMessage(BugTrackerTraceSeverity.Information, s_PlugInName, "Releasing context");
  190. m_ActiveContexts.Remove(context as FaultDatabaseContext);
  191. }
  192. #endregion Methods
  193. }
  194. }