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
.Windsor
.Proxy
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
);
31 /// Obtains the interceptors associated with the component.
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
];
41 foreach(InterceptorReference interceptorRef
in model
.Interceptors
)
45 if (interceptorRef
.ReferenceType
== InterceptorReferenceType
.Interface
)
47 handler
= kernel
.GetHandler(interceptorRef
.ServiceType
);
51 handler
= kernel
.GetHandler(interceptorRef
.ComponentKey
);
56 // This shoul be virtually impossible to happen
58 throw new ApplicationException("The interceptor could not be resolved");
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",
76 throw new ApplicationException(message
);
83 protected void SetOnBehalfAware(IOnBehalfAware onBehalfAware
, ComponentModel target
)
85 if (onBehalfAware
!= null)
87 onBehalfAware
.SetInterceptedComponentModel(target
);