1 // Copyright 2004-2008 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
.MicroKernel
.ModelBuilder
.Inspectors
18 using System
.Configuration
;
22 /// Inspects the component configuration and the type looking for a
23 /// definition of component activator type. The configuration preceeds whatever
24 /// is defined in the component.
27 /// This inspector is not guarantee to always set up an component activator type.
28 /// If nothing could be found it wont touch the model. In this case is up to
29 /// the kernel to establish a default component activator for components.
32 public class ComponentActivatorInspector
: IContributeComponentModelConstruction
35 /// Seaches for the component activator in the configuration and, if unsuccessful
36 /// look for the component activator attribute in the implementation type.
38 /// <param name="kernel">The kernel instance</param>
39 /// <param name="model">The model instance</param>
40 public virtual void ProcessModel(IKernel kernel
, ComponentModel model
)
42 if (!ReadComponentActivatorFromConfiguration(model
))
44 ReadComponentActivatorFromType(model
);
49 /// Reads the attribute "componentActivatorType" associated with the
50 /// component configuration and verifies it implements the <see cref="IComponentActivator"/>
53 /// <exception cref="System.Configuration.ConfigurationException">
54 /// If the type does not implement the proper interface
56 /// <param name="model"></param>
57 /// <returns></returns>
58 protected virtual bool ReadComponentActivatorFromConfiguration(ComponentModel model
)
60 if (model
.Configuration
!= null)
62 string componentActivatorType
= model
.Configuration
.Attributes
["componentActivatorType"];
64 if (componentActivatorType
== null)
71 Type customComponentActivator
= Type
.GetType(componentActivatorType
, true, false);
73 ValidateComponentActivator(customComponentActivator
);
75 model
.CustomComponentActivator
= customComponentActivator
;
80 String
.Format("The Type '{0}' specified in the componentActivatorType attribute could not be loaded.",
81 componentActivatorType
);
83 throw new ConfigurationErrorsException(message
, ex
);
91 /// Check if the type expose one of the component activator attributes
92 /// defined in Castle.Core namespace.
94 /// <param name="model"></param>
95 protected virtual void ReadComponentActivatorFromType(ComponentModel model
)
97 object[] attributes
= model
.Implementation
.GetCustomAttributes(typeof(ComponentActivatorAttribute
), true);
99 if (attributes
.Length
!= 0)
101 ComponentActivatorAttribute attribute
= (ComponentActivatorAttribute
) attributes
[0];
103 ValidateComponentActivator(attribute
.ComponentActivatorType
);
105 model
.CustomComponentActivator
= attribute
.ComponentActivatorType
;
110 /// Validates that the provide type implements IComponentActivator
112 /// <param name="customComponentActivator">The custom component activator.</param>
113 protected virtual void ValidateComponentActivator(Type customComponentActivator
)
115 if (!typeof(IComponentActivator
).IsAssignableFrom(customComponentActivator
))
119 "The Type '{0}' specified in the componentActivatorType attribute must implement Castle.MicroKernel.IComponentActivator",
120 customComponentActivator
.FullName
);
121 throw new InvalidOperationException(message
);