Merge pull request #1860 from saper/tz-fix
[mono.git] / mcs / class / System.Web / System.Web.Hosting / HostingEnvironment.cs
1 //
2 // System.Web.Hosting.HostingEnvironment.cs
3 //
4 // Author:
5 //      Chris Toshok (toshok@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8
9 //
10 // Copyright (C) 2005,2006 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32
33 using System;
34 using System.Globalization;
35 using System.Security.Permissions;
36 using System.Threading;
37 using System.Web.Configuration;
38 using System.Web.Caching;
39 using System.Web.Util;
40
41 namespace System.Web.Hosting {
42
43         [AspNetHostingPermission (SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Medium)]
44         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.High)]
45         public sealed class HostingEnvironment : MarshalByRefObject
46         {
47                 static bool is_hosted;
48 #pragma warning disable 0649
49                 static string site_name;
50                 static ApplicationShutdownReason shutdown_reason;
51 #pragma warning restore 0649
52                 internal static BareApplicationHost Host;
53                 static VirtualPathProvider vpath_provider = (HttpRuntime.AppDomainAppVirtualPath == null) ? null :
54                                                                 new DefaultVirtualPathProvider ();
55                 static int busy_count;
56
57                 internal static bool HaveCustomVPP {
58                         get;
59                         private set;
60                 }
61                 
62                 public HostingEnvironment ()
63                 {
64                         // The documentation says that this is called once per domain by the ApplicationManager and
65                         // then it throws InvalidOperationException whenever called.
66                         throw new InvalidOperationException ();
67                 }
68
69                 public static string ApplicationID {
70                         get { return HttpRuntime.AppDomainAppId; }
71                 }
72
73                 public static string ApplicationPhysicalPath {
74                         get { return HttpRuntime.AppDomainAppPath; }
75                 }
76
77                 public static string ApplicationVirtualPath {
78                         get { return HttpRuntime.AppDomainAppVirtualPath; }
79                 }
80
81                 public static Cache Cache {
82                         get { return HttpRuntime.Cache; }
83                 }
84
85                 public static Exception InitializationException {
86                         get { return HttpApplication.InitializationException; }
87                 }
88
89                 public static bool IsHosted {
90                         get { return is_hosted; }
91                         internal set { is_hosted = value; }
92                 }
93
94                 public static ApplicationShutdownReason ShutdownReason {
95                         get { return shutdown_reason; }
96                 }
97
98                 public static string SiteName {
99                         get { return site_name; }
100                         internal set { site_name = value; }
101                 }
102
103                 public static VirtualPathProvider VirtualPathProvider {
104                         get { return vpath_provider; }
105                 }
106
107                 public static bool InClientBuildManager {
108                         get {
109                                 // Mono doesn't have a ClientBuildManager, so we can't be in it. Simple as that.
110                                 return false;
111                         }
112                 }
113
114                 public static void DecrementBusyCount ()
115                 {
116                         Interlocked.Decrement (ref busy_count);
117                 }
118
119                 [MonoTODO ("Not implemented")]
120                 public static IDisposable Impersonate ()
121                 {
122                         throw new NotImplementedException ();
123                 }
124
125                 [MonoTODO ("Not implemented")]
126                 public static IDisposable Impersonate (IntPtr token)
127                 {
128                         throw new NotImplementedException ();
129                 }
130
131                 [MonoTODO ("Not implemented")]
132                 public static IDisposable Impersonate (IntPtr userToken, string virtualPath)
133                 {
134                         throw new NotImplementedException ();
135                 }
136
137                 public static void IncrementBusyCount ()
138                 {
139                         Interlocked.Increment (ref busy_count);
140                 }
141
142                 public override object InitializeLifetimeService ()
143                 {
144                         return null;
145                 }
146
147                 public static void InitiateShutdown ()
148                 {
149                         HttpRuntime.UnloadAppDomain ();
150                 }
151
152                 public static string MapPath (string virtualPath)
153                 {
154                         if (virtualPath == null || virtualPath == "")
155                                 throw new ArgumentNullException ("virtualPath");
156                         
157                         HttpContext context = HttpContext.Current;
158                         HttpRequest req = context == null ? null : context.Request;
159                         if (req == null)
160                                 return null;
161
162                         return req.MapPath (virtualPath);
163                 }
164
165                 public static void RegisterObject (IRegisteredObject obj)
166                 {
167                         if (obj == null)
168                                 throw new ArgumentNullException ("obj");
169
170                         if (Host != null)
171                                 Host.RegisterObject (obj, false);
172                 }
173
174                 public static void RegisterVirtualPathProvider (VirtualPathProvider virtualPathProvider)
175                 {
176                         if (HttpRuntime.AppDomainAppVirtualPath == null)
177                                 throw new InvalidOperationException ();
178
179                         if (virtualPathProvider == null)
180                                 throw new ArgumentNullException ("virtualPathProvider");
181
182                         VirtualPathProvider previous = vpath_provider;
183                         vpath_provider = virtualPathProvider;
184                         vpath_provider.InitializeAndSetPrevious (previous);
185                         if (!(virtualPathProvider is DefaultVirtualPathProvider))
186                                 HaveCustomVPP = true;
187                         else
188                                 HaveCustomVPP = false;
189                 }
190                 
191                 public static IDisposable SetCultures (string virtualPath)
192                 {
193                         GlobalizationSection gs = WebConfigurationManager.GetSection ("system.web/globalization", virtualPath) as GlobalizationSection;
194                         IDisposable ret = Thread.CurrentThread.CurrentCulture as IDisposable;
195                         string culture = gs.Culture;
196                         if (String.IsNullOrEmpty (culture))
197                                 return ret;
198                         Thread.CurrentThread.CurrentCulture = new CultureInfo (culture);
199                         return ret;
200                 }
201
202                 public static IDisposable SetCultures ()
203                 {
204                         return SetCultures ("~/");
205                 }
206
207                 public static void UnregisterObject (IRegisteredObject obj)
208                 {
209                         if (obj == null)
210                                 throw new ArgumentNullException ("obj");
211
212                         if (Host != null)
213                                 Host.UnregisterObject (obj);
214                 }
215         }
216 }
217