Added generalized installation capabilities to Windsor (analogous to Kernel registration)
[castle.git] / Tools / ManagedExtensions / ManagementExtensions / MServerFactory.cs
blob1598ccde9182f029f85366059aa83eb3a8e5be38
1 // Copyright 2003-2004 DigitalCraftsmen - http://www.digitalcraftsmen.com.br/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.ManagementExtensions
17 using System;
18 using System.IO;
19 using System.Collections;
20 using System.Configuration;
21 using System.Security.Policy;
23 using Castle.ManagementExtensions.Default;
25 /// <summary>
26 /// Summary description for MServerFactory.
27 /// </summary>
28 public sealed class MServerFactory
30 public static readonly String CustomServerConfigurationKey = "MServerFactory";
32 private static readonly Hashtable domains = Hashtable.Synchronized(
33 new Hashtable(CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default));
35 private MServerFactory()
39 /// <summary>
40 /// Creates a <see cref="MServer"/> instance.
41 /// </summary>
42 /// <param name="createNewAppDomain">true if MServerFactory should create a dedicated
43 /// AppDomain for the <see cref="MServer"/> instance.</param>
44 /// <returns>A <see cref="MServer"/> instance.</returns>
45 public static MServer CreateServer(bool createNewAppDomain)
47 return CreateServer(String.Empty, createNewAppDomain);
50 /// <summary>
51 /// Creates a <see cref="MServer"/> instance.
52 /// </summary>
53 /// <param name="domain">The domain name</param>
54 /// <param name="createNewAppDomain">true if MServerFactory should create a dedicated
55 /// AppDomain for the <see cref="MServer"/> instance.</param>
56 /// <returns>A <see cref="MServer"/> instance.</returns>
57 public static MServer CreateServer(String domain, bool createNewAppDomain)
59 if (domain == null)
61 throw new ArgumentNullException("domain");
64 if (domains.Contains(domain))
66 throw new DomainAlreadyExistsException(domain);
69 String typeName = ConfigurationSettings.AppSettings[CustomServerConfigurationKey];
70 Type serverType = null;
72 if (typeName != null && typeName != String.Empty)
74 // TODO: Allow custom servers..
76 else
78 serverType = typeof(MDefaultServer);
81 if (createNewAppDomain)
83 // Lets create a seperated AppDomain for this server
85 AppDomain currentDomain = AppDomain.CurrentDomain;
87 String baseDir = new FileInfo(currentDomain.BaseDirectory).FullName;
89 String configFile = String.Format(
90 "{0}/{1}.config",
91 baseDir, domain);
93 AppDomainSetup setup = new AppDomainSetup();
95 setup.ApplicationName = domain;
96 setup.ApplicationBase = currentDomain.SetupInformation.ApplicationBase;
97 setup.PrivateBinPath = currentDomain.SetupInformation.PrivateBinPath;
98 setup.ConfigurationFile = configFile;
99 // setup.ShadowCopyFiles = "false";
100 // setup.ShadowCopyDirectories = appBase;
102 Evidence baseEvidence = currentDomain.Evidence;
103 Evidence evidence = new Evidence(baseEvidence);
105 AppDomain newDomain = AppDomain.CreateDomain(
106 domain, evidence, setup);
108 object remoteInstance = newDomain.CreateInstanceAndUnwrap(
109 serverType.Assembly.FullName, serverType.FullName);
111 // Register the domain
113 domains.Add(domain, new DomainInfo( domain, remoteInstance as MServer, newDomain) );
115 // As this already method "unwraps" the target object, its safe
116 // to return it - in an "wrapped" object we should invoke the
117 // class's constructor
119 return (MServer) remoteInstance;
121 else
123 object localInstance = Activator.CreateInstance(serverType);
125 // Register the domain
127 domains.Add(domain, new DomainInfo( domain, localInstance as MServer ) );
129 return (MServer) localInstance;
133 /// <summary>
134 /// Releases a <see cref="MServer"/> instance. This method
135 /// accepts a null argument.
136 /// </summary>
137 /// <param name="server">The <see cref="MServer"/> instance to be released.</param>
138 public static void Release(MServer server)
140 if (server != null)
142 foreach(DomainInfo info in domains.Values)
144 if (info.Server == server)
146 domains.Remove( info.Name );
148 if (info.DedicatedDomain != null)
150 AppDomain.Unload( info.DedicatedDomain );
153 break;
160 /// <summary>
161 /// Holds registered domains information.
162 /// </summary>
163 class DomainInfo
165 public String Name;
166 public AppDomain DedicatedDomain;
167 public MServer Server;
169 private DomainInfo(String name)
171 this.Name = name;
174 public DomainInfo(String name, MServer Server) : this(name)
176 this.Server = Server;
179 public DomainInfo(String name, MServer Server, AppDomain domain) : this(name, Server)
181 this.DedicatedDomain = domain;