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. 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 CreateOrReuseHost (string appId, string vpath, string ppath)
  96. {
  97. if (id_to_host.ContainsKey (appId))
  98. return id_to_host [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. id_to_host [appId] = host;
  106. return host;
  107. }
  108. IRegisteredObject CheckIfExists (BareApplicationHost host, Type type, bool failIfExists)
  109. {
  110. IRegisteredObject ireg = host.GetObject (type);
  111. if (ireg == null)
  112. return null;
  113. if (failIfExists) {
  114. string msg = String.Format ("Well known object of type '{0}' already " +
  115. "exists in this domain.", type.Name);
  116. throw new InvalidOperationException (msg);
  117. }
  118. return ireg;
  119. }
  120. public static ApplicationManager GetApplicationManager ()
  121. {
  122. return instance;
  123. }
  124. public IRegisteredObject GetObject (string appId, Type type)
  125. {
  126. if (appId == null)
  127. throw new ArgumentNullException ("appId");
  128. if (type == null)
  129. throw new ArgumentNullException ("type");
  130. BareApplicationHost host = null;
  131. if (!id_to_host.ContainsKey (appId))
  132. return null;
  133. host = id_to_host [appId];
  134. return host.GetObject (type);
  135. }
  136. public ApplicationInfo [] GetRunningApplications ()
  137. {
  138. ICollection<string> coll = id_to_host.Keys;
  139. string [] keys = new string [coll.Count];
  140. coll.CopyTo (keys, 0);
  141. ApplicationInfo [] result = new ApplicationInfo [coll.Count];
  142. int i = 0;
  143. foreach (string str in keys) {
  144. BareApplicationHost host = id_to_host [str];
  145. result [i++] = new ApplicationInfo (str, host.PhysicalPath, host.VirtualPath);
  146. }
  147. return result;
  148. }
  149. public override object InitializeLifetimeService ()
  150. {
  151. return null;
  152. }
  153. public bool IsIdle ()
  154. {
  155. throw new NotImplementedException ();
  156. }
  157. public void Open ()
  158. {
  159. Interlocked.Increment (ref users);
  160. }
  161. public void ShutdownAll ()
  162. {
  163. ICollection<string> coll = id_to_host.Keys;
  164. string [] keys = new string [coll.Count];
  165. coll.CopyTo (keys, 0);
  166. ApplicationInfo [] result = new ApplicationInfo [coll.Count];
  167. int i = 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