Repository navigation
Expand file tree
/
Copy pathActContext.java
More file actions
145 lines (125 loc) · 3.78 KB
/
Copy pathActContext.java
File metadata and controls
145 lines (125 loc) · 3.78 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
package act.util;
import act.Destroyable;
import act.app.App;
import act.conf.AppConfig;
import act.view.Template;
import org.osgl.http.H;
import org.osgl.util.C;
import org.osgl.util.E;
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> {
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);
public static interface Listener {
void onDestroy(ActContext context);
}
public static abstract class ActContextBase<VC_TYPE extends ActContextBase> extends DestroyableBase implements ActContext<VC_TYPE> {
private App app;
private String templatePath;
private Template template;
private Map<String, Object> renderArgs;
private List<Listener> listenerList = C.newList();
private List<Destroyable> destroyableList = C.newList();
public ActContextBase(App app) {
E.NPE(app);
this.app = app;
renderArgs = C.newMap();
}
@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);
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();
}
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);
}
@Override
public VC_TYPE addListener(Listener listener) {
listenerList.add(listener);
return me();
}
@Override
public VC_TYPE addDestroyable(Destroyable resource) {
destroyableList.add(resource);
return me();
}
}
}