1
|
|
#region using directives
|
2
|
|
|
3
|
|
using System;
|
4
|
|
using System.Diagnostics;
|
5
|
|
using System.Text;
|
6
|
|
using System.Reflection;
|
7
|
|
using System.Collections;
|
8
|
|
|
9
|
|
using Seasar.Framework.Aop.Impl;
|
10
|
|
using Seasar.Framework.Util;
|
11
|
|
using Seasar.Framework.Aop.Interceptors;
|
12
|
|
using Seasar.Framework.Container.Util;
|
13
|
|
|
14
|
|
using Castle.DynamicProxy;
|
15
|
|
|
16
|
|
#endregion
|
17
|
|
|
18
|
|
namespace Seasar.Framework.Aop.Proxy
|
19
|
|
{
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
|
26
|
|
|
27
|
|
[Serializable]
|
28
|
|
public class DynamicAopProxy : IInterceptor
|
29
|
|
{
|
30
|
|
#region fields
|
31
|
|
|
32
|
|
private ProxyGenerator generator;
|
33
|
|
|
34
|
|
private object target;
|
35
|
|
private IAspect[] aspects;
|
36
|
|
private Type type;
|
37
|
|
private Hashtable parameters;
|
38
|
|
|
39
|
|
#endregion
|
40
|
|
#region constructors
|
41
|
|
|
42
|
|
|
43
|
|
|
44
|
|
|
45
|
|
|
46
|
0
|
public DynamicAopProxy(Type type)
|
47
|
|
: this(type, null)
|
48
|
|
{
|
49
|
|
}
|
50
|
|
|
51
|
|
|
52
|
|
|
53
|
|
|
54
|
|
|
55
|
|
|
56
|
0
|
public DynamicAopProxy(Type type, IAspect[] aspects)
|
57
|
|
: this(type, aspects, null)
|
58
|
|
{
|
59
|
|
}
|
60
|
|
|
61
|
|
|
62
|
|
|
63
|
|
|
64
|
|
|
65
|
|
|
66
|
|
|
67
|
0
|
public DynamicAopProxy(Type type, IAspect[] aspects, Hashtable parameters)
|
68
|
|
: this(type, aspects, parameters, null)
|
69
|
|
{
|
70
|
|
}
|
71
|
|
|
72
|
|
|
73
|
|
|
74
|
|
|
75
|
|
|
76
|
|
|
77
|
|
|
78
|
|
|
79
|
0
|
public DynamicAopProxy(Type type, IAspect[] aspects, Hashtable parameters, object target)
|
80
|
|
{
|
81
|
|
this.type = type;
|
82
|
|
this.target = target;
|
83
|
|
if (this.target == null) this.target = new object();
|
84
|
|
this.aspects = aspects;
|
85
|
|
this.parameters = parameters;
|
86
|
|
this.generator = new ProxyGenerator();
|
87
|
|
}
|
88
|
|
|
89
|
|
#endregion
|
90
|
|
#region public method
|
91
|
|
|
92
|
|
|
93
|
|
|
94
|
|
|
95
|
0
|
public object Create()
|
96
|
|
{
|
97
|
|
object result = null;
|
98
|
|
if (this.type.IsInterface)
|
99
|
|
{
|
100
|
|
result = this.generator.CreateProxy(this.type, this, this.target);
|
101
|
|
}
|
102
|
|
else
|
103
|
|
{
|
104
|
|
result = this.generator.CreateClassProxy(this.type, this, new object[] { });
|
105
|
|
|
106
|
|
}
|
107
|
|
return result;
|
108
|
|
}
|
109
|
|
|
110
|
|
|
111
|
|
|
112
|
|
|
113
|
|
|
114
|
|
|
115
|
0
|
public object Create(Type[] argTypes, object[] args)
|
116
|
|
{
|
117
|
|
object result = null;
|
118
|
|
if (this.type.IsInterface)
|
119
|
|
{
|
120
|
|
result = this.generator.CreateProxy(this.type, this, args);
|
121
|
|
}
|
122
|
|
else
|
123
|
|
{
|
124
|
|
result = this.generator.CreateClassProxy(this.type, this, args);
|
125
|
|
}
|
126
|
|
return result;
|
127
|
|
}
|
128
|
|
|
129
|
|
|
130
|
|
|
131
|
|
|
132
|
|
|
133
|
|
|
134
|
0
|
public object Create(Type[] argTypes, object[] args, Type targetType)
|
135
|
|
{
|
136
|
|
object result = null;
|
137
|
|
if (this.type.IsInterface)
|
138
|
|
{
|
139
|
|
result = this.generator.CreateProxy(targetType, this, args);
|
140
|
|
}
|
141
|
|
else
|
142
|
|
{
|
143
|
|
result = this.generator.CreateClassProxy(targetType, this, args);
|
144
|
|
}
|
145
|
|
return result;
|
146
|
|
}
|
147
|
|
|
148
|
|
#endregion
|
149
|
|
#region IInterceptor member
|
150
|
|
|
151
|
0
|
public object Intercept(IInvocation invocation, params object[] args)
|
152
|
|
{
|
153
|
|
ArrayList interceptorList = new ArrayList();
|
154
|
|
object ret = null;
|
155
|
|
|
156
|
|
if (aspects != null)
|
157
|
|
{
|
158
|
|
foreach (IAspect aspect in aspects)
|
159
|
|
{
|
160
|
|
IPointcut pointcut = aspect.Pointcut;
|
161
|
|
if (pointcut == null || pointcut.IsApplied(invocation.Method))
|
162
|
|
{
|
163
|
|
interceptorList.Add(aspect.MethodInterceptor);
|
164
|
|
}
|
165
|
|
}
|
166
|
|
}
|
167
|
|
if (interceptorList.Count == 0)
|
168
|
|
{
|
169
|
|
ret = invocation.Proceed(args);
|
170
|
|
}
|
171
|
|
else
|
172
|
|
{
|
173
|
|
IMethodInterceptor[] interceptors = (IMethodInterceptor[])
|
174
|
|
interceptorList.ToArray(typeof(IMethodInterceptor));
|
175
|
|
IMethodInvocation mehotdInvocation =
|
176
|
|
new DynamicProxyMethodInvocation(
|
177
|
|
invocation.InvocationTarget, this.type, invocation, args, interceptors, parameters);
|
178
|
|
ret = interceptors[0].Invoke(mehotdInvocation);
|
179
|
|
}
|
180
|
|
return ret;
|
181
|
|
}
|
182
|
|
|
183
|
|
#endregion
|
184
|
|
}
|
185
|
|
}
|
186
|
|
|