ApplicationManager.cs 6.8 KB

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