Added RedirectUsingNamedRoute
[castle.git] / InversionOfControl / Castle.MicroKernel / ModelBuilder / Inspectors / InterceptorInspector.cs
blob0f5bdd3d48cc70916f1e19b5aa20f12e23595dc0
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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.MicroKernel.ModelBuilder.Inspectors
17 using System;
18 using System.Configuration;
20 using Castle.MicroKernel.Util;
21 using Castle.Core;
22 using Castle.Core.Configuration;
24 /// <summary>
25 /// Inspect the component for <c>InterceptorAttribute</c> and
26 /// the configuration for the interceptors node
27 /// </summary>
28 [Serializable]
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})",
54 value);
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 ))
71 return;
74 object[] attributes = model.Implementation.GetCustomAttributes(true);
76 foreach(object attribute in attributes)
78 if (attribute is InterceptorAttribute)
80 InterceptorAttribute attr = (attribute as InterceptorAttribute);
82 AddInterceptor(
83 attr.Interceptor,
84 model.Interceptors );
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 );