ApplicationManager.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #if NET_2_0
  30. using System;
  31. using System.Collections.Generic;
  32. using System.IO;
  33. using System.Security.Permissions;
  34. using System.Security.Policy;
  35. using System.Threading;
  36. namespace System.Web.Hosting {
  37. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. public sealed class ApplicationManager : MarshalByRefObject {
  39. static ApplicationManager instance = new ApplicationManager ();
  40. int users;
  41. Dictionary <string, BareApplicationHost> id_to_host;
  42. ApplicationManager ()
  43. {
  44. id_to_host = new Dictionary<string, BareApplicationHost> ();
  45. }
  46. public void Close ()
  47. {
  48. if (Interlocked.Decrement (ref users) == 0)
  49. ShutdownAll ();
  50. }
  51. [MonoTODO ("Need to take advantage of the configuration mapping capabilities of IApplicationHost")]
  52. [SecurityPermissionAttribute(SecurityAction.Demand, Unrestricted = true)]
  53. public IRegisteredObject CreateObject (IApplicationHost appHost, Type type)
  54. {
  55. if (appHost == null)
  56. throw new ArgumentNullException ("appHost");
  57. if (type == null)
  58. throw new ArgumentNullException ("type");
  59. return CreateObject (appHost.GetSiteID (),
  60. type,
  61. appHost.GetVirtualPath (),
  62. appHost.GetPhysicalPath (),
  63. true,
  64. true);
  65. }
  66. public IRegisteredObject CreateObject (string appId, Type type, string virtualPath,
  67. string physicalPath, bool failIfExists)
  68. {
  69. return CreateObject (appId, type, virtualPath, physicalPath, failIfExists, true);
  70. }
  71. public IRegisteredObject CreateObject (string appId, Type type, string virtualPath,
  72. string physicalPath, bool failIfExists, bool throwOnError)
  73. {
  74. if (appId == null)
  75. throw new ArgumentNullException ("appId");
  76. if (!VirtualPathUtility.IsAbsolute (virtualPath))
  77. throw new ArgumentException ("Relative path no allowed.", "virtualPath");
  78. if (String.IsNullOrEmpty (physicalPath))
  79. throw new ArgumentException ("Cannot be null or empty", "physicalPath");
  80. // 'type' is not checked. If it's null, we'll throw a NullReferenceException
  81. if (!typeof (IRegisteredObject).IsAssignableFrom (type))
  82. throw new ArgumentException (String.Concat ("Type '", type.Name, "' does not implement IRegisteredObject."), "type");
  83. //
  84. // ArgumentException is thrown for the physical path from the internal object created
  85. // in the new application domain.
  86. BareApplicationHost host = null;
  87. if (id_to_host.ContainsKey (appId))
  88. host = id_to_host [appId];
  89. IRegisteredObject ireg = null;
  90. if (host != null) {
  91. ireg = CheckIfExists (host, type, failIfExists);
  92. if (ireg != null)
  93. return ireg;
  94. }
  95. try {
  96. if (host == null)
  97. host = CreateHost (appId, virtualPath, physicalPath);
  98. ireg = host.CreateInstance (type);
  99. } catch (Exception) {
  100. if (throwOnError)
  101. throw;
  102. }
  103. if (ireg != null && host.GetObject (type) == null) // If not registered from ctor...
  104. host.RegisterObject (ireg, true);
  105. return ireg;
  106. }
  107. // Used from ClientBuildManager
  108. internal BareApplicationHost CreateHostWithCheck (string appId, string vpath, string ppath)
  109. {
  110. if (id_to_host.ContainsKey (appId))
  111. throw new InvalidOperationException ("Already have a host with the same appId");
  112. return CreateHost (appId, vpath, ppath);
  113. }
  114. BareApplicationHost CreateHost (string appId, string vpath, string ppath)
  115. {
  116. BareApplicationHost host;
  117. host = (BareApplicationHost) ApplicationHost.CreateApplicationHost (typeof (BareApplicationHost), vpath, ppath);
  118. host.Manager = this;
  119. host.AppID = appId;
  120. id_to_host [appId] = host;
  121. return host;
  122. }
  123. internal void RemoveHost (string appId)
  124. {
  125. id_to_host.Remove (appId);
  126. }
  127. IRegisteredObject CheckIfExists (BareApplicationHost host, Type type, bool failIfExists)
  128. {
  129. IRegisteredObject ireg = host.GetObject (type);
  130. if (ireg == null)
  131. return null;
  132. if (failIfExists)
  133. throw new InvalidOperationException (String.Concat ("Well known object of type '", type.Name, "' already exists in this domain."));
  134. return ireg;
  135. }
  136. public static ApplicationManager GetApplicationManager ()
  137. {
  138. return instance;
  139. }
  140. public IRegisteredObject GetObject (string appId, Type type)
  141. {
  142. if (appId == null)
  143. throw new ArgumentNullException ("appId");
  144. if (type == null)
  145. throw new ArgumentNullException ("type");
  146. BareApplicationHost host = null;
  147. if (!id_to_host.ContainsKey (appId))
  148. return null;
  149. host = id_to_host [appId];
  150. return host.GetObject (type);
  151. }
  152. public ApplicationInfo [] GetRunningApplications ()
  153. {
  154. ICollection<string> coll = id_to_host.Keys;
  155. string [] keys = new string [coll.Count];
  156. coll.CopyTo (keys, 0);
  157. ApplicationInfo [] result = new ApplicationInfo [coll.Count];
  158. int i = 0;
  159. foreach (string str in keys) {
  160. BareApplicationHost host = id_to_host [str];
  161. result [i++] = new ApplicationInfo (str, host.PhysicalPath, host.VirtualPath);
  162. }
  163. return result;
  164. }
  165. public override object InitializeLifetimeService ()
  166. {
  167. return null;
  168. }
  169. public bool IsIdle ()
  170. {
  171. throw new NotImplementedException ();
  172. }
  173. public void Open ()
  174. {
  175. Interlocked.Increment (ref users);
  176. }
  177. public void ShutdownAll ()
  178. {
  179. ICollection<string> coll = id_to_host.Keys;
  180. string [] keys = new string [coll.Count];
  181. coll.CopyTo (keys, 0);
  182. foreach (string str in keys) {
  183. BareApplicationHost host = id_to_host [str];
  184. host.Shutdown ();
  185. }
  186. id_to_host.Clear ();
  187. }
  188. public void ShutdownApplication (string appId)
  189. {
  190. if (appId == null)
  191. throw new ArgumentNullException ("appId");
  192. BareApplicationHost host = id_to_host [appId];
  193. if (host == null)
  194. return;
  195. host.Shutdown ();
  196. }
  197. public void StopObject (string appId, Type type)
  198. {
  199. if (appId == null)
  200. throw new ArgumentNullException ("appId");
  201. if (type == null)
  202. throw new ArgumentNullException ("type");
  203. if (!id_to_host.ContainsKey (appId))
  204. return;
  205. BareApplicationHost host = id_to_host [appId];
  206. if (host == null)
  207. return;
  208. host.StopObject (type);
  209. }
  210. }
  211. }
  212. #endif