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
18 using System
.Collections
;
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
)
35 public override object InitializeLifetimeService()
40 public void AddComponentEntry(ComponentModel model
)
42 if (model
.Service
.IsGenericType
)
44 genericEntries
[model
.Service
] = model
;
48 entries
[model
.Name
] = model
;
51 public object CreateRemoteInstance(String key
)
58 private ComponentModel
GetModel(string key
)
60 ComponentModel model
= (ComponentModel
) entries
[key
];
64 throw new KernelException(
65 String
.Format("No remote/available component found for key {0}", key
));
71 public void Publish(string key
)
73 ComponentModel model
= GetModel(key
);
75 MarshalByRefObject mbr
= (MarshalByRefObject
) kernel
[key
];
77 RemoteMarshallerActivator
.Marshal(mbr
, model
);
81 /// Used in case of generics:
83 /// <param name="serviceType"></param>
84 /// <returns></returns>
85 private ComponentModel
GetModel(Type serviceType
)
87 ComponentModel model
= (ComponentModel
) genericEntries
[serviceType
];
91 throw new KernelException(
92 String
.Format("No remote/available component found for service type {0}", serviceType
));
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()
119 genericEntries
.Clear();