forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapLoader.java
More file actions
192 lines (181 loc) · 7.54 KB
/
MapLoader.java
File metadata and controls
192 lines (181 loc) · 7.54 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
package act.inject.param;
/*-
* #%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.App;
import act.app.data.StringValueResolverManager;
import act.inject.DependencyInjector;
import act.util.ActContext;
import act.util.LogSupport;
import org.osgl.$;
import org.osgl.inject.BeanSpec;
import org.osgl.mvc.result.BadRequest;
import org.osgl.util.E;
import org.osgl.util.S;
import org.osgl.util.StringValueResolver;
import java.lang.reflect.Type;
import java.util.*;
class MapLoader extends LogSupport implements ParamValueLoader {
private final ParamKey key;
private final Class<? extends Map> mapClass;
private final Class keyClass;
private final Type valType;
private final DependencyInjector<?> injector;
private final StringValueResolver keyResolver;
private final StringValueResolver valueResolver;
private final Map<ParamKey, ParamValueLoader> childLoaders = new HashMap<ParamKey, ParamValueLoader>();
private final ParamValueLoaderService manager;
private final BeanSpec targetSpec;
MapLoader(
ParamKey key,
Class<? extends Map> mapClass,
Type keyType,
Type valType,
BeanSpec targetSpec,
DependencyInjector<?> injector,
ParamValueLoaderService manager
) {
this.key = key;
this.mapClass = mapClass;
this.keyClass = BeanSpec.rawTypeOf(keyType);
this.valType = valType;
this.injector = injector;
this.manager = manager;
this.targetSpec = targetSpec;
StringValueResolverManager resolverManager = App.instance().resolverManager();
BeanSpec valSpec = BeanSpec.of(valType, injector);
Class<?> valClass = valSpec.rawType();
if (Collection.class.isAssignableFrom(valClass)) {
Class<? extends Collection> colClass = $.cast(valClass);
this.valueResolver = resolverManager.collectionResolver(colClass, (Class<?>)valSpec.typeParams().get(0), ',');
} else {
this.valueResolver = resolverManager.resolver(valClass, BeanSpec.of(valType, injector));
}
if (null == valueResolver) {
warn("Map value type not resolvable: " + valClass.getName());
}
this.keyResolver = resolverManager.resolver(this.keyClass, BeanSpec.of(this.keyClass, injector));
if (null == keyResolver) {
throw new IllegalArgumentException("Map key type not resolvable: " + keyClass.getName());
}
}
@Override
public Object load(Object bean, ActContext<?> context, boolean noDefaultValue) {
ParamTree tree = ParamValueLoaderService.ensureParamTree(context);
ParamTreeNode node = tree.node(key);
if (null == node) {
return noDefaultValue ? null : injector.get(mapClass);
}
Map map = null == bean ? injector.get(mapClass) : (Map) bean;
if (node.isList()) {
if (Integer.class != keyClass) {
throw new BadRequest("cannot load list into map with key type: %s", this.keyClass);
}
List<ParamTreeNode> list = node.list();
for (int i = 0; i < list.size(); ++i) {
ParamTreeNode elementNode = list.get(i);
if (!elementNode.isLeaf()) {
throw new BadRequest("cannot parse param: expect leaf node, found: \n%s", node.debug());
}
if (null == valueResolver) {
throw E.unexpected("Component type not resolvable: %s", valType);
}
if (null != elementNode.value()) {
map.put(i, valueResolver.resolve(elementNode.value()));
}
}
} else if (node.isMap()) {
Set<String> childrenKeys = node.mapKeys();
Class valClass = BeanSpec.rawTypeOf(valType);
for (String s : childrenKeys) {
ParamTreeNode child = node.child(s);
Object key = s;
if (String.class != keyClass) {
key = keyResolver.resolve(s);
}
if (child.isLeaf()) {
if (null == valueResolver) {
throw E.unexpected("Component type not resolvable: %s", valType);
}
String sval = child.value();
if (null == sval) {
continue;
}
if (valClass != String.class) {
Object value = valueResolver.resolve(sval);
if (!valClass.isInstance(value)) {
throw new BadRequest("Cannot load parameter, expected type: %s, found: %s", valClass, value.getClass());
}
map.put(key, value);
} else {
map.put(key, sval);
}
} else {
ParamValueLoader childLoader = childLoader(child.key());
Object value = childLoader.load(null, context, false);
if (null != value) {
if (!valClass.isInstance(value)) {
throw new BadRequest("Cannot load parameter, expected type: %s, found: %s", valClass, value.getClass());
}
map.put(key, value);
}
}
}
} else {
// try evaluate the Map instance from string value
// to support Matrix style URL path variable
String value = node.value();
if (S.notBlank(value)) {
// ";" has higher priority in common separators
String[] pairs = value.split(value.contains(";") ? ";" : S.COMMON_SEP);
for (String pair : pairs) {
if (!pair.contains("=")) {
throw new BadRequest("Cannot load parameter, expected map, found:%s", node.value());
}
String sKey = S.beforeFirst(pair, "=");
String sVal = S.afterFirst(pair, "=");
Object oKey = keyResolver.resolve(sKey);
Object oVal = valueResolver.resolve(sVal);
map.put(oKey, oVal);
}
}
}
return map;
}
@Override
public String bindName() {
return key.toString();
}
private ParamValueLoader childLoader(ParamKey key) {
ParamValueLoader loader = childLoaders.get(key);
if (null == loader) {
loader = buildChildLoader(key);
childLoaders.put(key, loader);
}
return loader;
}
private ParamValueLoader buildChildLoader(ParamKey key) {
return manager.buildLoader(key, valueSpec());
}
private BeanSpec valueSpec() {
List<Type> typeParams = targetSpec.typeParams();
E.illegalArgumentIf(typeParams.size() != 2);
return BeanSpec.of(typeParams.get(1), targetSpec.injector());
}
}