forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActContext.java
More file actions
532 lines (448 loc) · 15.6 KB
/
ActContext.java
File metadata and controls
532 lines (448 loc) · 15.6 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
package act.util;
/*-
* #%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.Destroyable;
import act.act_messages;
import act.app.ActionContext;
import act.app.App;
import act.cli.CliContext;
import act.conf.AppConfig;
import act.i18n.I18n;
import act.mail.MailerContext;
import act.view.Template;
import org.osgl.$;
import org.osgl.http.H;
import org.osgl.mvc.util.ParamValueProvider;
import org.osgl.util.C;
import org.osgl.util.E;
import org.osgl.util.S;
import javax.enterprise.context.RequestScoped;
import javax.validation.ConstraintViolation;
import java.lang.reflect.Method;
import java.util.*;
public interface ActContext<CTX_TYPE extends ActContext> extends ParamValueProvider {
/**
* Used to store the {@link java.lang.reflect.Method} this context is trying
* to invoke right now. Note the method might be corresponding to the
* {@link #methodPath()} or not if the current method is an interceptor.
*/
String ATTR_CUR_METHOD = "__ctx_cur_method__";
App app();
AppConfig config();
CTX_TYPE accept(H.Format fmt);
H.Format accept();
CTX_TYPE locale(Locale locale);
Locale locale();
Locale locale(boolean required);
/**
* If {@link #templatePath(String) template path has been set before} then return
* the template path
*/
String templatePath();
/**
* Set path to template file
* @param path the path to template file
* @return this {@code AppContext}
*/
CTX_TYPE templatePath(String path);
/**
* Returns the template context
* @return template context
*/
String templateContext();
/**
* Set template context
* @param context the path to template context
* @return this {@code ActContext}
*/
CTX_TYPE templateContext(String context);
/**
* Check if the template path is implicit i.e. derived from {@link #methodPath()}
* @return `true` if template path is implicit; `false` otherwise
*/
boolean templatePathIsImplicit();
Template cachedTemplate();
CTX_TYPE cacheTemplate(Template template);
<T> T renderArg(String name);
/**
* Returns all render arguments
*/
Map<String, Object> renderArgs();
CTX_TYPE renderArg(String name, Object val);
CTX_TYPE addListener(Listener listener);
CTX_TYPE addDestroyable(Destroyable resource);
CTX_TYPE attribute(String name, Object attr);
<T> T attribute(String name);
Map<String, Object> attributes();
CTX_TYPE removeAttribute(String name);
CTX_TYPE addViolations(Map<String, ConstraintViolation> violations);
CTX_TYPE addViolation(String property, ConstraintViolation violation);
boolean hasViolation();
Map<String, ConstraintViolation> violations();
ConstraintViolation violation(String property);
/**
* Returns the user session with which this context is associated
*
* @return session id or null if no session associated with this context (e.g. mail context)
*/
String sessionId();
/**
* Returns data format pattern. Normally should be date time format
*
* @return the data format pattern
*/
String pattern();
/**
* Set data format pattern
*
* @param pattern
* the data format pattern
* @return
* this context instance
*/
CTX_TYPE pattern(String pattern);
String _act_i18n(String msgId, Object... args);
String i18n(boolean ignoreError, String msgId, Object... args);
String i18n(String msgId, Object ... args);
String i18n(Class<?> bundleSpec, String msgId, Object... args);
String i18n(boolean ignoreError, Class<?> bundleSpec, String msgId, Object... args);
String i18n(Enum<?> msgId);
String i18n(Class<?> bundleSpec, Enum<?> msgId);
Map<String, Object> i18n(Class<? extends Enum> enumClass);
Map<String, Object> i18n(Class<?> bundleSpec, Class<? extends Enum> enumClass);
Map<String, Object> i18n(Class<? extends Enum> enumClass, boolean outputProperties);
Map<String, Object> i18n(Class<?> bundleSpec, Class<? extends Enum> enumClass, boolean outputProperties);
String methodPath();
Method handlerMethod();
CTX_TYPE handlerMethod(Method method);
/**
* Returns a reusable {@link S.Buffer} instance
* @return an S.Buffer instance that can be reused
*/
S.Buffer strBuf();
interface Listener {
void onDestroy(ActContext context);
}
abstract class Base<CTX extends Base> extends DestroyableBase
implements ActContext<CTX> {
private App app;
private String templatePath;
private String templateContext;
private Template template;
private Map<String, Object> renderArgs;
private List<Listener> listenerList;
private List<Destroyable> destroyableList;
private Map<String, Object> attributes;
private Locale locale;
private int fieldOutputVarCount;
private S.Buffer strBuf;
private boolean noTemplateCache;
private SimpleProgressGauge progress = new SimpleProgressGauge();
private String jobId;
private Method handlerMethod;
private String pattern;
// (violation.propertyPath, violation)
private Map<String, ConstraintViolation> violations;
public Base(App app) {
E.NPE(app);
this.app = app;
renderArgs = new HashMap<>();
attributes = new HashMap<>();
listenerList = new ArrayList<>();
destroyableList = new ArrayList<>();
strBuf = S.newBuffer();
violations = new HashMap<>();
}
@Override
protected void releaseResources() {
for (Listener l : listenerList) {
try {
l.onDestroy(this);
} catch (Exception e) {
warn(e, "error calling listener onDestroy method");
}
}
Destroyable.Util.destroyAll(destroyableList, RequestScoped.class);
Destroyable.Util.tryDestroyAll(attributes.values(), RequestScoped.class);
this.attributes.clear();
this.renderArgs.clear();
this.template = null;
this.app = null;
this.template = null;
this.listenerList.clear();
this.destroyableList.clear();
this.violations.clear();
// note we can't destroy progress as it might still be used
// by background thread
//this.progress.destroy();
}
@Override
public App app() {
return app;
}
@Override
public AppConfig config() {
return app().config();
}
@Override
public Method handlerMethod() {
return handlerMethod;
}
@Override
public CTX handlerMethod(Method method) {
this.handlerMethod = method;
return me();
}
@Override
public String templatePath() {
String path = templatePath;
String context = templateContext;
if (S.notBlank(path)) {
return path.startsWith("/") || S.blank(context) ? path : S.pathConcat(context, '/', path);
} else {
if (S.blank(context)) {
return methodPath().replace('.', '/');
} else {
return S.pathConcat(context, '/', S.afterLast(methodPath(), "."));
}
}
}
@Override
public CTX templatePath(String templatePath) {
this.template = null;
this.templatePath = templatePath;
return me();
}
public String templateContext() {
return this.templateContext;
}
public CTX templateContext(String templateContext) {
this.template = null;
this.templateContext = templateContext;
return me();
}
/**
* Template path is implicit if {@link #templatePath(String)} is never called
* on this context instance
* @return `true` if template path is implicit
*/
@Override
public boolean templatePathIsImplicit() {
return null == templatePath;
}
@Override
public Template cachedTemplate() {
return template;
}
public CTX disableTemplateCaching() {
this.noTemplateCache = true;
return me();
}
@Override
public CTX cacheTemplate(Template template) {
if (!noTemplateCache) {
this.template = template;
}
return me();
}
@Override
public String pattern() {
return pattern;
}
@Override
public CTX pattern(String pattern) {
this.pattern = pattern;
return me();
}
/**
* Default session id is `null`
* @return the session id
*/
public String sessionId() {
return null;
}
public final CTX locale(Locale locale) {
this.locale = locale;
return me();
}
public final Locale locale() {
return this.locale;
}
public Locale locale(boolean required) {
Locale locale = this.locale;
if (null == locale) {
if (!required) {
return null;
}
locale = config().locale();
}
return locale;
}
public String i18n(boolean ignoreError, String msgId, Object... args) {
return I18n.i18n(ignoreError, locale(true), msgId, args);
}
public String i18n(String msgId, Object ... args) {
return I18n.i18n(locale(true), msgId, args);
}
public String _act_i18n(String msgId, Object... args) {
return I18n.i18n(locale(true), act_messages.class, msgId, args);
}
public String i18n(Class<?> bundleSpec, String msgId, Object... args) {
return I18n.i18n(locale(true), bundleSpec, msgId, args);
}
public String i18n(boolean ignoreError, Class<?> bundleSpec, String msgId, Object... args) {
return I18n.i18n(ignoreError, locale(true), bundleSpec, msgId, args);
}
public String i18n(Enum<?> msgId) {
return I18n.i18n(locale(true), msgId);
}
public String i18n(Class<?> bundleSpec, Enum<?> msgId) {
return I18n.i18n(locale(true), bundleSpec, msgId);
}
public Map<String, Object> i18n(Class<? extends Enum> enumClass) {
return I18n.i18n(locale(true), enumClass);
}
public Map<String, Object> i18n(Class<?> bundleSpec, Class<? extends Enum> enumClass) {
return I18n.i18n(locale(true), bundleSpec, enumClass);
}
public Map<String, Object> i18n(Class<? extends Enum> enumClass, boolean outputPropeties) {
return I18n.i18n(locale(true), enumClass, outputPropeties);
}
public Map<String, Object> i18n(Class<?> bundleSpec, Class<? extends Enum> enumClass, boolean outputProperties) {
return I18n.i18n(locale(true), bundleSpec, enumClass, outputProperties);
}
protected CTX me() {
return (CTX) this;
}
@Override
public <T> T renderArg(String name) {
return (T) renderArgs.get(name);
}
@Override
public CTX renderArg(String name, Object val) {
renderArgs.put(name, val);
return me();
}
// see https://github.com/actframework/actframework/issues/312
public CTX fieldOutputVarCount(int count) {
this.fieldOutputVarCount = count;
return me();
}
public int fieldOutputVarCount() {
return fieldOutputVarCount;
}
@Override
public Map<String, Object> renderArgs() {
return C.newMap(renderArgs);
}
protected boolean hasRenderArgs() {
return !renderArgs.isEmpty();
}
/**
* Associate a user attribute to the context. Could be used by third party
* libraries or user application
*
* @param name the className used to reference the attribute
* @param attr the attribute object
* @return this context
*/
public CTX attribute(String name, Object attr) {
attributes.put(name, attr);
return me();
}
public <T> T attribute(String name) {
return $.cast(attributes.get(name));
}
public CTX removeAttribute(String name) {
attributes.remove(name);
return me();
}
@Override
public Map<String, Object> attributes() {
return attributes;
}
@Override
public CTX addListener(Listener listener) {
listenerList.add(listener);
return me();
}
@Override
public CTX addDestroyable(Destroyable resource) {
destroyableList.add(resource);
return me();
}
@Override
public S.Buffer strBuf() {
return strBuf.consumed() ? strBuf.reset() : S.newBuffer();
}
@Override
public CTX addViolations(Map<String, ConstraintViolation> violations) {
this.violations.putAll(violations);
return me();
}
@Override
public CTX addViolation(String property, ConstraintViolation violation) {
this.violations.put(property, violation);
return me();
}
@Override
public boolean hasViolation() {
return !violations.isEmpty();
}
@Override
public Map<String, ConstraintViolation> violations() {
return C.map(this.violations);
}
@Override
public ConstraintViolation violation(String property) {
return this.violations.get(property);
}
public void setJobId(String jobId) {
this.jobId = jobId;
app().jobManager().setJobProgressGauge(jobId, progress);
}
public ProgressGauge progress() {
return progress;
}
public static ActContext.Base<?> currentContext() {
ActContext.Base<?> ctx = ActionContext.current();
if (null != ctx) {
return ctx;
}
ctx = CliContext.current();
if (null != ctx) {
return ctx;
}
ctx = MailerContext.current();
if (null != ctx) {
return ctx;
}
return null;
}
public static Class<? extends ActContext> currentContextType() {
ActContext ctx = currentContext();
return null == ctx ? null : ctx.getClass();
}
public static String dataPattern() {
ActContext<?> current = currentContext();
return null == current ? null : current.pattern();
}
}
}