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.Reflection;
|
21
|
|
using System.Collections;
|
22
|
|
using System.Runtime.Remoting.Proxies;
|
23
|
|
using System.Runtime.Remoting.Messaging;
|
24
|
|
using Seasar.Framework.Aop.Impl;
|
25
|
|
using Seasar.Framework.Util;
|
26
|
|
|
27
|
|
namespace Seasar.Framework.Aop.Proxy
|
28
|
|
{
|
29
|
|
|
30
|
|
|
31
|
|
|
32
|
|
|
33
|
|
|
34
|
|
|
35
|
|
[Serializable]
|
36
|
|
public sealed class AopProxy : RealProxy
|
37
|
|
{
|
38
|
|
|
39
|
|
|
40
|
|
|
41
|
|
private object target_;
|
42
|
|
|
43
|
|
|
44
|
|
|
45
|
|
|
46
|
|
private IAspect[] aspects_;
|
47
|
|
|
48
|
|
|
49
|
|
|
50
|
|
|
51
|
|
private Type type_;
|
52
|
|
|
53
|
|
|
54
|
|
|
55
|
|
|
56
|
|
private Hashtable parameters_;
|
57
|
|
|
58
|
|
public Type TargetType
|
59
|
|
{
|
60
|
2
|
get { return type_; }
|
61
|
|
}
|
62
|
|
|
63
|
|
|
64
|
|
|
65
|
|
|
66
|
|
|
67
|
|
|
68
|
|
|
69
|
52
|
public AopProxy(Type type,IAspect[] aspects,Hashtable parameters, object target) : base(type)
|
70
|
|
{
|
71
|
52
|
type_ = type;
|
72
|
52
|
target_ = target;
|
73
|
52
|
aspects_ = aspects;
|
74
|
52
|
parameters_ = parameters;
|
75
|
|
}
|
76
|
|
|
77
|
|
|
78
|
|
|
79
|
|
|
80
|
|
|
81
|
|
|
82
|
|
|
83
|
7
|
public AopProxy(Type type,IAspect[] aspects,Hashtable parameters)
|
84
|
|
: this(type,aspects,parameters,null)
|
85
|
|
{
|
86
|
|
}
|
87
|
|
|
88
|
|
|
89
|
|
|
90
|
|
|
91
|
|
|
92
|
|
|
93
|
7
|
public AopProxy(Type type,IAspect[] aspects)
|
94
|
|
: this(type,aspects,null)
|
95
|
|
{
|
96
|
|
}
|
97
|
|
|
98
|
|
|
99
|
|
|
100
|
|
|
101
|
|
|
102
|
0
|
public AopProxy(Type type)
|
103
|
|
: this(type,null)
|
104
|
|
{
|
105
|
|
}
|
106
|
|
|
107
|
|
|
108
|
|
|
109
|
|
|
110
|
|
|
111
|
44
|
public object Create()
|
112
|
|
{
|
113
|
44
|
return this.GetTransparentProxy();
|
114
|
|
}
|
115
|
|
|
116
|
|
|
117
|
|
|
118
|
|
|
119
|
|
|
120
|
|
|
121
|
|
|
122
|
1
|
public object Create(Type[] argTypes,object[] args)
|
123
|
|
{
|
124
|
1
|
ConstructorInfo constructor = ClassUtil.GetConstructorInfo(type_,argTypes);
|
125
|
1
|
target_ = ConstructorUtil.NewInstance(constructor,args);
|
126
|
1
|
return this.GetTransparentProxy();
|
127
|
|
}
|
128
|
|
|
129
|
|
|
130
|
|
|
131
|
|
|
132
|
|
|
133
|
|
|
134
|
|
|
135
|
|
|
136
|
0
|
public object Create(Type[] argTypes,object[] args,Type targetType)
|
137
|
|
{
|
138
|
|
ConstructorInfo constructor = ClassUtil.GetConstructorInfo(targetType,argTypes);
|
139
|
|
target_ = ConstructorUtil.NewInstance(constructor,args);
|
140
|
|
return this.GetTransparentProxy();
|
141
|
|
}
|
142
|
|
|
143
|
|
#region RealProxy メンバ
|
144
|
|
|
145
|
|
|
146
|
|
|
147
|
|
|
148
|
|
|
149
|
|
|
150
|
|
|
151
|
65
|
public override IMessage Invoke(IMessage msg)
|
152
|
|
{
|
153
|
65
|
if(target_ == null)
|
154
|
|
{
|
155
|
0
|
if(!type_.IsInterface) target_ = Activator.CreateInstance(type_);
|
156
|
6
|
if(target_==null) target_ = new object();
|
157
|
|
}
|
158
|
|
|
159
|
65
|
IMethodMessage methodMessage = msg as IMethodMessage;
|
160
|
65
|
MethodBase method = methodMessage.MethodBase;
|
161
|
|
|
162
|
65
|
ArrayList interceptorList = new ArrayList();
|
163
|
|
|
164
|
65
|
if(aspects_ != null)
|
165
|
|
{
|
166
|
|
|
167
|
65
|
foreach(IAspect aspect in aspects_)
|
168
|
|
{
|
169
|
69
|
IPointcut pointcut = aspect.Pointcut;
|
170
|
|
|
171
|
69
|
if(pointcut == null || pointcut.IsApplied(method))
|
172
|
|
{
|
173
|
|
|
174
|
63
|
interceptorList.Add(aspect.MethodInterceptor);
|
175
|
|
}
|
176
|
|
}
|
177
|
|
}
|
178
|
|
|
179
|
65
|
Object ret = null;
|
180
|
65
|
ArrayList outParameters = new ArrayList();
|
181
|
65
|
ParameterInfo[] pis = method.GetParameters();
|
182
|
|
|
183
|
65
|
if(interceptorList.Count == 0)
|
184
|
|
{
|
185
|
6
|
object[] args = methodMessage.Args;
|
186
|
|
|
187
|
6
|
ret = method.Invoke(target_, args);
|
188
|
7
|
for(int i = 0; i < pis.Length; i++)
|
189
|
|
{
|
190
|
1
|
if (pis[i].ParameterType.IsByRef)
|
191
|
|
{
|
192
|
0
|
outParameters.Add(args[i]);
|
193
|
|
}
|
194
|
|
}
|
195
|
|
}
|
196
|
|
else
|
197
|
|
{
|
198
|
|
|
199
|
59
|
IMethodInterceptor[] interceptors = (IMethodInterceptor[])
|
200
|
|
interceptorList.ToArray(typeof(IMethodInterceptor));
|
201
|
59
|
IMethodInvocation invocation = new MethodInvocationImpl(target_,
|
202
|
|
method,methodMessage.Args,interceptors,parameters_);
|
203
|
59
|
ret = interceptors[0].Invoke(invocation);
|
204
|
|
|
205
|
51
|
for(int i = 0; i < pis.Length; i++)
|
206
|
|
{
|
207
|
2
|
if (pis[i].ParameterType.IsByRef)
|
208
|
|
{
|
209
|
0
|
outParameters.Add(invocation.Arguments[i]);
|
210
|
|
}
|
211
|
|
}
|
212
|
|
}
|
213
|
|
|
214
|
55
|
if (outParameters.Count == 0)
|
215
|
|
{
|
216
|
55
|
return new ReturnMessage(ret, null, 0,
|
217
|
|
methodMessage.LogicalCallContext, (IMethodCallMessage)msg);
|
218
|
|
}
|
219
|
|
else
|
220
|
|
{
|
221
|
0
|
return new ReturnMessage(ret, outParameters.ToArray(), outParameters.Count,
|
222
|
|
methodMessage.LogicalCallContext, (IMethodCallMessage)msg);
|
223
|
|
}
|
224
|
|
}
|
225
|
|
|
226
|
|
#endregion
|
227
|
|
|
228
|
|
}
|
229
|
|
}
|
230
|
|
|