PageRenderTime 103ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/jlangridge/monodevelop
C# | 319 lines | 238 code | 50 blank | 31 comment | 28 complexity | a0dbbaf601db936e4add10b747045099 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.Collections;
  30. using System.Runtime.InteropServices;
  31. using System.Text;
  32. using MonoDevelop.Core;
  33. using MonoDevelop.Core.Assemblies;
  34. using MonoDevelop.Core.AddIns;
  35. using MonoDevelop.Core.Execution;
  36. using Mono.Addins;
  37. using Mono.Addins.Setup;
  38. using MonoDevelop.Core.Instrumentation;
  39. namespace MonoDevelop.Core
  40. {
  41. public static class Runtime
  42. {
  43. static ProcessService processService;
  44. static SystemAssemblyService systemAssemblyService;
  45. static SetupService setupService;
  46. static ApplicationService applicationService;
  47. static bool initialized;
  48. public static void Initialize (bool updateAddinRegistry)
  49. {
  50. if (initialized)
  51. return;
  52. Counters.RuntimeInitialization.BeginTiming ();
  53. SetupInstrumentation ();
  54. AddinManager.AddinLoadError += OnLoadError;
  55. AddinManager.AddinLoaded += OnLoad;
  56. AddinManager.AddinUnloaded += OnUnload;
  57. try {
  58. Counters.RuntimeInitialization.Trace ("Initializing Addin Manager");
  59. AddinManager.Initialize (MonoDevelop.Core.PropertyService.ConfigPath);
  60. AddinManager.InitializeDefaultLocalizer (new DefaultAddinLocalizer ());
  61. if (updateAddinRegistry)
  62. AddinManager.Registry.Update (null);
  63. setupService = new SetupService (AddinManager.Registry);
  64. Counters.RuntimeInitialization.Trace ("Initialized Addin Manager");
  65. RegisterAddinRepositories ();
  66. Counters.RuntimeInitialization.Trace ("Initializing Assembly Service");
  67. systemAssemblyService = new SystemAssemblyService ();
  68. systemAssemblyService.Initialize ();
  69. initialized = true;
  70. } catch (Exception ex) {
  71. Console.WriteLine (ex);
  72. AddinManager.AddinLoadError -= OnLoadError;
  73. AddinManager.AddinLoaded -= OnLoad;
  74. AddinManager.AddinUnloaded -= OnUnload;
  75. } finally {
  76. Counters.RuntimeInitialization.EndTiming ();
  77. }
  78. }
  79. static void RegisterAddinRepositories ()
  80. {
  81. string stableUrl = GetRepoUrl ("Stable");
  82. string betaUrl = GetRepoUrl ("Beta");
  83. string alphaUrl = GetRepoUrl ("Alpha");
  84. IList validUrls = new string[] { stableUrl, betaUrl, alphaUrl };
  85. // Remove old repositories
  86. var reps = setupService.Repositories;
  87. foreach (AddinRepository rep in reps.GetRepositories ()) {
  88. if (rep.Url.StartsWith ("http://go-mono.com/md/") ||
  89. (rep.Url.StartsWith ("http://monodevelop.com/files/addins/")) ||
  90. (rep.Url.StartsWith ("http://addins.monodevelop.com/") && !validUrls.Contains (rep.Url)))
  91. reps.RemoveRepository (rep.Url);
  92. }
  93. if (!reps.ContainsRepository (stableUrl)) {
  94. // Add the stable and beta channels. Don't add alpha.
  95. reps.RegisterRepository (null, stableUrl, false);
  96. reps.RegisterRepository (null, betaUrl, false);
  97. }
  98. }
  99. internal static string GetRepoUrl (string quality)
  100. {
  101. string platform;
  102. if (PropertyService.IsWindows)
  103. platform = "Win32";
  104. else if (PropertyService.IsMac)
  105. platform = "Mac";
  106. else
  107. platform = "Linux";
  108. return "http://addins.monodevelop.com/" + quality + "/" + platform + "/" + AddinManager.CurrentAddin.Version + "/main.mrep";
  109. }
  110. static void SetupInstrumentation ()
  111. {
  112. InstrumentationService.Enabled = PropertyService.Get ("MonoDevelop.EnableInstrumentation", false);
  113. if (InstrumentationService.Enabled) {
  114. LoggingService.LogInfo ("Instrumentation Service started");
  115. try {
  116. int port = InstrumentationService.PublishService (0);
  117. LoggingService.LogInfo ("Instrumentation available at port " + port);
  118. } catch (Exception ex) {
  119. LoggingService.LogError ("Instrumentation service could not be published", ex);
  120. }
  121. }
  122. PropertyService.AddPropertyHandler ("MonoDevelop.EnableInstrumentation", delegate {
  123. InstrumentationService.Enabled = PropertyService.Get ("MonoDevelop.EnableInstrumentation", false);
  124. });
  125. }
  126. static void OnLoadError (object s, AddinErrorEventArgs args)
  127. {
  128. string msg = "Add-in error (" + args.AddinId + "): " + args.Message;
  129. LoggingService.LogError (msg, args.Exception);
  130. }
  131. static void OnLoad (object s, AddinEventArgs args)
  132. {
  133. Counters.AddinsLoaded.Inc ("Add-in loaded: " + args.AddinId);
  134. }
  135. static void OnUnload (object s, AddinEventArgs args)
  136. {
  137. Counters.AddinsLoaded.Dec ("Add-in unloaded: " + args.AddinId);
  138. }
  139. internal static bool Initialized {
  140. get { return initialized; }
  141. }
  142. public static void Shutdown ()
  143. {
  144. if (!initialized)
  145. return;
  146. if (ShuttingDown != null)
  147. ShuttingDown (null, EventArgs.Empty);
  148. PropertyService.SaveProperties ();
  149. if (processService != null) {
  150. processService.Dispose ();
  151. processService = null;
  152. }
  153. initialized = false;
  154. }
  155. public static ProcessService ProcessService {
  156. get {
  157. if (processService == null)
  158. processService = new ProcessService ();
  159. return processService;
  160. }
  161. }
  162. public static SystemAssemblyService SystemAssemblyService {
  163. get {
  164. return systemAssemblyService;
  165. }
  166. }
  167. public static SetupService AddinSetupService {
  168. get {
  169. return setupService;
  170. }
  171. }
  172. public static ApplicationService ApplicationService {
  173. get {
  174. if (applicationService == null)
  175. applicationService = new ApplicationService ();
  176. return applicationService;
  177. }
  178. }
  179. public static void SetProcessName (string name)
  180. {
  181. if (Environment.OSVersion.Platform == PlatformID.Unix) {
  182. try {
  183. unixSetProcessName (name);
  184. } catch (Exception e) {
  185. LoggingService.LogError ("Error setting process name", e);
  186. }
  187. }
  188. }
  189. [DllImport ("libc")] // Linux
  190. private static extern int prctl (int option, byte [] arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5);
  191. [DllImport ("libc")] // BSD
  192. private static extern void setproctitle (byte [] fmt, byte [] str_arg);
  193. //this is from http://abock.org/2006/02/09/changing-process-name-in-mono/
  194. static void unixSetProcessName (string name)
  195. {
  196. try {
  197. if (prctl (15 /* PR_SET_NAME */, Encoding.ASCII.GetBytes (name + "\0"), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero) != 0) {
  198. throw new ApplicationException ("Error setting process name: " + Mono.Unix.Native.Stdlib.GetLastError ());
  199. }
  200. } catch (EntryPointNotFoundException) {
  201. // Not every BSD has setproctitle
  202. try {
  203. setproctitle (Encoding.ASCII.GetBytes ("%s\0"), Encoding.ASCII.GetBytes (name + "\0"));
  204. } catch (EntryPointNotFoundException) {}
  205. }
  206. }
  207. public static event EventHandler ShuttingDown;
  208. }
  209. public class ApplicationService
  210. {
  211. public int StartApplication (string appId, string[] parameters)
  212. {
  213. ExtensionNode node = AddinManager.GetExtensionNode ("/MonoDevelop/Core/Applications/" + appId);
  214. if (node == null)
  215. throw new InstallException ("Application not found: " + appId);
  216. ApplicationExtensionNode apnode = node as ApplicationExtensionNode;
  217. if (apnode == null)
  218. throw new Exception ("Invalid node type");
  219. IApplication app = (IApplication) apnode.CreateInstance ();
  220. try {
  221. return app.Run (parameters);
  222. } catch (Exception ex) {
  223. Console.WriteLine (ex.Message);
  224. LoggingService.LogFatalError (ex.ToString ());
  225. return -1;
  226. }
  227. }
  228. public IApplicationInfo[] GetApplications ()
  229. {
  230. ExtensionNodeList nodes = AddinManager.GetExtensionNodes ("/MonoDevelop/Core/Applications");
  231. IApplicationInfo[] apps = new IApplicationInfo [nodes.Count];
  232. for (int n=0; n<nodes.Count; n++)
  233. apps [n] = (ApplicationExtensionNode) nodes [n];
  234. return apps;
  235. }
  236. }
  237. public interface IApplicationInfo
  238. {
  239. string Id { get; }
  240. string Description { get; }
  241. }
  242. public interface IApplication
  243. {
  244. int Run (string[] arguments);
  245. }
  246. internal static class Counters
  247. {
  248. public static TimerCounter RuntimeInitialization = InstrumentationService.CreateTimerCounter ("Runtime initialization", "Runtime");
  249. public static TimerCounter PropertyServiceInitialization = InstrumentationService.CreateTimerCounter ("Property Service initialization", "Runtime");
  250. public static Counter AddinsLoaded = InstrumentationService.CreateCounter ("Add-ins loaded", "Add-in Engine", true);
  251. public static Counter ProcessesStarted = InstrumentationService.CreateCounter ("Processes started", "Process Service");
  252. public static Counter ExternalObjects = InstrumentationService.CreateCounter ("External objects", "Process Service");
  253. public static Counter ExternalHostProcesses = InstrumentationService.CreateCounter ("External processes hosting objects", "Process Service");
  254. public static TimerCounter TargetRuntimesLoading = InstrumentationService.CreateTimerCounter ("Target runtimes loaded", "Assembly Service", 0, true);
  255. public static Counter PcFilesParsed = InstrumentationService.CreateCounter (".pc Files parsed", "Assembly Service");
  256. public static Counter FileChangeNotifications = InstrumentationService.CreateCounter ("File change notifications", "File Service");
  257. public static Counter FilesRemoved = InstrumentationService.CreateCounter ("Files removed", "File Service");
  258. public static Counter FilesCreated = InstrumentationService.CreateCounter ("Files created", "File Service");
  259. public static Counter FilesRenamed = InstrumentationService.CreateCounter ("Files renamed", "File Service");
  260. public static Counter DirectoriesRemoved = InstrumentationService.CreateCounter ("Directories removed", "File Service");
  261. public static Counter DirectoriesCreated = InstrumentationService.CreateCounter ("Directories created", "File Service");
  262. public static Counter DirectoriesRenamed = InstrumentationService.CreateCounter ("Directories renamed", "File Service");
  263. public static Counter LogErrors = InstrumentationService.CreateCounter ("Errors", "Log");
  264. public static Counter LogWarnings = InstrumentationService.CreateCounter ("Warnings", "Log");
  265. public static Counter LogMessages = InstrumentationService.CreateCounter ("Information messages", "Log");
  266. public static Counter LogFatalErrors = InstrumentationService.CreateCounter ("Fatal errors", "Log");
  267. public static Counter LogDebug = InstrumentationService.CreateCounter ("Debug messages", "Log");
  268. }
  269. }