Add licensing info
[mono.git] / mcs / class / System.Web / System.Web.Hosting / ApplicationHost.cs
1 //
2 // System.Web.Hosting.ApplicationHost
3 //
4 // Authors:
5 //      Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 //      (class signature from Bob Smith <bob@thestuff.net> (C) )
7 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System;
31 using System.Collections;
32 using System.IO;
33 using System.Runtime.Remoting;
34 using System.Web.Util;
35
36 namespace System.Web.Hosting
37 {
38         public sealed class ApplicationHost
39         {
40                 internal class ConfigInitHelper : MarshalByRefObject
41                 {
42                         internal void InitConfig ()
43                         {
44                         }
45                 }
46                 
47                 private ApplicationHost ()
48                 {
49                 }
50
51                 public static object CreateApplicationHost (Type hostType,
52                                                             string virtualDir,
53                                                             string physicalDir)
54                 {
55                         if (hostType == null)
56                                 throw new ArgumentException ("hostType");
57
58                         if (virtualDir == null || virtualDir.Length == 0)
59                                 throw new ArgumentException ("virtualDir");
60                         
61                         if (physicalDir == null || physicalDir.Length == 0)
62                                 throw new ArgumentException ("physicalDir");
63
64                         if (physicalDir [physicalDir.Length - 1] != Path.DirectorySeparatorChar)
65                                 physicalDir += Path.DirectorySeparatorChar;
66
67                         int nowInt = DateTime.Now.ToString ().GetHashCode ();
68                         string nowHash = nowInt.ToString ("x");
69                         nowInt += physicalDir.GetHashCode ();
70                         string sum = nowInt.ToString ("x");
71                         Hashtable hTable = new Hashtable ();
72                         AppDomainSetup domainSetup = new AppDomainSetup();
73
74                         AppDomainFactory.PopulateDomainBindings (nowHash,
75                                                                  sum,
76                                                                  sum,
77                                                                  physicalDir,
78                                                                  virtualDir,
79                                                                  domainSetup,
80                                                                  hTable);
81                         
82                         AppDomain domain = AppDomain.CreateDomain (nowHash, null, domainSetup);
83                         foreach (string key in hTable.Keys)
84                                 domain.SetData (key, (string) hTable [key]);
85
86                         domain.SetData (".hostingVirtualPath", virtualDir);
87                         domain.SetData (".hostingInstallDir", ICalls.GetMachineInstallDirectory ());
88                         InitConfigInNewAppDomain (domain);
89                         ObjectHandle o = domain.CreateInstance (hostType.Assembly.FullName,
90                                                                 hostType.FullName);
91                         return o.Unwrap();
92                 }
93
94                 private static void InitConfigInNewAppDomain (AppDomain appDomain)
95                 {
96                         Type t = typeof (ConfigInitHelper);
97                         ObjectHandle o = appDomain.CreateInstance (t.Assembly.FullName, t.FullName);
98                         ConfigInitHelper helper = (ConfigInitHelper) o.Unwrap();
99                         helper.InitConfig ();
100                 }
101         }
102 }
103