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
;
20 using Castle
.MicroKernel
.Util
;
22 using Castle
.Core
.Configuration
;
25 /// Inspect the component for <c>InterceptorAttribute</c> and
26 /// the configuration for the interceptors node
29 public class InterceptorInspector
: IContributeComponentModelConstruction
31 public virtual void ProcessModel(IKernel kernel
, ComponentModel model
)
33 CollectFromAttributes(model
);
34 CollectFromConfiguration(model
);
37 protected virtual void CollectFromConfiguration(ComponentModel model
)
39 if (model
.Configuration
== null) return;
41 IConfiguration interceptors
= model
.Configuration
.Children
["interceptors"];
43 if (interceptors
== null) return;
45 foreach(IConfiguration interceptor
in interceptors
.Children
)
47 String
value = interceptor
.Value
;
49 if (!ReferenceExpressionUtil
.IsReference(value))
51 String message
= String
.Format(
52 "The value for the interceptor must be a reference " +
53 "to a component (Currently {0})",
56 throw new ConfigurationErrorsException(message
);
59 InterceptorReference interceptorRef
=
60 new InterceptorReference( ReferenceExpressionUtil
.ExtractComponentKey(value) );
62 model
.Interceptors
.Add(interceptorRef
);
63 model
.Dependencies
.Add( CreateDependencyModel(interceptorRef
) );
67 protected virtual void CollectFromAttributes(ComponentModel model
)
69 if (!model
.Implementation
.IsDefined( typeof(InterceptorAttribute
), true ))
74 object[] attributes
= model
.Implementation
.GetCustomAttributes(true);
76 foreach(object attribute
in attributes
)
78 if (attribute
is InterceptorAttribute
)
80 InterceptorAttribute attr
= (attribute
as InterceptorAttribute
);
86 model
.Dependencies
.Add(
87 CreateDependencyModel(attr
.Interceptor
) );
92 protected DependencyModel
CreateDependencyModel(InterceptorReference interceptor
)
94 return new DependencyModel(DependencyType
.Service
, interceptor
.ComponentKey
,
95 interceptor
.ServiceType
, false);
98 protected void AddInterceptor(InterceptorReference interceptorRef
, InterceptorReferenceCollection interceptors
)
100 interceptors
.Add( interceptorRef
);