1 // Copyright 2003-2004 DigitalCraftsmen - http://www.digitalcraftsmen.com.br/
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
.ManagementExtensions
.Default
20 /// Default (and simple) implementation of <see cref="Castle.ManagementExtensions.MRegistry"/>.
21 /// TODO: Implement a lyfecycle for registering managed components.
24 public class MDefaultRegistry
: MarshalByRefObject
, MRegistry
26 protected DomainCollection domains
= new DomainCollection();
28 protected MServer server
;
30 public MDefaultRegistry(MServer server
)
34 throw new ArgumentNullException("server");
40 #region MRegistry Members
43 /// Registers an instance if its not already registered.
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>
49 public ManagedInstance
RegisterManagedObject(Object instance
, ManagedObjectName name
)
53 throw new ArgumentNullException("name");
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
;
82 domain
= domains
[domainName
];
86 domain
= new Domain(domainName
);
91 Entry entry
= new Entry(instance
, dynamic);
95 MRegistrationListener registration
= instance
as MRegistrationListener
;
97 InvokeBeforeRegister(registration
, name
);
101 if (domain
.Contains(name
))
103 throw new InstanceAlreadyRegistredException(name
.ToString());
106 domain
.Add(name
, entry
);
109 InvokeAfterRegister(registration
);
118 return new ManagedInstance(instance
.GetType().FullName
, name
);
122 /// Returns <see cref="Castle.ManagementExtensions.ManagedInstance"/>
123 /// of specified <see cref="Castle.ManagementExtensions.ManagedObjectName"/>.
125 /// <param name="name">The name to be located.</param>
126 /// <returns>A ManagedInstance representing the instance data.</returns>
128 public ManagedInstance
GetManagedInstance(ManagedObjectName name
)
132 throw new ArgumentNullException("name");
135 return new ManagedInstance(GetEntry(name
).Instance
.GetType().FullName
, name
);
139 /// Unregisters the specified object.
141 /// <param name="name">The name to be located.</param>
143 public void UnregisterManagedObject(ManagedObjectName name
)
147 throw new ArgumentNullException("name");
150 String domainName
= name
.Domain
;
154 Domain domain
= FindDomain(domainName
);
155 Entry entry
= domain
[name
];
159 MRegistrationListener listener
= entry
.Instance
as MRegistrationListener
;
161 InvokeBeforeDeregister(listener
);
165 InvokeAfterDeregister(listener
);
168 catch(InvalidDomainException
)
174 /// Returns true if the Registry contains the specified object.
176 /// <param name="name">The name to be located.</param>
177 /// <returns>true if the object could be found</returns>
179 public bool Contains(ManagedObjectName name
)
183 throw new ArgumentNullException("name");
186 String domainName
= name
.Domain
;
188 Domain domain
= FindDomain(domainName
);
190 return domain
.Contains(name
);
194 /// Returns the number of currently registered objects.
203 foreach(Domain domain
in domains
)
205 total
+= domain
.Count
;
213 /// Indexer for registered objects.
215 public Object
this[ManagedObjectName name
]
221 throw new ArgumentNullException("name");
224 return GetEntry(name
).Instance
;
229 /// Invokes an action in managed object
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
);
243 /// Returns the info (attributes and operations) about the specified object.
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
;
254 /// Gets an attribute value of the specified managed object.
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
);
266 /// Sets an attribute value of the specified managed object.
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
);
278 /// Returns an array of registered domains.
280 /// <returns>a list of domains</returns>
281 public String
[] GetDomains()
283 return domains
.ToArray();
287 /// Queries the registerd components.
289 /// <returns></returns>
290 public ManagedObjectName
[] Query(ManagedObjectName query
)
292 // TODO: several queries...
294 if (query
.LiteralProperties
.Equals("*"))
296 return FindAllFromDomain(query
.Domain
);
305 /// Helper to locate the domain.
307 /// <param name="domainName"></param>
308 /// <returns></returns>
309 private Domain
FindDomain(String domainName
)
311 Domain domain
= domains
[domainName
];
315 throw new InvalidDomainException(domainName
);
322 /// Helper to locate Entries.
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
];
333 throw new ManagedObjectNotFoundException(name
.ToString());
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();
365 // An exception here shall not stop us from continue
370 private void InvokeAfterDeregister(MRegistrationListener listener
)
372 if (listener
!= null)
376 listener
.AfterDeregister();
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
)