ApplicationManager.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // System.Web.Hosting.ApplicationManager
  3. //
  4. // Author:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2006-2010 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections.Generic;
  31. using System.IO;
  32. using System.Security.Permissions;
  33. using System.Security.Policy;
  34. using System.Threading;
  35. namespace System.Web.Hosting {
  36. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. public sealed class ApplicationManager : MarshalByRefObject {
  38. static ApplicationManager instance = new ApplicationManager ();
  39. int users;
  40. Dictionary <string, BareApplicationHost> id_to_host;
  41. ApplicationManager ()
  42. {
  43. id_to_host = new Dictionary<string, BareApplicationHost> ();
  44. }
  45. public void Close ()
  46. {
  47. if (Interlocked.Decrement (ref users) == 0)
  48. ShutdownAll ();
  49. }
  50. [MonoTODO ("Need to take advantage of the configuration mapping capabilities of IApplicationHost")]
  51. [SecurityPermissionAttribute(SecurityAction.Demand, Unrestricted = true)]
  52. public IRegisteredObject CreateObject (IApplicationHost appHost, Type type)
  53. {
  54. if (appHost == null)
  55. throw new ArgumentNullException ("appHost");
  56. if (type == null)
  57. throw new ArgumentNullException ("type");
  58. return CreateObject (appHost.GetSiteID (),
  59. type,
  60. appHost.GetVirtualPath (),
  61. appHost.GetPhysicalPath (),
  62. true,
  63. true);
  64. }
  65. public IRegisteredObject CreateObject (string appId, Type type, string virtualPath,
  66. string physicalPath, bool failIfExists)
  67. {
  68. return CreateObject (appId, type, virtualPath, physicalPath, failIfExists, true);
  69. }
  70. public IRegisteredObject CreateObject (string appId, Type type, string virtualPath,
  71. string physicalPath, bool failIfExists, bool throwOnError)
  72. {
  73. if (appId == null)
  74. throw new ArgumentNullException ("appId");
  75. if (!VirtualPathUtility.IsAbsolute (virtualPath))
  76. throw new ArgumentException ("Relative path no allowed.", "virtualPath");
  77. if (String.IsNullOrEmpty (physicalPath))
  78. throw new ArgumentException ("Cannot be null or empty", "physicalPath");
  79. // 'type' is not checked. If it's null, we'll throw a NullReferenceException
  80. if (!typeof (IRegisteredObject).IsAssignableFrom (type))
  81. throw new ArgumentException (String.Concat ("Type '", type.Name, "' does not implement IRegisteredObject."), "type");
  82. //
  83. // ArgumentException is thrown for the physical path from the internal object created
  84. // in the new application domain.
  85. BareApplicationHost host = null;
  86. if (id_to_host.ContainsKey (appId))
  87. host = id_to_host [appId];
  88. IRegisteredObject ireg = null;
  89. if (host != null) {
  90. ireg = CheckIfExists (host, type, failIfExists);
  91. if (ireg != null)
  92. return ireg;
  93. }
  94. try {
  95. if (host == null)
  96. host = CreateHost (appId, virtualPath, physicalPath);
  97. ireg = host.CreateInstance (type);
  98. } catch (Exception) {
  99. if (throwOnError)
  100. throw;
  101. }
  102. if (ireg != null && host.GetObject (type) == null) // If not registered from ctor...
  103. host.RegisterObject (ireg, true);
  104. return ireg;
  105. }
  106. // Used from ClientBuildManager
  107. internal BareApplicationHost CreateHostWithCheck (string appId, string vpath, string ppath)
  108. {
  109. if (id_to_host.ContainsKey (appId))
  110. throw new InvalidOperationException ("Already have a host with the same appId");
  111. return CreateHost (appId, vpath, ppath);
  112. }
  113. BareApplicationHost CreateHost (string appId, string vpath, string ppath)
  114. {
  115. BareApplicationHost host;
  116. host = (BareApplicationHost) ApplicationHost.CreateApplicationHost (typeof (BareApplicationHost), vpath, ppath);
  117. host.Manager = this;
  118. host.AppID = appId;
  119. id_to_host [appId] = host;
  120. return host;
  121. }
  122. internal void RemoveHost (string appId)
  123. {
  124. id_to_host.Remove (appId);
  125. }
  126. IRegisteredObject CheckIfExists (BareApplicationHost host, Type type, bool failIfExists)
  127. {
  128. IRegisteredObject ireg = host.GetObject (type);
  129. if (ireg == null)
  130. return null;
  131. if (failIfExists)
  132. throw new InvalidOperationException (String.Concat ("Well known object of type '", type.Name, "' already exists in this domain."));
  133. return ireg;
  134. }
  135. public static ApplicationManager GetApplicationManager ()
  136. {
  137. return instance;
  138. }
  139. public IRegisteredObject GetObject (string appId, Type type)
  140. {
  141. if (appId == null)
  142. throw new ArgumentNullException ("appId");
  143. if (type == null)
  144. throw new ArgumentNullException ("type");
  145. BareApplicationHost host = null;
  146. if (!id_to_host.ContainsKey (appId))
  147. return null;
  148. host = id_to_host [appId];
  149. return host.GetObject (type);
  150. }
  151. public ApplicationInfo [] GetRunningApplications ()
  152. {
  153. ICollection<string> coll = id_to_host.Keys;
  154. string [] keys = new string [coll.Count];
  155. coll.CopyTo (keys, 0);
  156. ApplicationInfo [] result = new ApplicationInfo [coll.Count];
  157. int i = 0;
  158. foreach (string str in keys) {
  159. BareApplicationHost host = id_to_host [str];
  160. result [i++] = new ApplicationInfo (str, host.PhysicalPath, host.VirtualPath);
  161. }
  162. return result;
  163. }
  164. public override object InitializeLifetimeService ()
  165. {
  166. return null;
  167. }
  168. public bool IsIdle ()
  169. {
  170. throw new NotImplementedException ();
  171. }
  172. public void Open ()
  173. {
  174. Interlocked.Increment (ref users);
  175. }
  176. public void ShutdownAll ()
  177. {
  178. ICollection<string> coll = id_to_host.Keys;
  179. string [] keys = new string [coll.Count];
  180. coll.CopyTo (keys, 0);
  181. foreach (string str in keys) {
  182. BareApplicationHost host = id_to_host [str];
  183. host.Shutdown ();
  184. }
  185. id_to_host.Clear ();
  186. }
  187. public void ShutdownApplication (string appId)
  188. {
  189. if (appId == null)
  190. throw new ArgumentNullException ("appId");
  191. BareApplicationHost host = id_to_host [appId];
  192. if (host == null)
  193. return;
  194. host.Shutdown ();
  195. }
  196. public void StopObject (string appId, Type type)
  197. {
  198. if (appId == null)
  199. throw new ArgumentNullException ("appId");
  200. if (type == null)
  201. throw new ArgumentNullException ("type");
  202. if (!id_to_host.ContainsKey (appId))
  203. return;
  204. BareApplicationHost host = id_to_host [appId];
  205. if (host == null)
  206. return;
  207. host.StopObject (type);
  208. }
  209. }
  210. }