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

/main/src/core/MonoDevelop.Core/MonoDevelop.Core/Runtime.cs

https://github.com/tritao/monodevelop
C# | 289 lines | 212 code | 45 blank | 32 comment | 28 complexity | ef3ebf790fff1812c3a1e96f3b0e9485 MD5 | raw file
  1. //
  2. // Runtime.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Linq;
  30. using System.Runtime.InteropServices;
  31. using System.Text;
  32. using System.Threading;
  33. using Mono.Addins;
  34. using Mono.Addins.Setup;
  35. using MonoDevelop.Core;
  36. using MonoDevelop.Core.Assemblies;
  37. using MonoDevelop.Core.Execution;
  38. using MonoDevelop.Core.Instrumentation;
  39. using MonoDevelop.Core.Setup;
  40. namespace MonoDevelop.Core
  41. {
  42. public static class Runtime
  43. {
  44. static ProcessService processService;
  45. static SystemAssemblyService systemAssemblyService;
  46. static AddinSetupService setupService;
  47. static ApplicationService applicationService;
  48. static bool initialized;
  49. public static void Initialize (bool updateAddinRegistry)
  50. {
  51. if (initialized)
  52. return;
  53. Counters.RuntimeInitialization.BeginTiming ();
  54. SetupInstrumentation ();
  55. // Set a default sync context
  56. if (SynchronizationContext.Current == null)
  57. SynchronizationContext.SetSynchronizationContext (new SynchronizationContext ());
  58. AddinManager.AddinLoadError += OnLoadError;
  59. AddinManager.AddinLoaded += OnLoad;
  60. AddinManager.AddinUnloaded += OnUnload;
  61. try {
  62. Counters.RuntimeInitialization.Trace ("Initializing Addin Manager");
  63. AddinManager.Initialize (
  64. PropertyService.Locations.Config,
  65. PropertyService.Locations.Addins,
  66. PropertyService.Locations.Cache);
  67. AddinManager.InitializeDefaultLocalizer (new DefaultAddinLocalizer ());
  68. if (updateAddinRegistry)
  69. AddinManager.Registry.Update (null);
  70. setupService = new AddinSetupService (AddinManager.Registry);
  71. Counters.RuntimeInitialization.Trace ("Initialized Addin Manager");
  72. //have to do this after the addin service is initialized
  73. if (UserDataMigrationService.HasSource) {
  74. Counters.RuntimeInitialization.Trace ("Migrating User Data from MD " + UserDataMigrationService.SourceVersion);
  75. UserDataMigrationService.StartMigration ();
  76. }
  77. RegisterAddinRepositories ();
  78. Counters.RuntimeInitialization.Trace ("Initializing Assembly Service");
  79. systemAssemblyService = new SystemAssemblyService ();
  80. systemAssemblyService.Initialize ();
  81. initialized = true;
  82. } catch (Exception ex) {
  83. Console.WriteLine (ex);
  84. AddinManager.AddinLoadError -= OnLoadError;
  85. AddinManager.AddinLoaded -= OnLoad;
  86. AddinManager.AddinUnloaded -= OnUnload;
  87. } finally {
  88. Counters.RuntimeInitialization.EndTiming ();
  89. }
  90. }
  91. static void RegisterAddinRepositories ()
  92. {
  93. var validUrls = Enum.GetValues (typeof(UpdateLevel)).Cast<UpdateLevel> ().Select (v => setupService.GetMainRepositoryUrl (v)).ToList ();
  94. // Remove old repositories
  95. var reps = setupService.Repositories;
  96. foreach (AddinRepository rep in reps.GetRepositories ()) {
  97. if (rep.Url.StartsWith ("http://go-mono.com/md/") ||
  98. (rep.Url.StartsWith ("http://monodevelop.com/files/addins/")) ||
  99. (rep.Url.StartsWith ("http://addins.monodevelop.com/") && !validUrls.Contains (rep.Url)))
  100. reps.RemoveRepository (rep.Url);
  101. }
  102. if (!setupService.IsMainRepositoryRegistered (UpdateLevel.Stable)) {
  103. setupService.RegisterMainRepository (UpdateLevel.Stable, true);
  104. setupService.RegisterMainRepository (UpdateLevel.Beta, true);
  105. }
  106. if (!setupService.IsMainRepositoryRegistered (UpdateLevel.Beta))
  107. setupService.RegisterMainRepository (UpdateLevel.Beta, false);
  108. if (!setupService.IsMainRepositoryRegistered (UpdateLevel.Alpha))
  109. setupService.RegisterMainRepository (UpdateLevel.Alpha, false);
  110. }
  111. internal static string GetRepoUrl (string quality)
  112. {
  113. string platform;
  114. if (PropertyService.IsWindows)
  115. platform = "Win32";
  116. else if (PropertyService.IsMac)
  117. platform = "Mac";
  118. else
  119. platform = "Linux";
  120. return "http://addins.monodevelop.com/" + quality + "/" + platform + "/" + AddinManager.CurrentAddin.Version + "/main.mrep";
  121. }
  122. static void SetupInstrumentation ()
  123. {
  124. InstrumentationService.Enabled = PropertyService.Get ("MonoDevelop.EnableInstrumentation", false);
  125. if (InstrumentationService.Enabled) {
  126. LoggingService.LogInfo ("Instrumentation Service started");
  127. try {
  128. int port = InstrumentationService.PublishService ();
  129. LoggingService.LogInfo ("Instrumentation available at port " + port);
  130. } catch (Exception ex) {
  131. LoggingService.LogError ("Instrumentation service could not be published", ex);
  132. }
  133. }
  134. PropertyService.AddPropertyHandler ("MonoDevelop.EnableInstrumentation", delegate {
  135. InstrumentationService.Enabled = PropertyService.Get ("MonoDevelop.EnableInstrumentation", false);
  136. });
  137. }
  138. static void OnLoadError (object s, AddinErrorEventArgs args)
  139. {
  140. string msg = "Add-in error (" + args.AddinId + "): " + args.Message;
  141. LoggingService.LogError (msg, args.Exception);
  142. }
  143. static void OnLoad (object s, AddinEventArgs args)
  144. {
  145. Counters.AddinsLoaded.Inc ("Add-in loaded: " + args.AddinId);
  146. }
  147. static void OnUnload (object s, AddinEventArgs args)
  148. {
  149. Counters.AddinsLoaded.Dec ("Add-in unloaded: " + args.AddinId);
  150. }
  151. internal static bool Initialized {
  152. get { return initialized; }
  153. }
  154. public static void Shutdown ()
  155. {
  156. if (!initialized)
  157. return;
  158. if (ShuttingDown != null)
  159. ShuttingDown (null, EventArgs.Empty);
  160. PropertyService.SaveProperties ();
  161. if (processService != null) {
  162. processService.Dispose ();
  163. processService = null;
  164. }
  165. initialized = false;
  166. }
  167. public static ProcessService ProcessService {
  168. get {
  169. if (processService == null)
  170. processService = new ProcessService ();
  171. return processService;
  172. }
  173. }
  174. public static SystemAssemblyService SystemAssemblyService {
  175. get {
  176. return systemAssemblyService;
  177. }
  178. }
  179. public static AddinSetupService AddinSetupService {
  180. get {
  181. return setupService;
  182. }
  183. }
  184. public static ApplicationService ApplicationService {
  185. get {
  186. if (applicationService == null)
  187. applicationService = new ApplicationService ();
  188. return applicationService;
  189. }
  190. }
  191. public static void SetProcessName (string name)
  192. {
  193. if (Environment.OSVersion.Platform == PlatformID.Unix) {
  194. try {
  195. unixSetProcessName (name);
  196. } catch (Exception e) {
  197. LoggingService.LogError ("Error setting process name", e);
  198. }
  199. }
  200. }
  201. [DllImport ("libc")] // Linux
  202. private static extern int prctl (int option, byte [] arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5);
  203. [DllImport ("libc")] // BSD
  204. private static extern void setproctitle (byte [] fmt, byte [] str_arg);
  205. //this is from http://abock.org/2006/02/09/changing-process-name-in-mono/
  206. static void unixSetProcessName (string name)
  207. {
  208. try {
  209. if (prctl (15 /* PR_SET_NAME */, Encoding.ASCII.GetBytes (name + "\0"), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero) != 0) {
  210. throw new ApplicationException ("Error setting process name: " + Mono.Unix.Native.Stdlib.GetLastError ());
  211. }
  212. } catch (EntryPointNotFoundException) {
  213. // Not every BSD has setproctitle
  214. try {
  215. setproctitle (Encoding.ASCII.GetBytes ("%s\0"), Encoding.ASCII.GetBytes (name + "\0"));
  216. } catch (EntryPointNotFoundException) {}
  217. }
  218. }
  219. public static event EventHandler ShuttingDown;
  220. }
  221. internal static class Counters
  222. {
  223. public static TimerCounter RuntimeInitialization = InstrumentationService.CreateTimerCounter ("Runtime initialization", "Runtime");
  224. public static TimerCounter PropertyServiceInitialization = InstrumentationService.CreateTimerCounter ("Property Service initialization", "Runtime");
  225. public static Counter AddinsLoaded = InstrumentationService.CreateCounter ("Add-ins loaded", "Add-in Engine", true);
  226. public static Counter ProcessesStarted = InstrumentationService.CreateCounter ("Processes started", "Process Service");
  227. public static Counter ExternalObjects = InstrumentationService.CreateCounter ("External objects", "Process Service");
  228. public static Counter ExternalHostProcesses = InstrumentationService.CreateCounter ("External processes hosting objects", "Process Service");
  229. public static TimerCounter TargetRuntimesLoading = InstrumentationService.CreateTimerCounter ("Target runtimes loaded", "Assembly Service", 0, true);
  230. public static Counter PcFilesParsed = InstrumentationService.CreateCounter (".pc Files parsed", "Assembly Service");
  231. public static Counter FileChangeNotifications = InstrumentationService.CreateCounter ("File change notifications", "File Service");
  232. public static Counter FilesRemoved = InstrumentationService.CreateCounter ("Files removed", "File Service");
  233. public static Counter FilesCreated = InstrumentationService.CreateCounter ("Files created", "File Service");
  234. public static Counter FilesRenamed = InstrumentationService.CreateCounter ("Files renamed", "File Service");
  235. public static Counter DirectoriesRemoved = InstrumentationService.CreateCounter ("Directories removed", "File Service");
  236. public static Counter DirectoriesCreated = InstrumentationService.CreateCounter ("Directories created", "File Service");
  237. public static Counter DirectoriesRenamed = InstrumentationService.CreateCounter ("Directories renamed", "File Service");
  238. public static Counter LogErrors = InstrumentationService.CreateCounter ("Errors", "Log");
  239. public static Counter LogWarnings = InstrumentationService.CreateCounter ("Warnings", "Log");
  240. public static Counter LogMessages = InstrumentationService.CreateCounter ("Information messages", "Log");
  241. public static Counter LogFatalErrors = InstrumentationService.CreateCounter ("Fatal errors", "Log");
  242. public static Counter LogDebug = InstrumentationService.CreateCounter ("Debug messages", "Log");
  243. }
  244. }