-
Notifications
You must be signed in to change notification settings - Fork 5
/
aop.cfc
312 lines (240 loc) · 7.79 KB
/
aop.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
component extends="framework.ioc" {
variables._fw1_version = "4.3.0-SNAPSHOT";
variables._aop1_version = variables._fw1_version;
/*
Copyright (c) 2013-2018, Mark Drew, Sean Corfield, Daniel Budde
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Internal cache of interceptor definitions.
variables.interceptorCache = {regex = [], name = {}, type = []};
// -------------- //
// PUBLIC METHODS //
// -------------- //
/** Constructor. */
public any function init(any folders, struct config = {})
{
super.init(argumentCollection = arguments);
if (structKeyExists(arguments.config, "interceptors") && isArray(arguments.config.interceptors) && arrayLen(arguments.config.interceptors))
{
loadInterceptors(arguments.config.interceptors);
}
}
/** Adds an interceptor definition to the definition cache. */
public any function intercept(string beanName, string interceptorName, string methods = "")
{
var interceptDefinition =
{
name = arguments.interceptorName,
methods = arguments.methods
};
arguments.beanName = trim(arguments.beanName);
// Determine if this is a name match or regex match.
if (len(arguments.beanName) && left(arguments.beanName, 1) == "/" && right(arguments.beanName, 1) == "/")
{
// Store regex without the forward slashes.
interceptDefinition.regex = mid(arguments.beanName, 2, len(arguments.beanName) - 2);
arrayAppend(variables.interceptorCache.regex, interceptDefinition);
}
else
{
if (!structKeyExists(variables.interceptorCache.name, arguments.beanName))
{
variables.interceptorCache.name[arguments.beanName] = [];
}
arrayAppend(variables.interceptorCache.name[arguments.beanName], interceptDefinition);
}
return this;
}
/** Adds an interceptor definition to the definition cache. */
public any function interceptByType(string type, string interceptorName, string methods = "")
{
var interceptDefinition =
{
type = arguments.type,
name = arguments.interceptorName,
methods = arguments.methods
};
arrayAppend(variables.interceptorCache.type, interceptDefinition);
}
// --------------- //
// PRIVATE METHODS //
// --------------- //
/** Hook point to wrap bean with proxy. */
private any function construct(string dottedPath)
{
var bean = super.construct(arguments.dottedPath);
var beanProxy = "";
// if it doesn't have a dotted path for us to create a new instance
// or it has no interceptors, we have to leave it alone
if (!hasInterceptors(arguments.dottedPath))
{
return bean;
}
// Create and return a proxy wrapping the bean.
beanProxy = new framework.beanProxy(bean, getInterceptorsForBean(arguments.dottedPath), variables.config);
return beanProxy;
}
/** Gets the associated interceptor definitions for a specific bean. */
private array function getInterceptorsForBean(string dottedPath)
{
// build the interceptor array:
var beanName = listLast(arguments.dottedPath, ".");
var beanNames = getAliases(beanName);
var beanTypes = "";
var interceptDefinition = "";
var interceptedBeanName = "";
var interceptors = [];
arrayPrepend(beanNames, beanName);
// Grab all name based interceptors that match.
for (interceptedBeanName in beanNames)
{
// Match on name.
if (structKeyExists(variables.interceptorCache.name, interceptedBeanName))
{
for (interceptDefinition in variables.interceptorCache.name[interceptedBeanName])
{
arrayAppend(interceptors, {bean = getBean(interceptDefinition.name), methods = interceptDefinition.methods});
}
}
}
// Match on regex. Ensure we only attach each one time.
if (arrayLen(variables.interceptorCache.regex))
{
for (interceptDefinition in variables.interceptorCache.regex)
{
for (interceptedBeanName in beanNames)
{
if (reFindNoCase(interceptDefinition.regex, interceptedBeanName))
{
arrayAppend(interceptors, {bean = getBean(interceptDefinition.name), methods = interceptDefinition.methods});
break;
}
}
}
}
// Grab all type based interceptors that match.
if (arrayLen(variables.interceptorCache.type))
{
beanTypes = getBeanTypes(arguments.dottedPath);
for (interceptDefinition in variables.interceptorCache.type)
{
if (listFindNoCase(beanTypes, interceptDefinition.type))
{
arrayAppend(interceptors, {bean = getBean(interceptDefinition.name), methods = interceptDefinition.methods});
}
}
}
return interceptors;
}
/** Determines if the bean has interceptor definitions associated with it. */
private boolean function hasInterceptors(string dottedPath)
{
var interceptedBeanName = "";
var interceptorDefinition = {};
var beanName = listLast(arguments.dottedPath, ".");
var beanNames = getAliases(beanName);
var beanTypes = "";
arrayPrepend(beanNames, beanName);
for (interceptedBeanName in beanNames)
{
// Look for matches on name first.
if (structKeyExists(variables.interceptorCache.name, interceptedBeanName))
{
return true;
}
// Look for matches on regex.
if (arrayLen(variables.interceptorCache.regex))
{
for (interceptorDefinition in variables.interceptorCache.regex)
{
if (reFindNoCase(interceptorDefinition.regex, interceptedBeanName))
{
return true;
}
}
}
// Look for matches by bean type.
if (arrayLen(variables.interceptorCache.type))
{
beanTypes = getBeanTypes(arguments.dottedPath);
for (interceptorDefinition in variables.interceptorCache.type)
{
if (listFindNoCase(beanTypes, interceptorDefinition.type))
{
return true;
}
}
}
}
return false;
}
/** Finds all aliases for the given beanName. */
private array function getAliases(string beanName)
{
var aliases = [];
var beanData = "";
var key = "";
if (structKeyExists(variables.beanInfo, arguments.beanName))
{
beanData = variables.beanInfo[arguments.beanName];
for (key in variables.beanInfo)
{
// Same cfc dotted path, must be an alias.
if (
key != arguments.beanName &&
structKeyExists(variables.beanInfo[key], "cfc") &&
structKeyExists(variables.beanInfo[arguments.beanName], "cfc") &&
variables.beanInfo[key].cfc == variables.beanInfo[arguments.beanName].cfc)
{
arrayAppend(aliases, key);
}
}
}
return aliases;
}
/** Returns a list of bean types (both name and dotted path) for a given bean. */
private string function getBeanTypes(string dottedPath)
{
var beanTypes = "";
var metadata = getComponentMetadata(arguments.dottedPath);
while (!len(beanTypes) || structKeyExists(metadata, "extends"))
{
beanTypes = listAppend(beanTypes, listLast(metadata.name, "."));
beanTypes = listAppend(beanTypes, metadata.name);
if (structKeyExists(metadata, "extends"))
{
metadata = metadata.extends;
}
}
return beanTypes;
}
/** Loads an array of interceptor definitions into the interceptor definition cache. */
private void function loadInterceptors(array interceptors)
{
var interceptor = false;
for (interceptor in interceptors)
{
if (structKeyExists(interceptor, "beanName"))
{
intercept(argumentCollection = interceptor);
}
else
{
interceptByType(argumentCollection = interceptor);
}
}
}
private void function setupFrameworkDefaults()
{
super.setupFrameworkDefaults();
variables.config.version = variables._fw1_version;
}
}