/
App.java
297 lines (232 loc) · 6.8 KB
/
App.java
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package org.rapidoid.setup;
/*
* #%L
* rapidoid-http-server
* %%
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and contributors
* %%
* 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 org.rapidoid.RapidoidThing;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.collection.Coll;
import org.rapidoid.commons.Arr;
import org.rapidoid.config.Conf;
import org.rapidoid.config.ConfigHelp;
import org.rapidoid.data.JSON;
import org.rapidoid.env.Env;
import org.rapidoid.io.Res;
import org.rapidoid.ioc.Beans;
import org.rapidoid.ioc.IoC;
import org.rapidoid.ioc.IoCContext;
import org.rapidoid.job.Jobs;
import org.rapidoid.log.Log;
import org.rapidoid.render.Templates;
import org.rapidoid.reverseproxy.Reverse;
import org.rapidoid.scan.ClasspathScanner;
import org.rapidoid.scan.ClasspathUtil;
import org.rapidoid.scan.Scan;
import org.rapidoid.u.U;
import org.rapidoid.util.Msc;
import java.util.List;
import java.util.Set;
@Authors("Nikolche Mihajlovski")
@Since("5.1.0")
public class App extends RapidoidThing {
private static volatile String[] path;
private static volatile String mainClassName;
private static volatile String appPkgName;
private static volatile boolean dirty;
private static volatile boolean restarted;
private static volatile boolean managed;
private static final Set<Class<?>> invoked = Coll.synchronizedSet();
static volatile ClassLoader loader = App.class.getClassLoader();
public static void args(String[] args, String... extraArgs) {
args = Arr.concat(extraArgs, args);
ConfigHelp.processHelp(args);
Env.setArgs(args);
AppVerification.selfVerify(args);
}
public static AppBootstrap bootstrap(String[] args, String... extraArgs) {
args = Arr.concat(extraArgs, args);
args(args);
if (!managed) scan();
return boot();
}
public static AppBootstrap run(String[] args, String... extraArgs) {
args = Arr.concat(extraArgs, args);
args(args);
// no implicit classpath scanning here
return boot();
}
public static AppBootstrap boot() {
Jobs.initialize();
registerConfigListeners();
AppBootstrap bootstrap = new AppBootstrap();
bootstrap.services();
return bootstrap;
}
private static void registerConfigListeners() {
Conf.PROXY.addChangeListener(new ProxyConfigListener());
Conf.API.addChangeListener(new APIConfigListener());
}
public static void profiles(String... profiles) {
Env.setProfiles(profiles);
Conf.reset();
}
public static void path(String... path) {
App.path = path;
}
public static synchronized String[] path() {
inferCallers();
if (App.path == null) {
App.path = appPkgName != null ? U.array(appPkgName) : new String[0];
}
return path;
}
static void inferCallers() {
if (!restarted && appPkgName == null && mainClassName == null) {
appPkgName = Msc.getCallingPackage();
if (mainClassName == null) {
Class<?> mainClass = Msc.getCallingMainClass();
invoked.add(mainClass);
mainClassName = mainClass != null ? mainClass.getName() : null;
}
if (mainClassName != null || appPkgName != null) {
Log.info("Inferring application root", "!main", mainClassName, "!package", appPkgName);
}
}
}
private static synchronized void restartApp() {
if (!Msc.hasRapidoidWatch()) {
Log.warn("Cannot reload/restart the application, module rapidoid-watch is missing!");
}
if (mainClassName == null) {
Log.warn("Cannot reload/restart the application, the main app class couldn't be detected!");
}
Msc.logSection("!Restarting the web application...");
restarted = true;
Set<AppRestartListener> listeners = U.set(On.changes().getRestartListeners());
for (AppRestartListener listener : listeners) {
try {
listener.beforeAppRestart();
} catch (Exception e) {
Log.error("Error occurred in the app restart listener!", e);
}
}
App.path = null;
Conf.reset();
Env.reset();
Res.reset();
Templates.reset();
JSON.reset();
AppBootstrap.reset();
ClasspathScanner.reset();
invoked.clear();
for (Setup setup : Setup.instances()) {
setup.reload();
}
Setup.initDefaults();
if (Msc.hasRapidoidJPA()) {
loader = ReloadUtil.reloader();
ClasspathUtil.setDefaultClassLoader(loader);
}
Class<?> entry;
try {
entry = loader.loadClass(mainClassName);
} catch (ClassNotFoundException e) {
Log.error("Cannot restart the application, the main class (app entry point) is missing!");
return;
}
Msc.invokeMain(entry, U.arrayOf(String.class, Env.args()));
for (AppRestartListener listener : listeners) {
try {
listener.afterAppRestart();
} catch (Exception e) {
Log.error("Error occurred in the app restart listener!", e);
}
}
Log.info("!Successfully restarted the application!");
}
public static void resetGlobalState() {
mainClassName = null;
appPkgName = null;
restarted = false;
managed = false;
dirty = false;
path = null;
loader = App.class.getClassLoader();
Setup.initDefaults();
AppBootstrap.reset();
invoked.clear();
Reverse.reset();
}
public static void notifyChanges() {
if (!dirty) {
dirty = true;
Log.info("Detected class or resource changes");
}
}
static void restartIfDirty() {
if (dirty) {
synchronized (Setup.class) {
if (dirty) {
restartApp();
dirty = false;
}
}
}
}
public static List<Class<?>> findBeans(String... packages) {
if (U.isEmpty(packages)) {
packages = path();
}
return Scan.annotated(IoC.ANNOTATIONS).in(packages).loadAll();
}
public static boolean scan(String... packages) {
String appPath = Conf.APP.entry("path").str().getOrNull();
if (U.notEmpty(appPath)) {
App.path(appPath);
}
List<Class<?>> beans = App.findBeans(packages);
beans(beans.toArray());
return !beans.isEmpty();
}
public static void beans(Object... beans) {
Setup.ON.beans(beans);
}
public static IoCContext context() {
return IoC.defaultContext();
}
static void filterAndInvokeMainClasses(Object[] beans) {
managed(true);
Msc.filterAndInvokeMainClasses(beans, invoked);
}
public static boolean isRestarted() {
return restarted;
}
public static boolean managed() {
return managed;
}
public static void managed(boolean managed) {
App.managed = managed;
}
public static void register(Beans beans) {
Setup.ON.register(beans);
}
public static void shutdown() {
Setup.shutdownAll();
}
}