2 // System.Web.Hosting.ApplicationManager
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
8 // Copyright (C) 2006-2010 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System
.Collections
.Generic
;
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 ();
41 Dictionary
<string, BareApplicationHost
> id_to_host
;
45 id_to_host
= new Dictionary
<string, BareApplicationHost
> ();
50 if (Interlocked
.Decrement (ref users
) == 0)
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
)
59 throw new ArgumentNullException ("appHost");
61 throw new ArgumentNullException ("type");
63 return CreateObject (appHost
.GetSiteID (),
65 appHost
.GetVirtualPath (),
66 appHost
.GetPhysicalPath (),
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
)
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;
102 ireg
= CheckIfExists (host
, type
, failIfExists
);
109 host
= CreateHost (appId
, virtualPath
, physicalPath
);
110 ireg
= host
.CreateInstance (type
);
111 } catch (Exception
) {
116 if (ireg
!= null && host
.GetObject (type
) == null) // If not registered from ctor...
117 host
.RegisterObject (ireg
, true);
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
);
137 id_to_host
[appId
] = 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
);
153 throw new InvalidOperationException (String
.Concat ("Well known object of type '", type
.Name
, "' already exists in this domain."));
158 public static ApplicationManager
GetApplicationManager ()
163 public IRegisteredObject
GetObject (string appId
, Type type
)
166 throw new ArgumentNullException ("appId");
169 throw new ArgumentNullException ("type");
171 BareApplicationHost host
= null;
172 if (!id_to_host
.ContainsKey (appId
))
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
];
186 foreach (string str
in keys
) {
187 BareApplicationHost host
= id_to_host
[str
];
188 result
[i
++] = new ApplicationInfo (str
, host
.PhysicalPath
, host
.VirtualPath
);
194 public override object InitializeLifetimeService ()
199 public bool IsIdle ()
201 throw new NotImplementedException ();
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
];
222 public void ShutdownApplication (string appId
)
225 throw new ArgumentNullException ("appId");
227 BareApplicationHost host
= id_to_host
[appId
];
234 public void StopObject (string appId
, Type type
)
237 throw new ArgumentNullException ("appId");
240 throw new ArgumentNullException ("type");
242 if (!id_to_host
.ContainsKey (appId
))
245 BareApplicationHost host
= id_to_host
[appId
];
249 host
.StopObject (type
);