Added container accessor to Castle.Core
[castle.git] / InversionOfControl / Castle.MicroKernel / Facilities / Remoting / RemotingFacility.cs
blob67582deb897a0e7d832f20801eeaf2032e4a5ef2
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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.Facilities.Remoting
17 using System;
18 using System.IO;
19 using System.Configuration;
20 using System.Runtime.Remoting;
22 using Castle.MicroKernel;
23 using Castle.MicroKernel.Facilities;
24 using Castle.MicroKernel.SubSystems.Conversion;
26 /// <summary>
27 /// Facility to allow the communication with remote kernel, using the .NET Remoting infraestructure.
28 /// </summary>
29 /// <remarks>
30 /// TODO
31 /// </remarks>
32 /// <example>
33 /// TODO
34 /// </example>
35 public class RemotingFacility : AbstractFacility
37 private ITypeConverter converter;
39 private bool isServer, isClient;
40 private bool disconnectLocalRegistry;
42 /// <summary>
43 /// Used for client side (Expand explanation)
44 /// </summary>
45 private String baseUri;
47 /// <summary>
48 /// Used for server side.
49 /// Holds the local registry
50 /// </summary>
51 private RemotingRegistry localRegistry;
53 /// <summary>
54 /// Used for client side.
55 /// Holds a remote proxy to the server registry
56 /// </summary>
57 private RemotingRegistry remoteRegistry;
59 /// <summary>
60 /// Constructs a RemotingFacility
61 /// </summary>
62 public RemotingFacility()
66 protected override void Init()
68 ObtainConverter();
70 SetUpRemotingConfiguration();
72 baseUri = FacilityConfig.Attributes["baseUri"];
74 String isServerAttValue = FacilityConfig.Attributes["isServer"];
75 String isClientAttValue = FacilityConfig.Attributes["isClient"];
77 if ("true".Equals(isServerAttValue))
79 isServer = true;
80 ConfigureServerFacility();
83 if ("true".Equals(isClientAttValue))
85 isClient = true;
86 ConfigureClientFacility();
89 Kernel.ComponentModelBuilder.AddContributor(
90 new RemotingInspector(converter, isServer, isClient, baseUri, remoteRegistry, localRegistry));
93 private void SetUpRemotingConfiguration()
95 String configurationFile = FacilityConfig.Attributes["remotingConfigurationFile"];
97 if (configurationFile == null) return;
99 if (!Path.IsPathRooted(configurationFile))
101 configurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configurationFile);
104 if (!File.Exists(configurationFile))
106 String message = String.Format("Remoting configuration file '{0}' does not exist", configurationFile);
108 throw new ConfigurationErrorsException(message);
111 #if !MONO
112 RemotingConfiguration.Configure(configurationFile, false);
113 #else
114 RemotingConfiguration.Configure(configurationFile);
115 #endif
118 private void ConfigureServerFacility()
120 Kernel.AddComponent("remoting.registry", typeof(RemotingRegistry));
122 localRegistry = (RemotingRegistry) Kernel[ typeof(RemotingRegistry) ];
124 String kernelUri = FacilityConfig.Attributes["registryUri"];
126 if (kernelUri == null || kernelUri.Length == 0)
128 String message = "When the remote facility is configured as " +
129 "server you must supply the URI for the component registry using the attribute 'registryUri'";
131 throw new ConfigurationErrorsException(message);
134 RemotingServices.Marshal(localRegistry, kernelUri, typeof(RemotingRegistry));
136 disconnectLocalRegistry = true;
139 private void ConfigureClientFacility()
141 String remoteKernelUri = FacilityConfig.Attributes["remoteKernelUri"];
143 if (remoteKernelUri == null || remoteKernelUri.Length == 0)
145 String message = "When the remote facility is configured as " +
146 "client you must supply the URI for the kernel using the attribute 'remoteKernelUri'";
148 throw new ConfigurationErrorsException(message);
151 remoteRegistry = (RemotingRegistry)
152 RemotingServices.Connect(typeof(RemotingRegistry), remoteKernelUri);
155 private void ObtainConverter()
157 converter = (ITypeConverter) Kernel.GetSubSystem(SubSystemConstants.ConversionManagerKey);
160 /// <summary>
161 /// Performs the tasks associated with freeing, releasing, or resetting
162 /// the facility resources.
163 /// </summary>
164 /// <remarks>It can be overriden.</remarks>
165 public override void Dispose()
167 if (disconnectLocalRegistry) RemotingServices.Disconnect(localRegistry);
169 base.Dispose();