forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActContextInjection.java
More file actions
157 lines (131 loc) · 4.3 KB
/
ActContextInjection.java
File metadata and controls
157 lines (131 loc) · 4.3 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
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.app.ActionContext;
import org.osgl.$;
import org.osgl.util.S;
/**
* Keep all information required to inject {@link ActionContext}
* into the controller action handler
*/
public class ActContextInjection<T> {
/**
* Define how framework should inject AppContext to the
* controller action handler
*/
public enum InjectType {
/**
* Inject AppContext into controller instance field. This injection
* is used when both of the following requirements are met
* <ul>
* <li>The controller has a field with type {@link ActionContext}</li>
* <li>The action handler method is not {@code static}</li>
* </ul>
* <p>Framework must instantiate an new instance of the
* controller before calling the action handler method</p>
*/
FIELD,
/**
* Pass AppContext via controller action method call. This injection
* is used when there are parameter of type AppContext in the action
* handler method signature
*/
PARAM,
/**
* Save AppContext to {@link org.osgl.concurrent.ContextLocal}. If none of
* the {@link #FIELD} and {@link #PARAM} can be used to inject the
* {@code AppContext}, then framework shall call {@link ActionContext#saveLocal}
* method to save the app context instance into thread local variable, such that the
* application developer could use {@link ActionContext#current} method to
* access the current application context
*/
LOCAL;
public boolean isLocal() {
return this == LOCAL;
}
public boolean isField() {
return this == FIELD;
}
public boolean isParam() {
return this == PARAM;
}
}
private InjectType type;
protected T v;
private ActContextInjection(InjectType type, T v) {
this.type = type;
this.v = v;
}
public InjectType injectVia() {
return type;
}
@Override
public int hashCode() {
return $.hc(type, v);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof ActContextInjection) {
ActContextInjection that = (ActContextInjection) obj;
return that.type == type && $.eq(that.v, v);
}
return false;
}
@Override
public String toString() {
return S.concat("inject[", type.name().toLowerCase(), ", ", S.string(v), "]");
}
public static class FieldActContextInjection extends ActContextInjection<String> {
public FieldActContextInjection(String fieldName) {
super(InjectType.FIELD, fieldName);
}
public String fieldName() {
return v;
}
}
public static class ParamAppContextInjection extends ActContextInjection<Integer> {
private int lvLookupIdx;
public ParamAppContextInjection(Integer paramIndex) {
super(InjectType.PARAM, paramIndex);
}
public int paramIndex() {
return v;
}
public ParamAppContextInjection lvLookupIdx(int index) {
this.lvLookupIdx = index;
return this;
}
public int lvLookupIdx() {
return lvLookupIdx;
}
}
public static class LocalAppContextInjection extends ActContextInjection<Void> {
public LocalAppContextInjection() {
super(InjectType.LOCAL, null);
}
@Override
public String toString() {
return "inject[local]";
}
}
}