Added RedirectUsingNamedRoute
[castle.git] / InversionOfControl / Castle.Windsor / Proxy / AbstractProxyFactory.cs
blobe0acd4ae7728183f768e613c85e6b8c97796227b
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.Windsor.Proxy
17 using System;
19 using Castle.Core;
20 using Castle.Core.Interceptor;
21 using Castle.MicroKernel;
23 public abstract class AbstractProxyFactory : IProxyFactory
25 public abstract object Create(IKernel kernel, object instance,
26 ComponentModel model, params object[] constructorArguments);
28 public abstract bool RequiresTargetInstance(IKernel kernel, ComponentModel model);
30 /// <summary>
31 /// Obtains the interceptors associated with the component.
32 /// </summary>
33 /// <param name="kernel">The kernel instance</param>
34 /// <param name="model">The component model</param>
35 /// <returns>interceptors array</returns>
36 protected IInterceptor[] ObtainInterceptors(IKernel kernel, ComponentModel model)
38 IInterceptor[] interceptors = new IInterceptor[model.Interceptors.Count];
39 int index = 0;
41 foreach(InterceptorReference interceptorRef in model.Interceptors)
43 IHandler handler;
45 if (interceptorRef.ReferenceType == InterceptorReferenceType.Interface)
47 handler = kernel.GetHandler(interceptorRef.ServiceType);
49 else
51 handler = kernel.GetHandler(interceptorRef.ComponentKey);
54 if (handler == null)
56 // This shoul be virtually impossible to happen
57 // Seriously!
58 throw new ApplicationException("The interceptor could not be resolved");
61 try
63 IInterceptor interceptor = (IInterceptor) handler.Resolve(CreationContext.Empty);
65 interceptors[index++] = interceptor;
67 SetOnBehalfAware(interceptor as IOnBehalfAware, model);
69 catch(InvalidCastException)
71 String message = String.Format(
72 "An interceptor registered for {0} doesnt implement " +
73 "the IMethodInterceptor interface",
74 model.Name);
76 throw new ApplicationException(message);
80 return interceptors;
83 protected void SetOnBehalfAware(IOnBehalfAware onBehalfAware, ComponentModel target)
85 if (onBehalfAware != null)
87 onBehalfAware.SetInterceptedComponentModel(target);