1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
.Facilities
.Remoting
19 using System
.Configuration
;
20 using System
.Runtime
.Remoting
;
22 using Castle
.MicroKernel
;
23 using Castle
.MicroKernel
.Facilities
;
24 using Castle
.MicroKernel
.SubSystems
.Conversion
;
27 /// Facility to allow the communication with remote kernel, using the .NET Remoting infraestructure.
35 public class RemotingFacility
: AbstractFacility
37 private ITypeConverter converter
;
39 private bool isServer
, isClient
;
40 private bool disconnectLocalRegistry
;
43 /// Used for client side (Expand explanation)
45 private String baseUri
;
48 /// Used for server side.
49 /// Holds the local registry
51 private RemotingRegistry localRegistry
;
54 /// Used for client side.
55 /// Holds a remote proxy to the server registry
57 private RemotingRegistry remoteRegistry
;
60 /// Constructs a RemotingFacility
62 public RemotingFacility()
66 protected override void Init()
70 SetUpRemotingConfiguration();
72 baseUri
= FacilityConfig
.Attributes
["baseUri"];
74 String isServerAttValue
= FacilityConfig
.Attributes
["isServer"];
75 String isClientAttValue
= FacilityConfig
.Attributes
["isClient"];
77 if ("true".Equals(isServerAttValue
))
80 ConfigureServerFacility();
83 if ("true".Equals(isClientAttValue
))
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
);
112 RemotingConfiguration
.Configure(configurationFile
, false);
114 RemotingConfiguration
.Configure(configurationFile
);
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
);
161 /// Performs the tasks associated with freeing, releasing, or resetting
162 /// the facility resources.
164 /// <remarks>It can be overriden.</remarks>
165 public override void Dispose()
167 if (disconnectLocalRegistry
) RemotingServices
.Disconnect(localRegistry
);