Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
7.5 KiB

  1. using System;
  2. using System.IO;
  3. using System.Security.Principal;
  4. using System.Xml.Serialization;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Resources;
  8. using UDDI.API;
  9. using UDDI.API.Extensions;
  10. namespace UDDI.Tools
  11. {
  12. class Bootstrap
  13. {
  14. static string filename;
  15. static string username;
  16. static int Main( string[] args )
  17. {
  18. int retCode = 1; // assume error
  19. //
  20. // Check if CurrentUICulture needs to be overridden
  21. //
  22. UDDI.Localization.SetConsoleUICulture();
  23. Console.WriteLine( FormatFromResource( "BOOTSTRAP_COPYRIGHT_1" ) );
  24. Console.WriteLine( FormatFromResource( "BOOTSTRAP_COPYRIGHT_2" ) );
  25. Console.WriteLine();
  26. //
  27. // parse the command line
  28. //
  29. if(!ProcessCommandLine( args ) )
  30. return 1;
  31. WindowsPrincipal prin = new WindowsPrincipal( WindowsIdentity.GetCurrent() );
  32. try
  33. {
  34. ConnectionManager.Open( true, true );
  35. }
  36. catch
  37. {
  38. Console.WriteLine( FormatFromResource( "BOOTSTRAP_DB_CONNECT_FAILED" ) );
  39. return 1;
  40. }
  41. try
  42. {
  43. //
  44. // Setup the UDDI user credentials
  45. //
  46. Context.User.SetRole( prin );
  47. //
  48. // Verify that the user is a member of the administrators group
  49. //
  50. if( !Context.User.IsAdministrator )
  51. {
  52. //
  53. // 735728 - Show an error to the user and exit the program.
  54. //
  55. Console.WriteLine( FormatFromResource( "BOOTSTRAP_ACCESS_DENIED" ) );
  56. Console.WriteLine( FormatFromResource( "BOOTSTRAP_ADMIN_GROUP_ONLY" ) );
  57. Console.WriteLine( FormatFromResource( "BOOTSTRAP_USER_NOT_ADMIN" , WindowsIdentity.GetCurrent().Name ) );
  58. return 1;
  59. }
  60. if( null != username && 0 != username.Length )
  61. {
  62. //
  63. // The /u option was specified
  64. //
  65. Context.User.SetPublisherRole( username );
  66. if( !Context.User.IsRegistered )
  67. {
  68. //
  69. // 735728 - Show an error to the user and exit the program.
  70. //
  71. Console.WriteLine( FormatFromResource( "BOOTSTRAP_USER_NOT_REGISTERED" , username ) );
  72. return 1;
  73. }
  74. //
  75. // If the current user is not the same as the publisher, set up impersonation
  76. //
  77. if ( Context.User.ID != WindowsIdentity.GetCurrent().Name )
  78. Context.User.ImpersonatorID = WindowsIdentity.GetCurrent().Name;
  79. }
  80. else
  81. {
  82. //
  83. // Default is to save data under the default publisher
  84. //
  85. Context.User.ID = UDDI.Utility.GetDefaultPublisher();
  86. }
  87. //
  88. // If user is not system publisher, a temporary operator must be added to support pre-assigned key behavior
  89. //
  90. string operatorkey = System.Guid.NewGuid().ToString();
  91. if( Context.User.ID != UDDI.Utility.GetDefaultPublisher() )
  92. Context.User.SetAllowPreassignedKeys( true );
  93. XmlSerializer serializer = new XmlSerializer( typeof( UDDI.API.Extensions.Resources ) );
  94. //
  95. // Load the XML file.
  96. //
  97. Console.WriteLine( FormatFromResource( "BOOTSTRAP_PROCESSING_MSG" , filename, Context.User.ID ) );
  98. FileStream strm = new FileStream( filename, FileMode.Open, FileAccess.Read );
  99. Resources.Validate( strm );
  100. Resources resources = (Resources) serializer.Deserialize( strm );
  101. strm.Close();
  102. //
  103. // Save the TModel
  104. //
  105. Console.WriteLine( FormatFromResource( "BOOTSTRAP_SAVING" ) );
  106. //
  107. // Determine the number of tModels that we imported.
  108. //
  109. int tModelCount = 0;
  110. if( null != resources.TModelDetail)
  111. {
  112. tModelCount += resources.TModelDetail.TModels.Count;
  113. }
  114. if( null != resources.CategorizationSchemes )
  115. {
  116. foreach( CategorizationScheme scheme in resources.CategorizationSchemes )
  117. {
  118. if( null != scheme.TModel )
  119. {
  120. tModelCount ++;
  121. }
  122. }
  123. }
  124. Console.WriteLine( FormatFromResource( "BOOTSTRAP_TMODELS", tModelCount ) );
  125. if( null != resources.CategorizationSchemes )
  126. Console.WriteLine( FormatFromResource( "BOOTSTRAP_CATEGORIZATION_SCHEMES", resources.CategorizationSchemes.Count ) );
  127. else
  128. Console.WriteLine( FormatFromResource( "BOOTSTRAP_CATEGORIZATION_SCHEMES", 0 ) );
  129. if( null != resources.BusinessDetail )
  130. Console.WriteLine( FormatFromResource( "BOOTSTRAP_PROVIDERS", resources.BusinessDetail.BusinessEntities.Count ) );
  131. else
  132. Console.WriteLine( FormatFromResource( "BOOTSTRAP_PROVIDERS", 0 ) );
  133. if( null != resources.ServiceDetail )
  134. Console.WriteLine( FormatFromResource( "BOOTSTRAP_SERVICES", resources.ServiceDetail.BusinessServices.Count ) );
  135. else
  136. Console.WriteLine( FormatFromResource( "BOOTSTRAP_SERVICES", 0 ) );
  137. if( null != resources.BindingDetail )
  138. Console.WriteLine( FormatFromResource( "BOOTSTRAP_BINDINGS", resources.BindingDetail.BindingTemplates.Count ) );
  139. else
  140. Console.WriteLine( FormatFromResource( "BOOTSTRAP_BINDINGS", 0 ) );
  141. resources.Save();
  142. Console.WriteLine( FormatFromResource( "BOOTSTRAP_COMPLETE" ) );
  143. ConnectionManager.Commit();
  144. retCode = 0; // no error
  145. }
  146. catch( Exception e )
  147. {
  148. Console.WriteLine();
  149. Console.WriteLine( FormatFromResource( "BOOTSTRAP_FAILED" , e.Message ) );
  150. //
  151. // 735713 - Output an additional message if the user did not have permissions to modify an entity.
  152. //
  153. SqlException sqlException = e as SqlException;
  154. if( null != sqlException )
  155. {
  156. if( sqlException.Number - UDDI.Constants.ErrorTypeSQLOffset == ( int ) ErrorType.E_userMismatch )
  157. {
  158. Console.WriteLine( FormatFromResource( "ERROR_USER_MISMATCH" ) );
  159. }
  160. }
  161. ConnectionManager.Abort();
  162. }
  163. finally
  164. {
  165. ConnectionManager.Close();
  166. }
  167. return retCode;
  168. }
  169. private static bool ProcessCommandLine( string [] args )
  170. {
  171. bool bOK = false;
  172. for( int i = 0; i < args.Length; i ++ )
  173. {
  174. if( '-' == args[i][0] || '/' == args[i][0] )
  175. {
  176. string option = args[i].Substring( 1 );
  177. if( "help" == option.ToLower() || "?" == option )
  178. {
  179. break;
  180. }
  181. else if( "f" == option.ToLower() )
  182. {
  183. i++; // move to the next arg
  184. if( i >= args.Length )
  185. {
  186. break;
  187. }
  188. filename = args[i];
  189. bOK = true;
  190. }
  191. else if( "u" == option.ToLower() )
  192. {
  193. i++; // move to the next arg
  194. if( i >= args.Length )
  195. {
  196. break;
  197. }
  198. username = args[i];
  199. }
  200. else
  201. {
  202. DisplayUsage();
  203. return false;
  204. }
  205. }
  206. }
  207. if( !bOK )
  208. {
  209. DisplayUsage();
  210. return false;
  211. }
  212. return true;
  213. }
  214. static void DisplayUsage()
  215. {
  216. Console.WriteLine( FormatFromResource( "BOOTSTRAP_USAGE_1" ) );
  217. Console.WriteLine();
  218. Console.WriteLine( FormatFromResource( "BOOTSTRAP_USAGE_2" ) );
  219. Console.WriteLine();
  220. Console.WriteLine( FormatFromResource( "BOOTSTRAP_USAGE_3" ) );
  221. Console.WriteLine( FormatFromResource( "BOOTSTRAP_USAGE_4" ) );
  222. Console.WriteLine();
  223. }
  224. static string FormatFromResource( string resID, params object[] inserts )
  225. {
  226. try
  227. {
  228. string resourceStr = UDDI.Localization.GetString( resID );
  229. if( null != resourceStr )
  230. {
  231. string resultStr = string.Format( resourceStr, inserts );
  232. return resultStr;
  233. }
  234. return "String not specified in the resources: " + resID;
  235. }
  236. catch( Exception e )
  237. {
  238. return "FormatFromResource failed to load the resource string for ID: " + resID + " Reason: " + e.Message;
  239. }
  240. }
  241. }
  242. }