1 // Copyright 2003-2004 DigitalCraftsmen - http://www.digitalcraftsmen.com.br/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
19 using System
.Collections
;
20 using System
.Configuration
;
21 using System
.Security
.Policy
;
23 using Castle
.ManagementExtensions
.Default
;
26 /// Summary description for MServerFactory.
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()
40 /// Creates a <see cref="MServer"/> instance.
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
);
51 /// Creates a <see cref="MServer"/> instance.
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
)
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..
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(
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
;
123 object localInstance
= Activator
.CreateInstance(serverType
);
125 // Register the domain
127 domains
.Add(domain
, new DomainInfo( domain
, localInstance
as MServer
) );
129 return (MServer
) localInstance
;
134 /// Releases a <see cref="MServer"/> instance. This method
135 /// accepts a null argument.
137 /// <param name="server">The <see cref="MServer"/> instance to be released.</param>
138 public static void Release(MServer server
)
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
);
161 /// Holds registered domains information.
166 public AppDomain DedicatedDomain
;
167 public MServer Server
;
169 private DomainInfo(String 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
;