forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActProviders.java
More file actions
280 lines (246 loc) · 8.25 KB
/
ActProviders.java
File metadata and controls
280 lines (246 loc) · 8.25 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
package act.inject;
/*-
* #%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.Act;
import act.app.ActionContext;
import act.app.App;
import act.app.util.AppCrypto;
import act.cli.CliContext;
import act.cli.CliOverHttpContext;
import act.cli.CliSession;
import act.conf.AppConfig;
import act.db.Dao;
import act.event.EventBus;
import act.job.JobContext;
import act.mail.MailerContext;
import act.util.ActContext;
import act.util.ProgressGauge;
import act.util.SimpleProgressGauge;
import act.ws.SecureTicketCodec;
import act.ws.WebSocketContext;
import org.osgl.$;
import org.osgl.Osgl;
import org.osgl.cache.CacheService;
import org.osgl.exception.NotAppliedException;
import org.osgl.http.H;
import org.osgl.logging.Logger;
import org.osgl.util.C;
import org.osgl.util.E;
import org.osgl.web.util.UserAgent;
import javax.inject.Provider;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Locale;
import java.util.Set;
/**
* Name space of built in providers
*/
@SuppressWarnings("unused")
public final class ActProviders {
private ActProviders() {}
private static Set<Class> providedTypes = C.newSet();
static {
registerBuiltInProviders(ActProviders.class, new $.F2<Class, Provider, Void>() {
@Override
public Void apply(Class aClass, Provider provider) throws NotAppliedException, Osgl.Break {
providedTypes.add(aClass);
return null;
}
});
}
public static final Provider<App> APP = new Provider<App>() {
@Override
public App get() {
return app();
}
};
public static final Provider<ActionContext> ACTION_CONTEXT = new Provider<ActionContext>() {
@Override
public ActionContext get() {
return ActionContext.current();
}
};
public static final Provider<H.Session> SESSION = new Provider<H.Session>() {
@Override
public H.Session get() {
return ActionContext.current().session();
}
};
public static final Provider<H.Flash> FLASH = new Provider<H.Flash>() {
@Override
public H.Flash get() {
return ActionContext.current().flash();
}
};
public static final Provider<H.Request> REQUEST = new Provider<H.Request>() {
@Override
public H.Request get() {
return ActionContext.current().req();
}
};
public static final Provider<H.Response> RESPONSE = new Provider<H.Response>() {
@Override
public H.Response get() {
return ActionContext.current().resp();
}
};
public static final Provider<CliContext> CLI_CONTEXT = new Provider<CliContext>() {
@Override
public CliContext get() {
return CliContext.current();
}
};
public static final Provider<CliSession> CLI_SESSION = new Provider<CliSession>() {
@Override
public CliSession get() {
CliContext context = CliContext.current();
return null == context ? null : context.session();
}
};
public static final Provider<ProgressGauge> PROGRESS_GAUGE = new Provider<ProgressGauge>() {
@Override
public ProgressGauge get() {
ActContext.Base<?> context = ActContext.Base.currentContext();
if (null != context) {
return context.progress();
}
return new SimpleProgressGauge();
}
};
public static final Provider<MailerContext> MAILER_CONTEXT = new Provider<MailerContext>() {
@Override
public MailerContext get() {
return MailerContext.current();
}
};
public static final Provider<WebSocketContext> WEB_SOCKET_CONTEXT = new Provider<WebSocketContext>() {
@Override
public WebSocketContext get() {
return WebSocketContext.current();
}
};
public static final Provider<ActContext> ACT_CONTEXT = new Provider<ActContext>() {
@Override
public ActContext get() {
ActContext ctx = MailerContext.current();
if (null != ctx) {
return ctx;
}
ctx = WebSocketContext.current();
if (null != ctx) {
return ctx;
}
ctx = ActionContext.current();
if (null != ctx) {
return ctx;
}
ctx = CliContext.current();
if (null != ctx) {
return ctx;
}
ctx = CliOverHttpContext.current();
if (null != ctx) {
return ctx;
}
return JobContext.current();
}
};
public static final Provider<Logger> LOGGER = new Provider<Logger>() {
@Override
public Logger get() {
return Act.LOGGER;
}
};
public static final Provider<UserAgent> USER_AGENT = new Provider<UserAgent>() {
@Override
public UserAgent get() {
ActionContext actionContext = ActionContext.current();
if (null == actionContext) {
throw new IllegalStateException();
}
return actionContext.userAgent();
}
};
public static final Provider<AppConfig> APP_CONFIG = new Provider<AppConfig>() {
@Override
public AppConfig get() {
return app().config();
}
};
public static final Provider<AppCrypto> APP_CRYPTO = new Provider<AppCrypto>() {
@Override
public AppCrypto get() {
return app().crypto();
}
};
public static final Provider<CacheService> APP_CACHE_SERVICE = new Provider<CacheService>() {
@Override
public CacheService get() {
return app().cache();
}
};
public static final Provider<EventBus> EVENT_BUS = new Provider<EventBus>() {
@Override
public EventBus get() {
return app().eventBus();
}
};
public static final Provider<Locale> LOCALE = new Provider<Locale>() {
@Override
public Locale get() {
ActContext context = ActContext.Base.currentContext();
return null != context ? context.locale(true) : app().config().locale();
}
};
public static final Provider<SecureTicketCodec> SECURE_TICKET_CODEC_PROVIDER = new Provider<SecureTicketCodec>() {
@Override
public SecureTicketCodec get() {
AppConfig config = Act.appConfig();
return config.secureTicketCodec();
}
};
public static void registerBuiltInProviders(Class<?> providersClass, $.Func2<Class, Provider, ?> register) {
for (Field field : providersClass.getDeclaredFields()) {
try {
if (Provider.class.isAssignableFrom(field.getType())) {
field.setAccessible(true);
Type type = field.getGenericType();
if (type instanceof ParameterizedType) {
ParameterizedType ptype = $.cast(field.getGenericType());
Provider<?> provider = $.cast(field.get(null));
register.apply((Class) ptype.getActualTypeArguments()[0], provider);
}
}
} catch (Exception e) {
throw E.unexpected(e);
}
}
}
public static boolean isProvided(Class<?> aClass) {
return providedTypes.contains(aClass) || Dao.class.isAssignableFrom(aClass);
}
private static App app() {
return App.instance();
}
public static void addProvidedType(Class<?> aClass) {
providedTypes.add(aClass);
}
}