PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C# | 143 lines | 130 code | 9 blank | 4 comment | 29 complexity | 73c92095e0ddecd2f89e0f859c33f303 MD5 | raw file
  1. namespace Microsoft.BizTalk.ApplicationDeployment.CommandLine
  2. {
  3. using Microsoft.BizTalk.ApplicationDeployment;
  4. using Microsoft.BizTalk.BtsDbVersion;
  5. using Microsoft.BizTalk.Deployment;
  6. using Microsoft.BizTalk.ExplorerOM;
  7. using System;
  8. using System.Collections.Specialized;
  9. using System.Data.SqlClient;
  10. using System.Diagnostics;
  11. using System.Globalization;
  12. using System.Net;
  13. using System.Reflection;
  14. using System.Text;
  15. using System.Text.RegularExpressions;
  16. internal static class ParameterHelper
  17. {
  18. public static string ByteArrayToHexString(byte[] bytes)
  19. {
  20. StringBuilder builder = new StringBuilder();
  21. if (bytes != null)
  22. {
  23. foreach (byte num in bytes)
  24. {
  25. builder.Append(num.ToString("x2", CultureInfo.InvariantCulture));
  26. }
  27. }
  28. return builder.ToString();
  29. }
  30. private static void CheckDatabaseCompatibility(string serverName, string databaseName)
  31. {
  32. BtsDbCompatibility compatibility;
  33. BizTalkDBVersion version = new BizTalkDBVersion();
  34. try
  35. {
  36. compatibility = version.CheckCompatibility(serverName, databaseName, "Management DB", "3.6.1.0");
  37. }
  38. catch (Exception exception)
  39. {
  40. throw new CommandException(CommandResources.GetFormattedString(CommandResources.ResourceID.UnableToValidateDatabase, new object[] { databaseName, serverName }), exception);
  41. }
  42. switch (compatibility)
  43. {
  44. case BtsDbCompatibility.BtsDbFullyCompatible:
  45. case BtsDbCompatibility.BtsDbPartiallyCompatible:
  46. return;
  47. case BtsDbCompatibility.BtsDbIncompatible:
  48. case BtsDbCompatibility.BtsDbNonBiztalkDatabase:
  49. case BtsDbCompatibility.BtsDbBlankDatabase:
  50. throw new CommandLineArgumentException(CommandResources.GetString(CommandResources.ResourceID.IncompatibleDbVersion), "Database", System.Diagnostics.TraceLevel.Error);
  51. }
  52. }
  53. public static string GetConnectionString(string server, string database)
  54. {
  55. // ////Microsoft.BizTalk.ApplicationDeployment.Trace.WriteLine("Building SQL connection string...", new object[0]);
  56. SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
  57. builder.DataSource = server;
  58. builder.InitialCatalog = database;
  59. builder.IntegratedSecurity = true;
  60. builder.ApplicationName = Assembly.GetExecutingAssembly().GetName().Name;
  61. string str = builder.ToString();
  62. // ////Microsoft.BizTalk.ApplicationDeployment.Trace.WriteLine("ConnectionString: " + str, new object[0]);
  63. return str;
  64. }
  65. public static void ValidateApplicationName(NameValueCollection nameValueArgs)
  66. {
  67. // ////Microsoft.BizTalk.ApplicationDeployment.Trace.WriteLine("Validating ApplicationName parameter...", new object[0]);
  68. if (nameValueArgs["ApplicationName"] != null)
  69. {
  70. nameValueArgs.Set("ApplicationName", nameValueArgs["ApplicationName"].Trim());
  71. }
  72. if ((nameValueArgs["ApplicationName"] == null) || (nameValueArgs["ApplicationName"].Length == 0))
  73. {
  74. string server = nameValueArgs["Server"];
  75. string database = nameValueArgs["Database"];
  76. BtsCatalogExplorer explorer = new BtsCatalogExplorer();
  77. explorer.ConnectionString = GetConnectionString(server, database);
  78. Microsoft.BizTalk.ExplorerOM.Application defaultApplication = explorer.DefaultApplication;
  79. if (defaultApplication != null)
  80. {
  81. nameValueArgs.Set("ApplicationName", defaultApplication.Name);
  82. }
  83. }
  84. }
  85. public static void ValidateDatabase(string database)
  86. {
  87. Regex regex = new Regex(@"^[a-zA-Z]([a-zA-Z]|[0-9]|[!@#\$%\^&'\)\(\.\-_\{\}~\.]){0,62}$");
  88. if (!regex.Match(database).Success)
  89. {
  90. throw new CommandLineArgumentException(CommandResources.GetFormattedString(CommandResources.ResourceID.DatabaseInvalid, new object[] { database }), "Database", System.Diagnostics.TraceLevel.Error);
  91. }
  92. }
  93. public static void ValidateServer(string server)
  94. {
  95. IPAddress address;
  96. if (!IPAddress.TryParse(server, out address))
  97. {
  98. Regex regex = new Regex(@"^((np|tcp|spx|adsp|rpc|vines):)?([a-zA-Z]|[0-9]|[!@#\$%\^&'\)\(\.\-_\{\}~\.\\]){0,256}((,[0-9]{1,5})|(,(ncacn_np|ncacn_ip_tcp|ncacn_nb_nb|ncacn_spx|ncacn_vns_spp|ncadg_ip_udp|ncadg_ipx|ncalrpc)))?(\\([a-zA-Z]|[0-9]|[!@#\$%\^&'\)\(\.\-_\{\}~\.\\]){0,256})?$");
  99. if (!regex.Match(server).Success)
  100. {
  101. throw new CommandLineArgumentException(CommandResources.GetFormattedString(CommandResources.ResourceID.ServerInvalid, new object[] { server }), "Server", System.Diagnostics.TraceLevel.Error);
  102. }
  103. }
  104. }
  105. public static void ValidateServerDatabase(NameValueCollection nameValueArgs)
  106. {
  107. //Microsoft.BizTalk.ApplicationDeployment.Trace.WriteLine("Validating Server and Database parameters...", new object[0]);
  108. if ((nameValueArgs["Server"] != null) && (nameValueArgs["Server"].Length > 0))
  109. {
  110. if ((nameValueArgs["Database"] == null) || (nameValueArgs["Database"].Length == 0))
  111. {
  112. throw new CommandLineArgumentException(CommandResources.GetString(CommandResources.ResourceID.DatabaseNotSpecified), "Database", System.Diagnostics.TraceLevel.Error);
  113. }
  114. }
  115. else
  116. {
  117. nameValueArgs.Set("Server", Environment.MachineName);
  118. if ((nameValueArgs["Database"] == null) || (nameValueArgs["Database"].Length == 0))
  119. {
  120. ConfigurationDatabase database = new ConfigurationDatabase();
  121. if ((database.Server.Length == 0) || (database.Database.Length == 0))
  122. {
  123. throw new CommandLineArgumentException(CommandResources.GetString(CommandResources.ResourceID.DefaultServerDatabaseFailed), "Database", System.Diagnostics.TraceLevel.Error);
  124. }
  125. nameValueArgs.Set("Server", database.Server);
  126. nameValueArgs.Set("Database", database.Database);
  127. }
  128. }
  129. ValidateServer(nameValueArgs["Server"]);
  130. ValidateDatabase(nameValueArgs["Database"]);
  131. CheckDatabaseCompatibility(nameValueArgs["Server"], nameValueArgs["Database"]);
  132. }
  133. }
  134. }