ApplicationManager.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // System.Web.Hosting.ApplicationManager
  3. //
  4. // Author:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2006 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. private 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. public IRegisteredObject CreateObject (string appId, Type type, string virtualPath,
  52. string physicalPath, bool failIfExists)
  53. {
  54. return CreateObject (appId, type, virtualPath, physicalPath, failIfExists, true);
  55. }
  56. public IRegisteredObject CreateObject (string appId, Type type, string virtualPath,
  57. string physicalPath, bool failIfExists, bool throwOnError)
  58. {
  59. if (appId == null)
  60. throw new ArgumentNullException ("appId");
  61. if (!VirtualPathUtility.IsAbsolute (virtualPath))
  62. throw new ArgumentException ("Relative path no allowed.", "virtualPath");
  63. if (physicalPath == null || physicalPath == "")
  64. throw new ArgumentException ("Cannot be null or empty", "physicalPath");
  65. // 'type' is not checked. If it's null, we'll throw a NullReferenceException
  66. if (!typeof (IRegisteredObject).IsAssignableFrom (type))
  67. throw new ArgumentException (String.Concat ("Type '", type.Name, "' does not implement IRegisteredObject."), "type");
  68. //
  69. // ArgumentException is thrown for the physical path from the internal object created
  70. // in the new application domain.
  71. BareApplicationHost host = null;
  72. if (id_to_host.ContainsKey (appId))
  73. host = id_to_host [appId];
  74. IRegisteredObject ireg = null;
  75. if (host != null) {
  76. ireg = CheckIfExists (host, type, failIfExists);
  77. if (ireg != null)
  78. return ireg;
  79. }
  80. try {
  81. if (host == null)
  82. host = CreateHost (appId, virtualPath, physicalPath);
  83. ireg = host.CreateInstance (type);
  84. } catch (Exception) {
  85. if (throwOnError)
  86. throw;
  87. }
  88. if (ireg != null && host.GetObject (type) == null) // If not registered from ctor...
  89. host.RegisterObject (ireg, true);
  90. return ireg;
  91. }
  92. // Used from ClientBuildManager
  93. internal BareApplicationHost CreateHostWithCheck (string appId, string vpath, string ppath)
  94. {
  95. if (id_to_host.ContainsKey (appId))
  96. throw new InvalidOperationException ("Already have a host with the same appId");
  97. return CreateHost (appId, vpath, ppath);
  98. }
  99. BareApplicationHost CreateHost (string appId, string vpath, string ppath)
  100. {
  101. BareApplicationHost host;
  102. host = (BareApplicationHost) ApplicationHost.CreateApplicationHost (typeof (BareApplicationHost), vpath, ppath);
  103. host.Manager = this;
  104. host.AppID = appId;
  105. id_to_host [appId] = host;
  106. return host;
  107. }
  108. internal void RemoveHost (string appId)
  109. {
  110. id_to_host.Remove (appId);
  111. }
  112. IRegisteredObject CheckIfExists (BareApplicationHost host, Type type, bool failIfExists)
  113. {
  114. IRegisteredObject ireg = host.GetObject (type);
  115. if (ireg == null)
  116. return null;
  117. if (failIfExists)
  118. throw new InvalidOperationException (String.Concat ("Well known object of type '", type.Name, "' already exists in this domain."));
  119. return ireg;
  120. }
  121. public static ApplicationManager GetApplicationManager ()
  122. {
  123. return instance;
  124. }
  125. public IRegisteredObject GetObject (string appId, Type type)
  126. {
  127. if (appId == null)
  128. throw new ArgumentNullException ("appId");
  129. if (type == null)
  130. throw new ArgumentNullException ("type");
  131. BareApplicationHost host = null;
  132. if (!id_to_host.ContainsKey (appId))
  133. return null;
  134. host = id_to_host [appId];
  135. return host.GetObject (type);
  136. }
  137. public ApplicationInfo [] GetRunningApplications ()
  138. {
  139. ICollection<string> coll = id_to_host.Keys;
  140. string [] keys = new string [coll.Count];
  141. coll.CopyTo (keys, 0);
  142. ApplicationInfo [] result = new ApplicationInfo [coll.Count];
  143. int i = 0;
  144. foreach (string str in keys) {
  145. BareApplicationHost host = id_to_host [str];
  146. result [i++] = new ApplicationInfo (str, host.PhysicalPath, host.VirtualPath);
  147. }
  148. return result;
  149. }
  150. public override object InitializeLifetimeService ()
  151. {
  152. return null;
  153. }
  154. public bool IsIdle ()
  155. {
  156. throw new NotImplementedException ();
  157. }
  158. public void Open ()
  159. {
  160. Interlocked.Increment (ref users);
  161. }
  162. public void ShutdownAll ()
  163. {
  164. ICollection<string> coll = id_to_host.Keys;
  165. string [] keys = new string [coll.Count];
  166. coll.CopyTo (keys, 0);
  167. foreach (string str in keys) {
  168. BareApplicationHost host = id_to_host [str];
  169. host.Shutdown ();
  170. }
  171. id_to_host.Clear ();
  172. }
  173. public void ShutdownApplication (string appId)
  174. {
  175. if (appId == null)
  176. throw new ArgumentNullException ("appId");
  177. BareApplicationHost host = id_to_host [appId];
  178. if (host == null)
  179. return;
  180. host.Shutdown ();
  181. }
  182. public void StopObject (string appId, Type type)
  183. {
  184. if (appId == null)
  185. throw new ArgumentNullException ("appId");
  186. if (type == null)
  187. throw new ArgumentNullException ("type");
  188. if (!id_to_host.ContainsKey (appId))
  189. return;
  190. BareApplicationHost host = id_to_host [appId];
  191. if (host == null)
  192. return;
  193. host.StopObject (type);
  194. }
  195. }
  196. }
  197. #endif