Added container accessor to Castle.Core
[castle.git] / InversionOfControl / Castle.MicroKernel / Facilities / Remoting / RemotingRegistry.cs
blobe4433401efa85754cfca185306990b3bea1ef06b
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.Collections;
19 using Castle.Core;
20 using Castle.MicroKernel;
23 public class RemotingRegistry : MarshalByRefObject, IDisposable
25 private readonly IKernel kernel;
26 private readonly IDictionary entries = Hashtable.Synchronized(new Hashtable());
28 private readonly IDictionary genericEntries = Hashtable.Synchronized(new Hashtable());
30 public RemotingRegistry(IKernel kernel)
32 this.kernel = kernel;
35 public override object InitializeLifetimeService()
37 return null;
40 public void AddComponentEntry(ComponentModel model)
42 if (model.Service.IsGenericType)
44 genericEntries[model.Service] = model;
45 return;
48 entries[model.Name] = model;
51 public object CreateRemoteInstance(String key)
53 GetModel(key);
55 return kernel[key];
58 private ComponentModel GetModel(string key)
60 ComponentModel model = (ComponentModel) entries[key];
62 if (model == null)
64 throw new KernelException(
65 String.Format("No remote/available component found for key {0}", key));
68 return model;
71 public void Publish(string key)
73 ComponentModel model = GetModel(key);
75 MarshalByRefObject mbr = (MarshalByRefObject) kernel[key];
77 RemoteMarshallerActivator.Marshal(mbr, model);
80 /// <summary>
81 /// Used in case of generics:
82 /// </summary>
83 /// <param name="serviceType"></param>
84 /// <returns></returns>
85 private ComponentModel GetModel(Type serviceType)
87 ComponentModel model = (ComponentModel) genericEntries[serviceType];
89 if (model == null)
91 throw new KernelException(
92 String.Format("No remote/available component found for service type {0}", serviceType));
95 return model;
98 public object CreateRemoteInstance(Type serviceType)
100 return kernel[serviceType];
104 public void Publish(Type serviceType)
106 ComponentModel model = GetModel(serviceType);
108 MarshalByRefObject mbr = (MarshalByRefObject) kernel[serviceType];
110 RemoteMarshallerActivator.Marshal(mbr, model);
113 #region IDisposable Members
115 public void Dispose()
117 entries.Clear();
119 genericEntries.Clear();
122 #endregion