Fix java compilation of System.Web.Extensions
[mono.git] / mcs / class / System.Web / System.Web.Hosting / BareApplicationHost.cs
1 //
2 // System.Web.Hosting.BareApplicationHost
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.IO;
32 using System.Collections.Generic;
33
34 namespace System.Web.Hosting {
35         class RegisteredItem {
36                 public IRegisteredObject Item;
37                 public bool AutoClean;
38                 public RegisteredItem (IRegisteredObject item, bool autoclean)
39                 {
40                         this.Item = item;
41                         this.AutoClean = autoclean;
42                 }
43         }
44
45         sealed class BareApplicationHost : MarshalByRefObject {
46                 string vpath;
47                 string phys_path;
48                 Dictionary<Type, RegisteredItem> hash;
49                 internal ApplicationManager Manager;
50                 internal string AppID;
51
52                 public BareApplicationHost ()
53                 {
54                         Init ();
55                 }
56
57                 void Init ()
58                 {
59                         hash = new Dictionary<Type, RegisteredItem> ();
60                         HostingEnvironment.Host = this;
61                         AppDomain current = AppDomain.CurrentDomain;
62                         current.DomainUnload += OnDomainUnload;
63                         phys_path = (string) current.GetData (".appPath");
64                         vpath = (string) current.GetData (".appVPath");
65                 }
66
67                 public string VirtualPath {
68                         get { return vpath; }
69                 }
70
71                 public string PhysicalPath {
72                         get { return phys_path; }
73                 }
74
75                 public AppDomain Domain {
76                         get { return AppDomain.CurrentDomain; }
77                 }
78
79                 public void Shutdown ()
80                 {
81                         HostingEnvironment.InitiateShutdown ();
82                 }
83
84                 public void StopObject (Type type)
85                 {
86                         if (!hash.ContainsKey (type))
87                                 return;
88
89                         RegisteredItem reg = hash [type];
90                         reg.Item.Stop (false);
91                 }
92
93                 public IRegisteredObject CreateInstance (Type type)
94                 {
95                         return (IRegisteredObject) Activator.CreateInstance (type, null);
96                 }
97
98                 public void RegisterObject (IRegisteredObject obj, bool auto_clean)
99                 {
100                         hash [obj.GetType ()] = new RegisteredItem (obj, auto_clean);
101                 }
102
103                 public bool UnregisterObject (IRegisteredObject obj)
104                 {
105                         return hash.Remove (obj.GetType ());
106                 }
107
108                 public IRegisteredObject GetObject (Type type)
109                 {
110                         if (hash.ContainsKey (type))
111                                 return hash [type].Item;
112
113                         return null;
114                 }
115
116                 public string GetCodeGenDir ()
117                 {
118                         return AppDomain.CurrentDomain.SetupInformation.DynamicBase;
119                 }
120
121                 void OnDomainUnload (object sender, EventArgs args)
122                 {
123                         Manager.RemoveHost (AppID);
124                         ICollection<RegisteredItem> values = hash.Values;
125                         RegisteredItem [] objects = new RegisteredItem [hash.Count];
126                         values.CopyTo (objects, 0);
127
128                         foreach (RegisteredItem reg in objects) {
129                                 try {
130                                         reg.Item.Stop (true); // Stop should call Unregister. It's ok if not.
131                                 } catch {
132                                         // Ignore or throw?
133                                 }
134                         }
135                         hash.Clear ();
136                 }
137         }
138 }
139
140 #endif
141