update readme (#21797)
[mono-project.git] / mcs / class / System.Web / System.Web.Hosting / ApplicationManager.cs
blob1f1cc9010dbcf4635fe6ea2c9455c80699580e4c
1 //
2 // System.Web.Hosting.ApplicationManager
3 //
4 // Author:
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
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.
29 using System;
30 using System.Collections.Generic;
31 using System.IO;
32 using System.Security.Permissions;
33 using System.Security.Policy;
34 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;
43 ApplicationManager ()
45 id_to_host = new Dictionary<string, BareApplicationHost> ();
48 public void Close ()
50 if (Interlocked.Decrement (ref users) == 0)
51 ShutdownAll ();
54 [MonoTODO ("Need to take advantage of the configuration mapping capabilities of IApplicationHost")]
55 [SecurityPermissionAttribute(SecurityAction.Demand, Unrestricted = true)]
56 public IRegisteredObject CreateObject (IApplicationHost appHost, Type type)
58 if (appHost == null)
59 throw new ArgumentNullException ("appHost");
60 if (type == null)
61 throw new ArgumentNullException ("type");
63 return CreateObject (appHost.GetSiteID (),
64 type,
65 appHost.GetVirtualPath (),
66 appHost.GetPhysicalPath (),
67 true,
68 true);
71 public IRegisteredObject CreateObject (string appId, Type type, string virtualPath,
72 string physicalPath, bool failIfExists)
74 return CreateObject (appId, type, virtualPath, physicalPath, failIfExists, true);
77 public IRegisteredObject CreateObject (string appId, Type type, string virtualPath,
78 string physicalPath, bool failIfExists, bool throwOnError)
80 if (appId == null)
81 throw new ArgumentNullException ("appId");
83 if (!VirtualPathUtility.IsAbsolute (virtualPath))
84 throw new ArgumentException ("Relative path no allowed.", "virtualPath");
86 if (String.IsNullOrEmpty (physicalPath))
87 throw new ArgumentException ("Cannot be null or empty", "physicalPath");
89 // 'type' is not checked. If it's null, we'll throw a NullReferenceException
90 if (!typeof (IRegisteredObject).IsAssignableFrom (type))
91 throw new ArgumentException (String.Concat ("Type '", type.Name, "' does not implement IRegisteredObject."), "type");
94 // ArgumentException is thrown for the physical path from the internal object created
95 // in the new application domain.
96 BareApplicationHost host = null;
97 if (id_to_host.ContainsKey (appId))
98 host = id_to_host [appId];
100 IRegisteredObject ireg = null;
101 if (host != null) {
102 ireg = CheckIfExists (host, type, failIfExists);
103 if (ireg != null)
104 return ireg;
107 try {
108 if (host == null)
109 host = CreateHost (appId, virtualPath, physicalPath);
110 ireg = host.CreateInstance (type);
111 } catch (Exception) {
112 if (throwOnError)
113 throw;
116 if (ireg != null && host.GetObject (type) == null) // If not registered from ctor...
117 host.RegisterObject (ireg, true);
119 return ireg;
122 // Used from ClientBuildManager
123 internal BareApplicationHost CreateHostWithCheck (string appId, string vpath, string ppath)
125 if (id_to_host.ContainsKey (appId))
126 throw new InvalidOperationException ("Already have a host with the same appId");
128 return CreateHost (appId, vpath, ppath);
131 BareApplicationHost CreateHost (string appId, string vpath, string ppath)
133 BareApplicationHost host;
134 host = (BareApplicationHost) ApplicationHost.CreateApplicationHost (typeof (BareApplicationHost), vpath, ppath);
135 host.Manager = this;
136 host.AppID = appId;
137 id_to_host [appId] = host;
138 return host;
141 internal void RemoveHost (string appId)
143 id_to_host.Remove (appId);
146 IRegisteredObject CheckIfExists (BareApplicationHost host, Type type, bool failIfExists)
148 IRegisteredObject ireg = host.GetObject (type);
149 if (ireg == null)
150 return null;
152 if (failIfExists)
153 throw new InvalidOperationException (String.Concat ("Well known object of type '", type.Name, "' already exists in this domain."));
155 return ireg;
158 public static ApplicationManager GetApplicationManager ()
160 return instance;
163 public IRegisteredObject GetObject (string appId, Type type)
165 if (appId == null)
166 throw new ArgumentNullException ("appId");
168 if (type == null)
169 throw new ArgumentNullException ("type");
171 BareApplicationHost host = null;
172 if (!id_to_host.ContainsKey (appId))
173 return null;
175 host = id_to_host [appId];
176 return host.GetObject (type);
179 public ApplicationInfo [] GetRunningApplications ()
181 ICollection<string> coll = id_to_host.Keys;
182 string [] keys = new string [coll.Count];
183 coll.CopyTo (keys, 0);
184 ApplicationInfo [] result = new ApplicationInfo [coll.Count];
185 int i = 0;
186 foreach (string str in keys) {
187 BareApplicationHost host = id_to_host [str];
188 result [i++] = new ApplicationInfo (str, host.PhysicalPath, host.VirtualPath);
191 return result;
194 public override object InitializeLifetimeService ()
196 return null;
199 public bool IsIdle ()
201 throw new NotImplementedException ();
204 public void Open ()
206 Interlocked.Increment (ref users);
209 public void ShutdownAll ()
211 ICollection<string> coll = id_to_host.Keys;
212 string [] keys = new string [coll.Count];
213 coll.CopyTo (keys, 0);
214 foreach (string str in keys) {
215 BareApplicationHost host = id_to_host [str];
216 host.Shutdown ();
219 id_to_host.Clear ();
222 public void ShutdownApplication (string appId)
224 if (appId == null)
225 throw new ArgumentNullException ("appId");
227 BareApplicationHost host = id_to_host [appId];
228 if (host == null)
229 return;
231 host.Shutdown ();
234 public void StopObject (string appId, Type type)
236 if (appId == null)
237 throw new ArgumentNullException ("appId");
239 if (type == null)
240 throw new ArgumentNullException ("type");
242 if (!id_to_host.ContainsKey (appId))
243 return;
245 BareApplicationHost host = id_to_host [appId];
246 if (host == null)
247 return;
249 host.StopObject (type);