Repository navigation
Expand file tree
/
Copy pathActContext.java
More file actions
214 lines (186 loc) · 5.85 KB
/
Copy pathActContext.java
File metadata and controls
214 lines (186 loc) · 5.85 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
package act.util;
import act.Destroyable;
import act.app.ActionContext;
import act.app.App;
import act.cli.CliContext;
import act.conf.AppConfig;
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 javax.enterprise.context.RequestScoped;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import static act.app.App.logger;
public interface ActContext<CTX_TYPE extends ActContext> extends ParamValueProvider {
App app();
AppConfig config();
CTX_TYPE accept(H.Format fmt);
H.Format accept();
Locale locale();
/**
* 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);
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);
CTX_TYPE removeAttribute(String name);
interface Listener {
void onDestroy(ActContext context);
}
abstract class Base<VC_TYPE extends Base> extends DestroyableBase implements ActContext<VC_TYPE> {
private App app;
private String templatePath;
private Template template;
private Map<String, Object> renderArgs;
private List<Listener> listenerList;
private List<Destroyable> destroyableList;
private Map<String, Object> attributes;
private Locale locale;
public Base(App app) {
E.NPE(app);
this.app = app;
renderArgs = C.newMap();
attributes = C.newMap();
listenerList = C.newList();
destroyableList = C.newList();
}
@Override
protected void releaseResources() {
for (Listener l : listenerList) {
try {
l.onDestroy(this);
} catch (Exception e) {
logger.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();
}
@Override
public App app() {
return app;
}
@Override
public AppConfig config() {
return app().config();
}
@Override
public String templatePath() {
return templatePath;
}
@Override
public VC_TYPE templatePath(String templatePath) {
this.templatePath = templatePath;
return me();
}
@Override
public Template cachedTemplate() {
return template;
}
@Override
public VC_TYPE cacheTemplate(Template template) {
this.template = template;
return me();
}
public final VC_TYPE locale(Locale locale) {
this.locale = locale;
return me();
}
public final Locale locale() {
return this.locale;
}
protected VC_TYPE me() {
return (VC_TYPE) this;
}
@Override
public <T> T renderArg(String name) {
return (T) renderArgs.get(name);
}
@Override
public VC_TYPE renderArg(String name, Object val) {
renderArgs.put(name, val);
return me();
}
@Override
public Map<String, Object> renderArgs() {
return C.newMap(renderArgs);
}
/**
* 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 VC_TYPE attribute(String name, Object attr) {
attributes.put(name, attr);
return me();
}
public <T> T attribute(String name) {
return $.cast(attributes.get(name));
}
public VC_TYPE removeAttribute(String name) {
attributes.remove(name);
return me();
}
@Override
public VC_TYPE addListener(Listener listener) {
listenerList.add(listener);
return me();
}
@Override
public VC_TYPE addDestroyable(Destroyable resource) {
destroyableList.add(resource);
return me();
}
public static ActContext currentContext() {
ActContext ctx = ActionContext.current();
if (null != ctx) {
return ctx;
}
ctx = MailerContext.current();
if (null != ctx) {
return ctx;
}
ctx = CliContext.current();
if (null != ctx) {
return ctx;
}
return null;
}
public static Class<? extends ActContext> currentContextType() {
ActContext ctx = currentContext();
return null == ctx ? null : ctx.getClass();
}
}
}