importing messaging-2008 branch to trunk, going on.
[mono.git] / mcs / class / System.Web / System.Web.Hosting / ApplicationManager.cs
1 //
2 // System.Web.Hosting.ApplicationManager
3 // 
4 // Author:
5 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
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
37 namespace System.Web.Hosting {
38         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         public sealed class ApplicationManager : MarshalByRefObject {
40                 static ApplicationManager instance = new ApplicationManager ();
41                 int users;
42                 Dictionary <string, BareApplicationHost> id_to_host;
43
44                 ApplicationManager ()
45                 {
46                         id_to_host = new Dictionary<string, BareApplicationHost> ();
47                 }
48
49                 public void Close ()
50                 {
51                         if (Interlocked.Decrement (ref users) == 0)
52                                 ShutdownAll ();
53                 }
54
55                 public IRegisteredObject CreateObject (string appId, Type type, string virtualPath,
56                                                         string physicalPath, bool failIfExists)
57                 {
58                         return CreateObject (appId, type, virtualPath, physicalPath, failIfExists, true);
59                 }
60
61                 public IRegisteredObject CreateObject (string appId, Type type, string virtualPath,
62                                                         string physicalPath, bool failIfExists, bool throwOnError)
63                 {
64                         if (appId == null)
65                                 throw new ArgumentNullException ("appId");
66
67                         if (!VirtualPathUtility.IsAbsolute (virtualPath))
68                                 throw new ArgumentException ("Relative path no allowed.", "virtualPath");
69
70                         if (physicalPath == null || physicalPath == "")
71                                 throw new ArgumentException ("Cannot be null or empty", "physicalPath");
72
73                         // 'type' is not checked. If it's null, we'll throw a NullReferenceException
74                         if (!typeof (IRegisteredObject).IsAssignableFrom (type))
75                                 throw new ArgumentException (String.Concat ("Type '", type.Name, "' does not implement IRegisteredObject."), "type");
76
77                         //
78                         // ArgumentException is thrown for the physical path from the internal object created
79                         // in the new application domain.
80                         BareApplicationHost host = null;
81                         if (id_to_host.ContainsKey (appId))
82                                 host = id_to_host [appId];
83
84                         IRegisteredObject ireg = null;
85                         if (host != null) {
86                                 ireg = CheckIfExists (host, type, failIfExists);
87                                 if (ireg != null)
88                                         return ireg;
89                         }
90
91                         try {
92                                 if (host == null)
93                                         host = CreateHost (appId, virtualPath, physicalPath);
94                                 ireg = host.CreateInstance (type);
95                         } catch (Exception) {
96                                 if (throwOnError)
97                                         throw;
98                         }
99
100                         if (ireg != null && host.GetObject (type) == null) // If not registered from ctor...
101                                 host.RegisterObject (ireg, true);
102
103                         return ireg;
104                 }
105
106                 // Used from ClientBuildManager
107                 internal BareApplicationHost CreateHostWithCheck (string appId, string vpath, string ppath)
108                 {
109                         if (id_to_host.ContainsKey (appId))
110                                 throw new InvalidOperationException ("Already have a host with the same appId");
111
112                         return CreateHost (appId, vpath, ppath);
113                 }
114
115                 BareApplicationHost CreateHost (string appId, string vpath, string ppath)
116                 {
117                         BareApplicationHost host;
118                         host = (BareApplicationHost) ApplicationHost.CreateApplicationHost (typeof (BareApplicationHost), vpath, ppath);
119                         host.Manager = this;
120                         host.AppID = appId;
121                         id_to_host [appId] = host;
122                         return host;
123                 }
124
125                 internal void RemoveHost (string appId)
126                 {
127                         id_to_host.Remove (appId);
128                 }
129
130                 IRegisteredObject CheckIfExists (BareApplicationHost host, Type type, bool failIfExists)
131                 {
132                         IRegisteredObject ireg = host.GetObject (type);
133                         if (ireg == null)
134                                 return null;
135
136                         if (failIfExists)
137                                 throw new InvalidOperationException (String.Concat ("Well known object of type '", type.Name, "' already exists in this domain."));
138
139                         return ireg;
140                 }
141
142                 public static ApplicationManager GetApplicationManager ()
143                 {
144                         return instance;
145                 }
146
147                 public IRegisteredObject GetObject (string appId, Type type)
148                 {
149                         if (appId == null)
150                                 throw new ArgumentNullException ("appId");
151
152                         if (type == null)
153                                 throw new ArgumentNullException ("type");
154
155                         BareApplicationHost host = null;
156                         if (!id_to_host.ContainsKey (appId))
157                                 return null;
158
159                         host = id_to_host [appId];
160                         return host.GetObject (type);
161                 }
162
163                 public ApplicationInfo [] GetRunningApplications ()
164                 {
165                         ICollection<string> coll = id_to_host.Keys;
166                         string [] keys = new string [coll.Count];
167                         coll.CopyTo (keys, 0);
168                         ApplicationInfo [] result = new ApplicationInfo [coll.Count];
169                         int i = 0;
170                         foreach (string str in keys) {
171                                 BareApplicationHost host = id_to_host [str];
172                                 result [i++] = new ApplicationInfo (str, host.PhysicalPath, host.VirtualPath);
173                         }
174
175                         return result;
176                 }
177
178                 public override object InitializeLifetimeService ()
179                 {
180                         return null;
181                 }
182
183                 public bool IsIdle ()
184                 {
185                         throw new NotImplementedException ();
186                 }
187
188                 public void Open ()
189                 {
190                         Interlocked.Increment (ref users);
191                 }
192
193                 public void ShutdownAll ()
194                 {
195                         ICollection<string> coll = id_to_host.Keys;
196                         string [] keys = new string [coll.Count];
197                         coll.CopyTo (keys, 0);
198                         foreach (string str in keys) {
199                                 BareApplicationHost host = id_to_host [str];
200                                 host.Shutdown ();
201                         }
202
203                         id_to_host.Clear ();
204                 }
205
206                 public void ShutdownApplication (string appId)
207                 {
208                         if (appId == null)
209                                 throw new ArgumentNullException ("appId");
210
211                         BareApplicationHost host = id_to_host [appId];
212                         if (host == null)
213                                 return;
214
215                         host.Shutdown ();
216                 }
217
218                 public void StopObject (string appId, Type type)
219                 {
220                         if (appId == null)
221                                 throw new ArgumentNullException ("appId");
222
223                         if (type == null)
224                                 throw new ArgumentNullException ("type");
225
226                         if (!id_to_host.ContainsKey (appId))
227                                 return;
228
229                         BareApplicationHost host = id_to_host [appId];
230                         if (host == null)
231                                 return;
232
233                         host.StopObject (type);
234                 }
235         }
236 }
237
238 #endif
239