PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/BTSControl/Microsoft.BizTalk.ApplicationDeployment.CommandLine/GetInstanceCountCommand.cs

#
C# | 227 lines | 122 code | 22 blank | 83 comment | 9 complexity | 1a3474c1a7191506bb2e1ffeef7ca59f MD5 | raw file
  1. namespace Microsoft.BizTalk.ApplicationDeployment.CommandLine
  2. {
  3. using Microsoft.BizTalk.ApplicationDeployment;
  4. using Microsoft.BizTalk.ExplorerOM;
  5. using Microsoft.BizTalk.Operations;
  6. using Microsoft.BizTalk.Log;
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Specialized;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.Management;
  13. using System.Reflection;
  14. using System.Text;
  15. internal sealed class GetInstanceCountCommand : Command
  16. {
  17. public GetInstanceCountCommand(NameValueCollection nameValueArgs) : base(nameValueArgs)
  18. {
  19. }
  20. public override void Execute()
  21. {
  22. BtsCatalogExplorer explorer = null;
  23. try
  24. {
  25. this.Validate();
  26. string applicationName = base.Args["ApplicationName"];
  27. string server = base.Args["Server"];
  28. string database = base.Args["Database"];
  29. string formattedString = CommandResources.GetFormattedString(CommandResources.ResourceID.GetInstanceCount, new object[] { applicationName, server, database });
  30. base.WriteLogEntry(LogEntryType.Information, formattedString);
  31. explorer = new BtsCatalogExplorer();
  32. explorer.ConnectionString = ParameterHelper.GetConnectionString(server, database);
  33. Microsoft.BizTalk.ExplorerOM.Application application = explorer.Applications[applicationName];
  34. if (application == null)
  35. {
  36. throw new ApplicationException("Unable to find application named " + applicationName);
  37. }
  38. int instanceCount = getInstanceCount(applicationName, server, database);
  39. formattedString = CommandResources.GetFormattedString(CommandResources.ResourceID.GetInstanceCountSuccess, new object[] { applicationName, instanceCount });
  40. base.WriteLogEntry(LogEntryType.Information, formattedString);
  41. base.commandResult = new CommandResult();
  42. if (instanceCount > 0)
  43. {
  44. base.commandResult.ErrorCount += instanceCount;
  45. }
  46. }
  47. catch (Exception exception)
  48. {
  49. //Microsoft.BizTalk.ApplicationDeployment.Trace.WriteLine(exception.ToString(), new object[0]);
  50. base.WriteLogEntry(LogEntryType.Error, exception.Message);
  51. base.commandResult = new CommandResult(exception);
  52. if (((exception is OutOfMemoryException) || (exception is ExecutionEngineException)) || (exception is StackOverflowException))
  53. {
  54. throw;
  55. }
  56. }
  57. finally
  58. {
  59. if (explorer != null)
  60. {
  61. explorer.Dispose();
  62. explorer = null;
  63. }
  64. }
  65. }
  66. private int getInstanceCount(string applicationName, string MgmtDBServerName, string MgmtDBName)
  67. {
  68. int instanceCount = 0;
  69. // Load the Operations assembly that has all the types we need (found in GAC on BizTalk installations)
  70. Assembly operationsAssembly = Assembly.Load(new AssemblyName("Microsoft.BizTalk.Operations, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"));
  71. // Create an instance of the OperationsGroup class, passing in the MmgtDBServer and MgmtDBName
  72. Type operationsGroupType = operationsAssembly.GetType("Microsoft.BizTalk.Operations.OperationsGroup", true);
  73. Type[] operationsGroupCITypes = new Type[2];
  74. operationsGroupCITypes[0] = typeof(string);
  75. operationsGroupCITypes[1] = typeof(string);
  76. ConstructorInfo operationsGroupCI = operationsGroupType.GetConstructor(operationsGroupCITypes);
  77. object operationsGroupObject = operationsGroupCI.Invoke(new object[] { MgmtDBServerName, MgmtDBName });
  78. // Get types for the InstanceStatistic we hope to obtain
  79. Type instanceStatisticType = operationsAssembly.GetType("Microsoft.BizTalk.Operations.InstanceStatistic", true);
  80. PropertyInfo instanceStatisticApplicationPI = instanceStatisticType.GetProperty("Application");
  81. PropertyInfo instanceStatisticCountPI = instanceStatisticType.GetProperty("Count");
  82. // Get the method we need to invoke on the OperationsGroup object
  83. Type instanceGroupingCriteriaType = operationsAssembly.GetType("Microsoft.BizTalk.Operations.InstanceGroupingCriteria", true);
  84. Type[] getInstanceStatsMITypes = new Type[2];
  85. getInstanceStatsMITypes[0] = instanceGroupingCriteriaType;
  86. getInstanceStatsMITypes[1] = typeof(Int32);
  87. MethodInfo getInstanceStatsMI = operationsGroupType.GetMethod("GetInstanceStatistics", getInstanceStatsMITypes);
  88. // Invoke the method, passing an InstanceGroupingCriteria of 1 (Group by applications)
  89. // and a min value of 0 (no limit as to the number of instances queried)
  90. object[] getInstanceStatsParams = new object[2];
  91. getInstanceStatsParams[0] = 1;
  92. getInstanceStatsParams[1] = 0;
  93. IEnumerable numerable = (IEnumerable)getInstanceStatsMI.Invoke(operationsGroupObject, getInstanceStatsParams);
  94. foreach (object statisticObj in numerable)
  95. {
  96. // We have statistics for an application (we will not have a statisticsObject if there are no instances)
  97. // but is it the application that we care about?
  98. string appName = (string)instanceStatisticApplicationPI.GetValue(statisticObj,null);
  99. if(appName.ToUpperInvariant().Equals(applicationName.ToUpperInvariant()))
  100. {
  101. instanceCount = (int)instanceStatisticCountPI.GetValue(statisticObj, null);
  102. }
  103. }
  104. MethodInfo operationsGroupDisposeMI = operationsGroupType.GetMethod("Dispose");
  105. operationsGroupDisposeMI.Invoke(operationsGroupObject, null);
  106. return instanceCount;
  107. // Code from Microsoft.BizTalk.Administration.SnapIn.Forms.GroupHub.InstancesGrpByStatusStatisticLoader.ExecuteTaskOnAsyncThread()
  108. // called from Microsoft.BizTalk.Administration.SnapIn.Forms.GroupHub.HubPage.LoadStatistics()
  109. //using (OperationsGroup group = new OperationsGroup(MgmtDBServerName, MgmtDBName))
  110. //{
  111. // foreach (InstanceStatistic statistic in group.GetInstanceStatistics(this.criteria, this.filter, 0))
  112. // {
  113. // switch (statistic.Filter.InstanceStatus.Value)
  114. // {
  115. // case InstanceStatus.SuspendedNotResumable:
  116. // {
  117. // //this.suspNonResumableCount += statistic.Count;
  118. // continue;
  119. // }
  120. // case InstanceStatus.Reserved1:
  121. // {
  122. // //this.inBreakpointCount += statistic.Count;
  123. // continue;
  124. // }
  125. // case InstanceStatus.Scheduled:
  126. // {
  127. // //this.scheduledCount += statistic.Count;
  128. // continue;
  129. // }
  130. // case InstanceStatus.ReadyToRun:
  131. // {
  132. // //this.readyToRunCount += statistic.Count;
  133. // continue;
  134. // }
  135. // case InstanceStatus.Active:
  136. // {
  137. // //this.activeCount += statistic.Count;
  138. // continue;
  139. // }
  140. // case InstanceStatus.Suspended:
  141. // {
  142. // //this.suspResumableCount += statistic.Count;
  143. // continue;
  144. // }
  145. // case InstanceStatus.Dehydrated:
  146. // break;
  147. // default:
  148. // throw new InvalidOperationException();
  149. // }
  150. // if (!statistic.Filter.Class.Set)
  151. // {
  152. // throw new InvalidOperationException();
  153. // }
  154. // ServiceClass class2 = statistic.Filter.Class.Value;
  155. // if (class2 <= ServiceClass.Messaging)
  156. // {
  157. // switch (class2)
  158. // {
  159. // case ServiceClass.Orchestration:
  160. // goto Label_0117;
  161. // case ServiceClass.Messaging:
  162. // goto Label_00FE;
  163. // }
  164. // goto Label_0130;
  165. // }
  166. // if ((class2 != ServiceClass.MSMQT) && (class2 != ServiceClass.NonCreatableReceiver))
  167. // {
  168. // goto Label_0130;
  169. // }
  170. // Label_00FE:
  171. // //this.retryingCount += statistic.Count;
  172. // continue;
  173. // Label_0117:
  174. // //this.dehydratedOrchCount += statistic.Count;
  175. // continue;
  176. // Label_0130:
  177. // //throw new InvalidOperationException();
  178. // bool stop = true;
  179. // }
  180. //}
  181. }
  182. protected override CommandLineArgDescriptorList GetParameterDescriptors()
  183. {
  184. CommandLineArgDescriptor[] collection = new CommandLineArgDescriptor[] {
  185. new CommandLineArgDescriptor(true, "ApplicationName", CommandResources.GetString(CommandResources.ResourceID.ParamDesc_RequiredApplicationName), CommandLineArgDescriptor.ArgumentType.String, 1, 1),
  186. new CommandLineArgDescriptor(true, "Server", CommandResources.GetString(CommandResources.ResourceID.ParamDesc_Server), CommandLineArgDescriptor.ArgumentType.String),
  187. new CommandLineArgDescriptor(true, "Database", CommandResources.GetString(CommandResources.ResourceID.ParamDesc_Database), CommandLineArgDescriptor.ArgumentType.String) };
  188. CommandLineArgDescriptorList list = new CommandLineArgDescriptorList();
  189. list.AddRange(collection);
  190. return list;
  191. }
  192. public override void Validate()
  193. {
  194. ParameterHelper.ValidateServerDatabase(base.Args);
  195. ParameterHelper.ValidateApplicationName(base.Args);
  196. }
  197. public override string Name
  198. {
  199. get
  200. {
  201. return "GetInstanceCount";
  202. }
  203. }
  204. }
  205. }