2002-08-20 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[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 using System;
10 using System.Collections;
11 using System.IO;
12 using System.Runtime.Remoting;
13
14 namespace System.Web.Hosting
15 {
16         public sealed class ApplicationHost
17         {
18                 class ConfigInitHelper
19                 {
20                         public void InitConfig ()
21                         {
22                         }
23                 }
24                 
25                 public static object CreateApplicationHost (Type hostType,
26                                                             string virtualDir,
27                                                             string physicalDir)
28                 {
29                         if (hostType == null)
30                                 throw new ArgumentException ("hostType");
31
32                         if (virtualDir == null || virtualDir.Length == 0)
33                                 throw new ArgumentException ("virtualDir");
34                         
35                         if (physicalDir == null || physicalDir.Length == 0)
36                                 throw new ArgumentException ("physicalDir");
37
38                         if (physicalDir [physicalDir.Length - 1] != Path.DirectorySeparatorChar)
39                                 physicalDir += Path.DirectorySeparatorChar;
40
41                         int nowInt = DateTime.Now.ToString ().GetHashCode ();
42                         string nowHash = nowInt.ToString ("x");
43                         nowInt += physicalDir.GetHashCode ();
44                         string sum = nowInt.ToString ("x");
45                         Hashtable hTable = new Hashtable ();
46                         AppDomainSetup domainSetup = new AppDomainSetup();
47
48                         AppDomainFactory.PopulateDomainBindings (nowHash,
49                                                                  sum,
50                                                                  sum,
51                                                                  physicalDir,
52                                                                  virtualDir,
53                                                                  domainSetup,
54                                                                  hTable);
55                         
56                         AppDomain domain = AppDomain.CreateDomain (nowHash, null, domainSetup);
57                         foreach (string key in hTable.Keys)
58                                 domain.SetData (key, (string) hTable [key]);
59
60                         domain.SetData (".hostingVirtualPath", virtualDir);
61                         //domain.SetData(".hostingInstallDir", ?????);
62                         InitConfigInNewAppDomain (domain);
63                         ObjectHandle o = domain.CreateInstance (hostType.Assembly.FullName,
64                                                                 hostType.FullName);
65                         return o.Unwrap();
66                 }
67
68                 private static void InitConfigInNewAppDomain (AppDomain appDomain)
69                 {
70                         Type t = typeof (ConfigInitHelper);
71                         ObjectHandle o = appDomain.CreateInstance (t.Assembly.FullName, t.FullName);
72                         ConfigInitHelper helper = (ConfigInitHelper) o.Unwrap();
73                         helper.InitConfig ();
74                 }
75         }
76 }
77