ApplicationManager.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. 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. HostingEnvironment.SetIsHosted (true);
  104. host.Manager = this;
  105. host.AppID = appId;
  106. id_to_host [appId] = host;
  107. return host;
  108. }
  109. internal void RemoveHost (string appId)
  110. {
  111. id_to_host.Remove (appId);
  112. }
  113. IRegisteredObject CheckIfExists (BareApplicationHost host, Type type, bool failIfExists)
  114. {
  115. IRegisteredObject ireg = host.GetObject (type);
  116. if (ireg == null)
  117. return null;
  118. if (failIfExists)
  119. throw new InvalidOperationException (String.Concat ("Well known object of type '", type.Name, "' already exists in this domain."));
  120. return ireg;
  121. }
  122. public static ApplicationManager GetApplicationManager ()
  123. {
  124. return instance;
  125. }
  126. public IRegisteredObject GetObject (string appId, Type type)
  127. {
  128. if (appId == null)
  129. throw new ArgumentNullException ("appId");
  130. if (type == null)
  131. throw new ArgumentNullException ("type");
  132. BareApplicationHost host = null;
  133. if (!id_to_host.ContainsKey (appId))
  134. return null;
  135. host = id_to_host [appId];
  136. return host.GetObject (type);
  137. }
  138. public ApplicationInfo [] GetRunningApplications ()
  139. {
  140. ICollection<string> coll = id_to_host.Keys;
  141. string [] keys = new string [coll.Count];
  142. coll.CopyTo (keys, 0);
  143. ApplicationInfo [] result = new ApplicationInfo [coll.Count];
  144. int i = 0;
  145. foreach (string str in keys) {
  146. BareApplicationHost host = id_to_host [str];
  147. result [i++] = new ApplicationInfo (str, host.PhysicalPath, host.VirtualPath);
  148. }
  149. return result;
  150. }
  151. public override object InitializeLifetimeService ()
  152. {
  153. return null;
  154. }
  155. public bool IsIdle ()
  156. {
  157. throw new NotImplementedException ();
  158. }
  159. public void Open ()
  160. {
  161. Interlocked.Increment (ref users);
  162. }
  163. public void ShutdownAll ()
  164. {
  165. ICollection<string> coll = id_to_host.Keys;
  166. string [] keys = new string [coll.Count];
  167. coll.CopyTo (keys, 0);
  168. foreach (string str in keys) {
  169. BareApplicationHost host = id_to_host [str];
  170. host.Shutdown ();
  171. }
  172. id_to_host.Clear ();
  173. }
  174. public void ShutdownApplication (string appId)
  175. {
  176. if (appId == null)
  177. throw new ArgumentNullException ("appId");
  178. BareApplicationHost host = id_to_host [appId];
  179. if (host == null)
  180. return;
  181. host.Shutdown ();
  182. }
  183. public void StopObject (string appId, Type type)
  184. {
  185. if (appId == null)
  186. throw new ArgumentNullException ("appId");
  187. if (type == null)
  188. throw new ArgumentNullException ("type");
  189. if (!id_to_host.ContainsKey (appId))
  190. return;
  191. BareApplicationHost host = id_to_host [appId];
  192. if (host == null)
  193. return;
  194. host.StopObject (type);
  195. }
  196. }
  197. }
  198. #endif