PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C# | 197 lines | 179 code | 15 blank | 3 comment | 38 complexity | 5a87be03fe0740d40cc540a50e7eafaa 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 RestartHostInstancesCommand : Command
  16. {
  17. public RestartHostInstancesCommand(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.RestartHostInstances, 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. ArrayList distinctHosts = new ArrayList();
  39. getHostsFromApplication(distinctHosts, application);
  40. restartAlreadyRunningInProcHostInstances(explorer, distinctHosts);
  41. formattedString = CommandResources.GetFormattedString(CommandResources.ResourceID.RestartHostInstancesSuccess, new object[] { applicationName });
  42. base.WriteLogEntry(LogEntryType.Information, formattedString);
  43. base.commandResult = new CommandResult();
  44. }
  45. catch (Exception exception)
  46. {
  47. //Microsoft.BizTalk.ApplicationDeployment.Trace.WriteLine(exception.ToString(), new object[0]);
  48. base.WriteLogEntry(LogEntryType.Error, exception.Message);
  49. base.commandResult = new CommandResult(exception);
  50. if (((exception is OutOfMemoryException) || (exception is ExecutionEngineException)) || (exception is StackOverflowException))
  51. {
  52. throw;
  53. }
  54. }
  55. finally
  56. {
  57. if (explorer != null)
  58. {
  59. explorer.Dispose();
  60. explorer = null;
  61. }
  62. }
  63. }
  64. private void restartAlreadyRunningInProcHostInstances(BtsCatalogExplorer explorer, ArrayList distinctHosts)
  65. {
  66. bool oneOrMoreFailedToRestart = false;
  67. StringBuilder sbuilder = new StringBuilder();
  68. foreach (string hostname in distinctHosts)
  69. {
  70. ManagementScope scope = new ManagementScope(@"root\MicrosoftBizTalkServer");
  71. EnumerationOptions options = new EnumerationOptions();
  72. options.ReturnImmediately = false;
  73. ManagementObjectSearcher searcher = null;
  74. ManagementObjectCollection managementObjectCollection = null;
  75. try
  76. {
  77. SelectQuery query = new SelectQuery("MSBTS_HostInstance",
  78. string.Format("HostName=\"{0}\"", hostname));
  79. searcher = new ManagementObjectSearcher(scope, query, options);
  80. managementObjectCollection = searcher.Get();
  81. foreach (ManagementObject hostInstance in managementObjectCollection)
  82. {
  83. uint num = (uint)hostInstance["ServiceState"];
  84. if (num == 4)
  85. {
  86. // it is already running, restart it.
  87. object[] objparams = new object[0];
  88. hostInstance.InvokeMethod("Stop", objparams);
  89. hostInstance.InvokeMethod("Start", objparams);
  90. }
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. Trace.WriteLine(ex.ToString());
  96. sbuilder.AppendLine(ex.ToString());
  97. oneOrMoreFailedToRestart = true;
  98. }
  99. finally
  100. {
  101. if (searcher != null)
  102. {
  103. searcher.Dispose();
  104. searcher = null;
  105. }
  106. if (managementObjectCollection != null)
  107. {
  108. managementObjectCollection.Dispose();
  109. managementObjectCollection = null;
  110. }
  111. }
  112. }
  113. if (oneOrMoreFailedToRestart)
  114. {
  115. throw new ApplicationException("One or more host instances failed to restart.", new ApplicationException(sbuilder.ToString()));
  116. }
  117. }
  118. // Code from Microsoft.BizTalk.Administration.SnapIn, Version=3.0.1.0
  119. private static void getHostsFromApplication(ArrayList hosts, IBizTalkApplication application)
  120. {
  121. foreach (IBtsOrchestration orchestration in application.Orchestrations)
  122. {
  123. if (((orchestration.Host != null) && (orchestration.Host.Type == HostType.InProcess)) && !hosts.Contains(orchestration.Host.Name))
  124. {
  125. hosts.Add(orchestration.Host.Name);
  126. }
  127. }
  128. foreach (ISendPort port in application.SendPorts)
  129. {
  130. ISendHandler sendHandler = null;
  131. if (port.PrimaryTransport != null)
  132. {
  133. sendHandler = ((ITransportInfo2)port.PrimaryTransport).SendHandler;
  134. if (((sendHandler != null) && (sendHandler.Host.Type == HostType.InProcess)) && !hosts.Contains(sendHandler.Host.Name))
  135. {
  136. hosts.Add(sendHandler.Host.Name);
  137. }
  138. }
  139. if (port.SecondaryTransport != null)
  140. {
  141. sendHandler = ((ITransportInfo2)port.SecondaryTransport).SendHandler;
  142. if (((sendHandler != null) && (sendHandler.Host.Type == HostType.InProcess)) && !hosts.Contains(sendHandler.Host.Name))
  143. {
  144. hosts.Add(sendHandler.Host.Name);
  145. }
  146. }
  147. }
  148. foreach (IReceivePort port2 in application.ReceivePorts)
  149. {
  150. foreach (IReceiveLocation location in port2.ReceiveLocations)
  151. {
  152. if (((location.ReceiveHandler != null) && (location.ReceiveHandler.Host.Type == HostType.InProcess)) && !hosts.Contains(location.ReceiveHandler.Host.Name))
  153. {
  154. hosts.Add(location.ReceiveHandler.Host.Name);
  155. }
  156. }
  157. }
  158. }
  159. protected override CommandLineArgDescriptorList GetParameterDescriptors()
  160. {
  161. CommandLineArgDescriptor[] collection = new CommandLineArgDescriptor[] {
  162. new CommandLineArgDescriptor(true, "ApplicationName", CommandResources.GetString(CommandResources.ResourceID.ParamDesc_RequiredApplicationName), CommandLineArgDescriptor.ArgumentType.String, 1, 1),
  163. new CommandLineArgDescriptor(true, "Server", CommandResources.GetString(CommandResources.ResourceID.ParamDesc_Server), CommandLineArgDescriptor.ArgumentType.String),
  164. new CommandLineArgDescriptor(true, "Database", CommandResources.GetString(CommandResources.ResourceID.ParamDesc_Database), CommandLineArgDescriptor.ArgumentType.String) };
  165. CommandLineArgDescriptorList list = new CommandLineArgDescriptorList();
  166. list.AddRange(collection);
  167. return list;
  168. }
  169. public override void Validate()
  170. {
  171. ParameterHelper.ValidateServerDatabase(base.Args);
  172. ParameterHelper.ValidateApplicationName(base.Args);
  173. }
  174. public override string Name
  175. {
  176. get
  177. {
  178. return "RestartHostInstances";
  179. }
  180. }
  181. }
  182. }