forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerClassMetaInfo.java
More file actions
544 lines (473 loc) · 17.4 KB
/
ControllerClassMetaInfo.java
File metadata and controls
544 lines (473 loc) · 17.4 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package act.controller.meta;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* 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.
* #L%
*/
import act.Act;
import act.app.App;
import act.app.AppClassLoader;
import act.asm.Type;
import act.controller.annotation.UrlContext;
import act.handler.builtin.controller.ControllerAction;
import act.handler.builtin.controller.Handler;
import act.plugin.ControllerPlugin;
import act.util.ClassInfoRepository;
import act.util.ClassNode;
import act.util.DestroyableBase;
import org.osgl.http.H;
import org.osgl.mvc.annotation.*;
import org.osgl.util.C;
import org.osgl.util.S;
import javax.enterprise.context.ApplicationScoped;
import java.lang.annotation.Annotation;
import java.util.*;
import static act.Destroyable.Util.destroyAll;
/**
* Stores all class level information to support generating of
* {@link ControllerAction request dispatcher}
* and {@link Handler interceptors}
*/
@ApplicationScoped
public final class ControllerClassMetaInfo extends DestroyableBase {
private Type type;
private Type superType;
private boolean isAbstract = false;
private String ctxField = null;
private boolean ctxFieldIsPrivate = true;
private Set<String> withList = C.newSet();
private List<ActionMethodMetaInfo> actions = new ArrayList<>();
// actionLookup index action method by method name
private Map<String, ActionMethodMetaInfo> actionLookup = null;
// handlerLookup index handler method by method name
// handler could by action or any kind of interceptors
private Map<String, HandlerMethodMetaInfo> handlerLookup = null;
GroupInterceptorMetaInfo interceptors = new GroupInterceptorMetaInfo();
private ControllerClassMetaInfo parent;
private boolean isController;
private boolean possibleController;
private String urlContext;
private String templateContext;
public ControllerClassMetaInfo className(String name) {
this.type = Type.getObjectType(name);
return this;
}
@Override
protected void releaseResources() {
withList.clear();
destroyAll(actions, ApplicationScoped.class);
actions.clear();
if (null != actionLookup) {
destroyAll(actionLookup.values(), ApplicationScoped.class);
actionLookup.clear();
}
if (null != handlerLookup) {
destroyAll(handlerLookup.values(), ApplicationScoped.class);
handlerLookup.clear();
}
interceptors.destroy();
if (null != parent) parent.destroy();
super.releaseResources();
}
public String className() {
return type.getClassName();
}
public String internalName() {
return type.getInternalName();
}
public Type type() {
return type;
}
public ControllerClassMetaInfo superType(Type type) {
superType = type;
return this;
}
public Type superType() {
return superType;
}
public List<String> withList() {
return C.list(withList);
}
public ControllerClassMetaInfo setAbstract() {
isAbstract = true;
return this;
}
public boolean isAbstract() {
return isAbstract;
}
public boolean isController() {
return isController;
}
public ControllerClassMetaInfo isController(boolean b) {
isController = b;
return this;
}
public boolean possibleController() {
return possibleController;
}
public ControllerClassMetaInfo possibleController(boolean b) {
possibleController = b;
return this;
}
boolean isMyAncestor(ControllerClassMetaInfo clsInfo) {
ControllerClassMetaInfo parentInfo = parent(true);
if (null == parentInfo) {
return false;
}
if (parentInfo.equals(clsInfo)) {
return true;
}
return parentInfo.isMyAncestor(clsInfo);
}
public ControllerClassMetaInfo parent(ControllerClassMetaInfo parentInfo) {
parent = parentInfo;
return this;
}
public ControllerClassMetaInfo parent() {
return parent;
}
public ControllerClassMetaInfo parent(boolean checkClassInfoRepo) {
if (null != parent) {
return parent;
}
if (!checkClassInfoRepo) {
return null;
}
AppClassLoader classLoader = Act.app().classLoader();
ClassInfoRepository repo = classLoader.classInfoRepository();
ClassNode parentNode = repo.node(superType.getClassName());
while(null != parentNode) {
parentNode = parentNode.parent();
if (null != parentNode) {
ControllerClassMetaInfo parentInfo = classLoader.controllerClassMetaInfo(parentNode.name());
if (null != parentInfo) {
return parentInfo;
}
} else {
return null;
}
}
return null;
}
public ControllerClassMetaInfo ctxField(String fieldName, boolean isPrivate) {
ctxField = fieldName;
ctxFieldIsPrivate = isPrivate;
return this;
}
public String nonPrivateCtxField() {
if (null != ctxField) {
return ctxFieldIsPrivate ? null : ctxField;
}
return null == parent ? null : parent.nonPrivateCtxField();
}
public String ctxField() {
if (null != ctxField) {
return ctxField;
}
if (null != parent) {
return parent.nonPrivateCtxField();
}
return null;
}
public boolean hasCtxField() {
return null != ctxField;
}
public boolean ctxFieldIsPrivate() {
return ctxFieldIsPrivate;
}
public ControllerClassMetaInfo addWith(String... classes) {
int len = classes.length;
if (len > 0) {
for (int i = 0; i < len; ++i) {
_addWith(classes[i]);
}
}
return this;
}
public ControllerClassMetaInfo addBefore(InterceptorMethodMetaInfo before) {
interceptors.addBefore(before);
return this;
}
public ControllerClassMetaInfo addAfter(InterceptorMethodMetaInfo after) {
interceptors.addAfter(after);
return this;
}
public ControllerClassMetaInfo addCatch(CatchMethodMetaInfo cat) {
interceptors.addCatch(cat);
return this;
}
public ControllerClassMetaInfo addFinally(InterceptorMethodMetaInfo after) {
interceptors.addFinally(after);
return this;
}
public ControllerClassMetaInfo addInterceptor(InterceptorMethodMetaInfo info, Class<? extends Annotation> type) {
interceptors.add(info, type);
return this;
}
public ControllerClassMetaInfo addAction(ActionMethodMetaInfo info) {
actions.add(info);
return this;
}
public ActionMethodMetaInfo action(String name) {
if (null == actionLookup) {
for (ActionMethodMetaInfo act : actions) {
if (S.eq(name, act.name())) {
return act;
}
}
return null;
}
return actionLookup.get(name);
}
public HandlerMethodMetaInfo handler(String name) {
HandlerMethodMetaInfo info;
if (null == handlerLookup) {
info = action(name);
if (null != info) {
return info;
}
return interceptors.find(name, className());
}
return handlerLookup.get(name);
}
<T extends InterceptorMethodMetaInfo> List<T> convertDerived(List<T> interceptors) {
C.List<T> list = C.newSizedList(interceptors.size());
for (InterceptorMethodMetaInfo derived : interceptors) {
list.add((T)derived.extended(this));
}
return list;
}
public GroupInterceptorMetaInfo interceptors() {
return interceptors;
}
public List<InterceptorMethodMetaInfo> beforeInterceptors() {
return interceptors.beforeList();
}
public List<InterceptorMethodMetaInfo> afterInterceptors() {
return interceptors.afterList();
}
public List<CatchMethodMetaInfo> exceptionInterceptors() {
return interceptors.catchList();
}
public List<InterceptorMethodMetaInfo> finallyInterceptors() {
return interceptors.finallyList();
}
public ControllerClassMetaInfo merge(ControllerClassMetaInfoManager infoBase, App app) {
mergeFromWithList(infoBase, app);
mergeIntoActionList(infoBase, app);
buildActionLookup();
buildHandlerLookup();
return this;
}
public String templateContext() {
if (null != parent) {
if (S.notBlank(templateContext) && templateContext.length() > 1 && templateContext.startsWith("/")) {
return templateContext;
}
String parentContextPath = parent.templateContext();
if (null == templateContext) {
return parentContextPath;
}
if (null == parentContextPath) {
return templateContext;
}
S.Buffer sb = S.newBuffer(parentContextPath);
if (parentContextPath.endsWith("/")) {
sb.deleteCharAt(sb.length() - 1);
}
if (!templateContext.startsWith("/")) {
sb.append("/");
}
sb.append(templateContext);
return sb.toString();
}
return templateContext;
}
public String urlContext() {
if (null != parent) {
if (S.notBlank(urlContext) && urlContext.length() > 1 && urlContext.startsWith("/")) {
return urlContext;
}
String parentContextPath = parent.urlContext();
if (null == urlContext) {
return parentContextPath;
}
if (null == parentContextPath) {
return urlContext;
}
S.Buffer sb = S.newBuffer(parentContextPath);
if (parentContextPath.endsWith("/")) {
sb.deleteCharAt(sb.length() - 1);
}
if (!urlContext.startsWith("/")) {
sb.append("/");
}
sb.append(urlContext);
return sb.toString();
}
return urlContext;
}
public ControllerClassMetaInfo templateContext(String path) {
if (S.blank(path)) {
templateContext = "/";
} else {
templateContext = path;
}
return this;
}
public ControllerClassMetaInfo urlContext(String path) {
if (S.blank(path)) {
urlContext = "/";
} else {
urlContext = path;
}
return this;
}
private void _addWith(String clsName) {
withList.add(Type.getType(clsName).getClassName());
}
private void getAllWithList(final Set<String> withList, final ControllerClassMetaInfoManager infoBase) {
withList.addAll(this.withList);
if (null != superType) {
final String superClass = superType.getClassName();
App app = App.instance();
ClassInfoRepository repo = app.classLoader().classInfoRepository();
ControllerClassMetaInfo info = infoBase.controllerMetaInfo(superClass);
String curSuperClass = superClass;
while (null == info && !"java.lang.Object".equals(curSuperClass)) {
ClassNode node = repo.node(curSuperClass);
if (null != node) {
node = node.parent();
}
if (null == node) {
break;
}
curSuperClass = node.name();
info = infoBase.controllerMetaInfo(curSuperClass);
}
if (null != info) {
withList.add(superClass);
}
}
}
private void mergeFromWithList(final ControllerClassMetaInfoManager infoBase, final App app) {
C.Set<String> withClasses = C.newSet();
getAllWithList(withClasses, infoBase);
final ControllerClassMetaInfo me = this;
ClassInfoRepository repo = app.classLoader().classInfoRepository();
for (final String withClass : withClasses) {
String curWithClass = withClass;
ControllerClassMetaInfo withClassInfo = infoBase.controllerMetaInfo(curWithClass);
while (null == withClassInfo && !"java.lang.Object".equals(curWithClass)) {
ClassNode node = repo.node(curWithClass);
if (null != node) {
node = node.parent();
}
if (null == node) {
break;
}
curWithClass = node.name();
withClassInfo = infoBase.controllerMetaInfo(curWithClass);
}
if (null != withClassInfo) {
withClassInfo.merge(infoBase, app);
if (isMyAncestor(withClassInfo)) {
interceptors.mergeFrom(withClassInfo.interceptors, me);
} else {
interceptors.mergeFrom(withClassInfo.interceptors);
}
}
}
}
private void mergeIntoActionList(ControllerClassMetaInfoManager infoBase, App app) {
for (ActionMethodMetaInfo info : actions) {
info.merge(infoBase, app);
info.mergeFromClassInterceptors(interceptors);
}
}
private void buildActionLookup() {
Map<String, ActionMethodMetaInfo> lookup = new HashMap<>();
for (ActionMethodMetaInfo act : actions) {
lookup.put(act.name(), act);
}
actionLookup = lookup;
}
private void buildHandlerLookup() {
Map<String, HandlerMethodMetaInfo> lookup = new HashMap<>();
lookup.putAll(actionLookup);
for (InterceptorMethodMetaInfo info : beforeInterceptors()) {
lookup.put(info.name(), info);
}
for (InterceptorMethodMetaInfo info : afterInterceptors()) {
lookup.put(info.name(), info);
}
for (InterceptorMethodMetaInfo info : exceptionInterceptors()) {
lookup.put(info.name(), info);
}
for (InterceptorMethodMetaInfo info : finallyInterceptors()) {
lookup.put(info.name(), info);
}
handlerLookup = lookup;
}
public static void registerMethodLookups(Map<Class<? extends Annotation>, H.Method> annotationMethodLookup, boolean noDefaultPath) {
METHOD_LOOKUP.putAll(annotationMethodLookup);
if (noDefaultPath) {
NO_DEF_PATH_ACTIONS.addAll(annotationMethodLookup.keySet());
}
}
public static void registerUrlContextAnnotation(ControllerPlugin.PathAnnotationSpec pathAnnotationInfo) {
URL_CONTEXT_ANNOTATIONS.put(pathAnnotationInfo.annoType(), pathAnnotationInfo);
}
private static final C.Set<Class<? extends Annotation>> INTERCEPTOR_ANNOTATION_TYPES = C.set(
Before.class, After.class, Catch.class, Finally.class);
public static final C.Set<H.Method> ACTION_METHODS = C.set(H.Method.GET, H.Method.POST, H.Method.PUT, H.Method.DELETE);
private static final Map<Class<? extends Annotation>, H.Method> METHOD_LOOKUP = C.newMap(
GetAction.class, H.Method.GET,
PostAction.class, H.Method.POST,
PutAction.class, H.Method.PUT,
DeleteAction.class, H.Method.DELETE,
PatchAction.class, H.Method.PATCH,
WsAction.class, H.Method.GET
);
private static final Set<Class<? extends Annotation>> NO_DEF_PATH_ACTIONS = new HashSet<>();
public static boolean noDefPath(Class<? extends Annotation> actionAnno) {
return NO_DEF_PATH_ACTIONS.contains(actionAnno);
}
// map annotype to support_absolute_path
private static final Map<Class<? extends Annotation>, ControllerPlugin.PathAnnotationSpec> URL_CONTEXT_ANNOTATIONS = new HashMap<>();
public static boolean isActionAnnotation(Class<? extends Annotation> type) {
return METHOD_LOOKUP.containsKey(type) || Action.class == type;
}
public static boolean isUrlContextAnnotation(Class<? extends Annotation> anno) {
return UrlContext.class == anno || URL_CONTEXT_ANNOTATIONS.containsKey(anno);
}
public static boolean isUrlContextAnnotationSupportAbsolutePath(Class<? extends Annotation> anno) {
return UrlContext.class == anno || URL_CONTEXT_ANNOTATIONS.get(anno).supportAbsolutePath();
}
public static boolean isUrlContextAnnotationSupportInheritance(Class<? extends Annotation> anno) {
return UrlContext.class == anno || URL_CONTEXT_ANNOTATIONS.get(anno).supportInheritance();
}
public static H.Method lookupHttpMethod(Class annotationClass) {
return METHOD_LOOKUP.get(annotationClass);
}
public static boolean isActionUtilAnnotation(Class<? extends Annotation> type) {
return ActionUtil.class == type;
}
public static boolean isInterceptorAnnotation(Class<? extends Annotation> type) {
return INTERCEPTOR_ANNOTATION_TYPES.contains(type);
}
}