1
|
|
#region Copyright
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
#endregion
|
18
|
|
|
19
|
|
using System;
|
20
|
|
using System.Collections;
|
21
|
|
using System.Reflection;
|
22
|
|
using System.Runtime.Remoting;
|
23
|
|
using Seasar.Extension.Component;
|
24
|
|
using Seasar.Framework.Aop;
|
25
|
|
using Seasar.Framework.Aop.Impl;
|
26
|
|
using Seasar.Framework.Aop.Proxy;
|
27
|
|
using Seasar.Framework.Beans;
|
28
|
|
using Seasar.Framework.Container;
|
29
|
|
using Seasar.Framework.Util;
|
30
|
|
|
31
|
|
namespace Seasar.Extension.Component.Impl
|
32
|
|
{
|
33
|
|
public class ComponentInvoker : IComponentInvoker
|
34
|
|
{
|
35
|
|
private IS2Container container;
|
36
|
|
|
37
|
0
|
public IS2Container Container
|
38
|
|
{
|
39
|
|
set { container = value; }
|
40
|
|
}
|
41
|
|
|
42
|
0
|
public object Invoke(string componentName, string methodName, object[] args)
|
43
|
|
{
|
44
|
|
object component = container.GetComponent(componentName);
|
45
|
|
Type type = null;
|
46
|
|
try
|
47
|
|
{
|
48
|
|
if ( RemotingServices.IsTransparentProxy(component) )
|
49
|
|
{
|
50
|
|
AopProxy aopProxy = RemotingServices.GetRealProxy(component) as AopProxy;
|
51
|
|
type = aopProxy.TargetType;
|
52
|
|
}
|
53
|
|
else
|
54
|
|
{
|
55
|
|
type = component.GetType();
|
56
|
|
}
|
57
|
|
|
58
|
|
MethodInfo methodInfo = type.GetMethod(methodName);
|
59
|
|
|
60
|
|
if (methodInfo == null)
|
61
|
|
throw new MissingMethodException();
|
62
|
|
|
63
|
|
int ParametersSize = methodInfo.GetParameters().Length;
|
64
|
|
|
65
|
|
if ( (ParametersSize > 0 && args == null) ||
|
66
|
|
(args != null && ParametersSize != args.Length) )
|
67
|
|
throw new IllegalMethodRuntimeException(type, methodName, null);
|
68
|
|
|
69
|
|
if (type.IsMarshalByRef)
|
70
|
|
{
|
71
|
|
return MethodUtil.Invoke(methodInfo, component, args);
|
72
|
|
}
|
73
|
|
else
|
74
|
|
{
|
75
|
|
IComponentDef componentDef = container.GetComponentDef(componentName);
|
76
|
|
return InvokeNonMarshalByRefObject(component,
|
77
|
|
GetAspects(componentDef), methodInfo, args);
|
78
|
|
}
|
79
|
|
}
|
80
|
|
|
81
|
|
catch(MissingMethodException ex)
|
82
|
|
{
|
83
|
|
ex.ToString();
|
84
|
|
throw new MethodNotFoundRuntimeException(type, methodName, args);
|
85
|
|
}
|
86
|
|
catch(Exception ex)
|
87
|
|
{
|
88
|
|
throw new IllegalMethodRuntimeException(type, methodName, ex);
|
89
|
|
}
|
90
|
|
}
|
91
|
|
|
92
|
0
|
private object InvokeNonMarshalByRefObject(
|
93
|
|
object target, IAspect[] aspects, MethodBase method, object[] args)
|
94
|
|
{
|
95
|
|
|
96
|
|
ArrayList interceptorList = new ArrayList();
|
97
|
|
|
98
|
|
if(aspects != null)
|
99
|
|
{
|
100
|
|
|
101
|
|
foreach(IAspect aspect in aspects)
|
102
|
|
{
|
103
|
|
IPointcut pointcut = aspect.Pointcut;
|
104
|
|
|
105
|
|
if(pointcut == null || pointcut.IsApplied(method))
|
106
|
|
{
|
107
|
|
|
108
|
|
interceptorList.Add(aspect.MethodInterceptor);
|
109
|
|
}
|
110
|
|
}
|
111
|
|
}
|
112
|
|
|
113
|
|
object ret = null;
|
114
|
|
if(interceptorList.Count == 0)
|
115
|
|
{
|
116
|
|
|
117
|
|
ret = method.Invoke(target, args);
|
118
|
|
}
|
119
|
|
else
|
120
|
|
{
|
121
|
|
|
122
|
|
IMethodInterceptor[] interceptors = (IMethodInterceptor[])
|
123
|
|
interceptorList.ToArray(typeof(IMethodInterceptor));
|
124
|
|
|
125
|
|
IMethodInvocation invocation = new MethodInvocationImpl(target,
|
126
|
|
method, args, interceptors, new Hashtable());
|
127
|
|
|
128
|
|
ret = interceptors[0].Invoke(invocation);
|
129
|
|
|
130
|
|
}
|
131
|
|
return ret;
|
132
|
|
}
|
133
|
|
|
134
|
0
|
private static IAspect[] GetAspects(IComponentDef componentDef)
|
135
|
|
{
|
136
|
|
int size = componentDef.AspectDefSize;
|
137
|
|
IAspect[] aspects = new IAspect[size];
|
138
|
|
for(int i = 0; i < size; ++i)
|
139
|
|
{
|
140
|
|
aspects[i] = componentDef.GetAspectDef(i).Aspect;
|
141
|
|
}
|
142
|
|
return aspects;
|
143
|
|
}
|
144
|
|
}
|
145
|
|
}
|
146
|
|
|