Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / Tools / ManagedExtensions / ManagementExtensions / Default / MDefaultRegistry.cs
blob47bd58b16f5891efe64b079883dc96f2a1b971f5
1 // Copyright 2003-2004 DigitalCraftsmen - http://www.digitalcraftsmen.com.br/
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.ManagementExtensions.Default
17 using System;
19 /// <summary>
20 /// Default (and simple) implementation of <see cref="Castle.ManagementExtensions.MRegistry"/>.
21 /// TODO: Implement a lyfecycle for registering managed components.
22 /// </summary>
23 [ManagedComponent]
24 public class MDefaultRegistry : MarshalByRefObject, MRegistry
26 protected DomainCollection domains = new DomainCollection();
28 protected MServer server;
30 public MDefaultRegistry(MServer server)
32 if (server == null)
34 throw new ArgumentNullException("server");
37 this.server = server;
40 #region MRegistry Members
42 /// <summary>
43 /// Registers an instance if its not already registered.
44 /// </summary>
45 /// <param name="instance">Instance to be register - can't be null.</param>
46 /// <param name="name">Instance's name - can't be null.</param>
47 /// <returns>A ManagedInstance representing the instance data.</returns>
48 [ManagedOperation]
49 public ManagedInstance RegisterManagedObject(Object instance, ManagedObjectName name)
51 if (name == null)
53 throw new ArgumentNullException("name");
55 if (instance == null)
57 throw new ArgumentNullException("instance");
60 ComponentType cType = MInspector.Inspect(instance);
61 MDynamicSupport dynamic = null;
63 if (cType == ComponentType.None)
65 throw new InvalidComponentException("Component is not a managed component.");
68 if (cType == ComponentType.Standard)
70 dynamic = MDynamicSupportFactory.Create(instance);
72 else if (cType == ComponentType.Dynamic)
74 dynamic = (MDynamicSupport) instance;
77 String domainName = name.Domain;
78 Domain domain = null;
80 lock(domains)
82 domain = domains[domainName];
84 if (domain == null)
86 domain = new Domain(domainName);
87 domains.Add(domain);
91 Entry entry = new Entry(instance, dynamic);
93 try
95 MRegistrationListener registration = instance as MRegistrationListener;
97 InvokeBeforeRegister(registration, name);
99 lock(domain)
101 if (domain.Contains(name))
103 throw new InstanceAlreadyRegistredException(name.ToString());
106 domain.Add(name, entry);
109 InvokeAfterRegister(registration);
111 catch(Exception e)
113 domain.Remove(name);
115 throw e;
118 return new ManagedInstance(instance.GetType().FullName, name);
121 /// <summary>
122 /// Returns <see cref="Castle.ManagementExtensions.ManagedInstance"/>
123 /// of specified <see cref="Castle.ManagementExtensions.ManagedObjectName"/>.
124 /// </summary>
125 /// <param name="name">The name to be located.</param>
126 /// <returns>A ManagedInstance representing the instance data.</returns>
127 [ManagedOperation]
128 public ManagedInstance GetManagedInstance(ManagedObjectName name)
130 if (name == null)
132 throw new ArgumentNullException("name");
135 return new ManagedInstance(GetEntry(name).Instance.GetType().FullName, name);
138 /// <summary>
139 /// Unregisters the specified object.
140 /// </summary>
141 /// <param name="name">The name to be located.</param>
142 [ManagedOperation]
143 public void UnregisterManagedObject(ManagedObjectName name)
145 if (name == null)
147 throw new ArgumentNullException("name");
150 String domainName = name.Domain;
154 Domain domain = FindDomain(domainName);
155 Entry entry = domain[name];
157 if (entry != null)
159 MRegistrationListener listener = entry.Instance as MRegistrationListener;
161 InvokeBeforeDeregister(listener);
163 domain.Remove(name);
165 InvokeAfterDeregister(listener);
168 catch(InvalidDomainException)
173 /// <summary>
174 /// Returns true if the Registry contains the specified object.
175 /// </summary>
176 /// <param name="name">The name to be located.</param>
177 /// <returns>true if the object could be found</returns>
178 [ManagedOperation]
179 public bool Contains(ManagedObjectName name)
181 if (name == null)
183 throw new ArgumentNullException("name");
186 String domainName = name.Domain;
188 Domain domain = FindDomain(domainName);
190 return domain.Contains(name);
193 /// <summary>
194 /// Returns the number of currently registered objects.
195 /// </summary>
196 [ManagedAttribute]
197 public int Count
201 int total = 0;
203 foreach(Domain domain in domains)
205 total += domain.Count;
208 return total;
212 /// <summary>
213 /// Indexer for registered objects.
214 /// </summary>
215 public Object this[ManagedObjectName name]
219 if (name == null)
221 throw new ArgumentNullException("name");
224 return GetEntry(name).Instance;
228 /// <summary>
229 /// Invokes an action in managed object
230 /// </summary>
231 /// <param name="name"></param>
232 /// <param name="action"></param>
233 /// <param name="args"></param>
234 /// <param name="signature"></param>
235 /// <returns></returns>
236 /// <exception cref="InvalidDomainException">If domain name is not found.</exception>
237 public Object Invoke(ManagedObjectName name, String action, Object[] args, Type[] signature)
239 return GetEntry(name).Invoker.Invoke(action, args, signature);
242 /// <summary>
243 /// Returns the info (attributes and operations) about the specified object.
244 /// </summary>
245 /// <param name="name"></param>
246 /// <returns></returns>
247 /// <exception cref="InvalidDomainException">If domain name is not found.</exception>
248 public ManagementInfo GetManagementInfo(ManagedObjectName name)
250 return GetEntry(name).Invoker.Info;
253 /// <summary>
254 /// Gets an attribute value of the specified managed object.
255 /// </summary>
256 /// <param name="name"></param>
257 /// <param name="attributeName"></param>
258 /// <returns></returns>
259 /// <exception cref="InvalidDomainException">If domain name is not found.</exception>
260 public Object GetAttributeValue(ManagedObjectName name, String attributeName)
262 return GetEntry(name).Invoker.GetAttributeValue(attributeName);
265 /// <summary>
266 /// Sets an attribute value of the specified managed object.
267 /// </summary>
268 /// <param name="name"></param>
269 /// <param name="attributeName"></param>
270 /// <param name="attributeValue"></param>
271 /// <exception cref="InvalidDomainException">If domain name is not found.</exception>
272 public void SetAttributeValue(ManagedObjectName name, String attributeName, Object attributeValue)
274 GetEntry(name).Invoker.SetAttributeValue(attributeName, attributeValue);
277 /// <summary>
278 /// Returns an array of registered domains.
279 /// </summary>
280 /// <returns>a list of domains</returns>
281 public String[] GetDomains()
283 return domains.ToArray();
286 /// <summary>
287 /// Queries the registerd components.
288 /// </summary>
289 /// <returns></returns>
290 public ManagedObjectName[] Query(ManagedObjectName query)
292 // TODO: several queries...
294 if (query.LiteralProperties.Equals("*"))
296 return FindAllFromDomain(query.Domain);
299 return null;
302 #endregion
304 /// <summary>
305 /// Helper to locate the domain.
306 /// </summary>
307 /// <param name="domainName"></param>
308 /// <returns></returns>
309 private Domain FindDomain(String domainName)
311 Domain domain = domains[domainName];
313 if (domain == null)
315 throw new InvalidDomainException(domainName);
318 return domain;
321 /// <summary>
322 /// Helper to locate Entries.
323 /// </summary>
324 /// <param name="name"></param>
325 /// <returns></returns>
326 private Entry GetEntry(ManagedObjectName name)
328 Domain domain = FindDomain(name.Domain);
329 Entry entry = domain[name];
331 if (entry == null)
333 throw new ManagedObjectNotFoundException(name.ToString());
336 return entry;
339 private void InvokeBeforeRegister(MRegistrationListener listener, ManagedObjectName name)
341 if (listener != null)
343 listener.BeforeRegister(server, name);
347 private void InvokeAfterRegister(MRegistrationListener listener)
349 if (listener != null)
351 listener.AfterRegister();
355 private void InvokeBeforeDeregister(MRegistrationListener listener)
357 if (listener != null)
361 listener.BeforeDeregister();
363 catch(Exception)
365 // An exception here shall not stop us from continue
370 private void InvokeAfterDeregister(MRegistrationListener listener)
372 if (listener != null)
376 listener.AfterDeregister();
378 catch(Exception)
380 // An exception here shall not stop us from continue
385 private ManagedObjectName[] FindAllFromDomain(String domainName)
389 Domain domain = FindDomain(domainName);
390 return domain.ToArray();
392 catch(InvalidDomainException)
396 return null;