Repository navigation
Expand file tree
/
Copy pathAct.java
More file actions
580 lines (493 loc) · 16.6 KB
/
Copy pathAct.java
File metadata and controls
580 lines (493 loc) · 16.6 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
package act;
import act.app.*;
import act.app.event.AppEventId;
import act.app.util.NamedPort;
import act.boot.BootstrapClassLoader;
import act.boot.PluginClassProvider;
import act.conf.ActConfLoader;
import act.conf.ActConfig;
import act.conf.AppConfig;
import act.controller.meta.ActionMethodMetaInfo;
import act.controller.meta.CatchMethodMetaInfo;
import act.controller.meta.InterceptorMethodMetaInfo;
import act.db.DbManager;
import act.event.ActEvent;
import act.event.ActEventListener;
import act.event.EventBus;
import act.handler.builtin.controller.*;
import act.handler.builtin.controller.impl.ReflectedHandlerInvoker;
import act.job.AppJobManager;
import act.metric.MetricPlugin;
import act.metric.SimpleMetricPlugin;
import act.plugin.AppServicePluginManager;
import act.plugin.GenericPluginManager;
import act.plugin.Plugin;
import act.plugin.PluginScanner;
import act.util.*;
import act.view.ViewManager;
import act.xio.NetworkHandler;
import act.xio.Network;
import act.xio.undertow.UndertowNetwork;
import org.osgl.$;
import org.osgl.cache.CacheService;
import org.osgl.exception.NotAppliedException;
import org.osgl.logging.L;
import org.osgl.logging.Logger;
import org.osgl.util.C;
import org.osgl.util.E;
import java.util.List;
import java.util.Map;
import static act.Destroyable.Util.tryDestroy;
/**
* The Act server
*/
public final class Act {
public enum Mode {
PROD, DEV() {
@Override
public AppScanner appScanner() {
return AppScanner.SINGLE_APP_SCANNER;
}
@Override
public AppClassLoader classLoader(App app) {
return new DevModeClassLoader(app);
}
};
private final String confPrefix = "%" + name().toLowerCase() + ".";
/**
* DEV mode is special as Act might load classes
* directly from srccode code when running in this mode
*/
public boolean isDev() {
return DEV == this;
}
public String configKey(String key) {
return confPrefix + key;
}
public AppScanner appScanner() {
return AppScanner.DEF_SCANNER;
}
public AppClassLoader classLoader(App app) {
return new AppClassLoader(app);
}
public ControllerAction createRequestHandler(ActionMethodMetaInfo action, App app) {
return ReflectedHandlerInvoker.createControllerAction(action, app);
}
public BeforeInterceptor createBeforeInterceptor(InterceptorMethodMetaInfo before, App app) {
return ReflectedHandlerInvoker.createBeforeInterceptor(before, app);
}
public AfterInterceptor createAfterInterceptor(InterceptorMethodMetaInfo after, App app) {
return ReflectedHandlerInvoker.createAfterInterceptor(after, app);
}
public ExceptionInterceptor createExceptionInterceptor(CatchMethodMetaInfo ex, App app) {
return ReflectedHandlerInvoker.createExceptionInterceptor(ex, app);
}
public FinallyInterceptor createFinallyInterceptor(InterceptorMethodMetaInfo fin, App app) {
return ReflectedHandlerInvoker.createFinannyInterceptor(fin, app);
}
public static Mode valueOfIgnoreCase(String mode) {
return valueOf(mode.trim().toUpperCase());
}
}
public static final String VERSION = Version.fullVersion();
private static Logger logger = L.get(Act.class);
private static ActConfig conf;
private static Mode mode = Mode.PROD;
private static boolean multiTenant = false;
private static AppManager appManager;
private static ViewManager viewManager;
private static Network network;
private static MetricPlugin metricPlugin;
private static BytecodeEnhancerManager enhancerManager;
private static SessionManager sessionManager;
private static AppCodeScannerPluginManager scannerPluginManager;
private static DbManager dbManager;
private static GenericPluginManager pluginManager;
private static AppServicePluginManager appPluginManager;
private static Map<String, Plugin> genericPluginRegistry = C.newMap();
private static Map<Class<? extends ActEvent>, List<ActEventListener>> listeners = C.newMap();
public static List<Class<?>> pluginClasses() {
ClassLoader cl = Act.class.getClassLoader();
if (cl instanceof PluginClassProvider) {
return ((PluginClassProvider) cl).pluginClasses();
} else {
logger.warn("Class loader [%s] of Act is not a PluginClassProvider", cl);
return C.list();
}
}
public static ClassInfoRepository classInfoRepository() {
ClassLoader cl = Act.class.getClassLoader();
if (cl instanceof BootstrapClassLoader) {
return ((BootstrapClassLoader) cl).classInfoRepository();
} else {
logger.warn("Class loader [%s] of Act is not a ActClassLoader", cl);
return null;
}
}
public static Mode mode() {
return mode;
}
public static boolean isDev() {
return mode.isDev();
}
public static ActConfig conf() {
return conf;
}
public static boolean multiTenant() {
return multiTenant;
}
public static BytecodeEnhancerManager enhancerManager() {
return enhancerManager;
}
public static GenericPluginManager pluginManager() {
return pluginManager;
}
public static DbManager dbManager() {
return dbManager;
}
public static ViewManager viewManager() {
return viewManager;
}
public static SessionManager sessionManager() {
return sessionManager;
}
public static AppCodeScannerPluginManager scannerPluginManager() {
return scannerPluginManager;
}
public static AppServicePluginManager appServicePluginManager() {
return appPluginManager;
}
public static AppManager applicationManager() {
return appManager;
}
public static MetricPlugin metricPlugin() {
return metricPlugin;
}
public static void registerPlugin(Plugin plugin) {
genericPluginRegistry.put(plugin.getClass().getCanonicalName().intern(), plugin);
}
@SuppressWarnings("unchecked")
public static <T extends Plugin> T registeredPlugin(Class<T> type) {
return (T)genericPluginRegistry.get(type.getCanonicalName().intern());
}
public static void startServer() {
start(false, null, null);
}
public static void startApp(String appName, String appVersion) {
String s = System.getProperty("app.mode");
if (null != s) {
mode = Mode.valueOfIgnoreCase(s);
} else {
mode = Mode.DEV;
}
start(true, appName, appVersion);
}
public static void shutdownApp(App app) {
appManager.unload(app);
}
private static void start(boolean singleAppServer, String appName, String appVersion) {
Banner.print(appName, appVersion);
loadConfig();
initMetricPlugin();
initPluginManager();
initAppServicePluginManager();
initDbManager();
//initExecuteService();
initEnhancerManager();
initViewManager();
initSessionManager();
initAppCodeScannerPluginManager();
loadPlugins();
initNetworkLayer();
initApplicationManager();
try {
logger.info("loading application(s) ...");
if (singleAppServer) {
appManager.loadSingleApp(appName);
} else {
appManager.scan();
}
} catch (ActAppException e) {
logger.fatal(e, "Error starting ACT");
return;
}
startNetworkLayer();
Thread.currentThread().setContextClassLoader(Act.class.getClassLoader());
}
public static void shutdown() {
shutdownNetworkLayer();
destroyApplicationManager();
unloadPlugins();
destroyAppCodeScannerPluginManager();
destroySessionManager();
destroyViewManager();
destroyEnhancerManager();
destroyDbManager();
destroyAppServicePluginManager();
destroyPluginManager();
destroyMetricPlugin();
unloadConfig();
destroyNetworkLayer();
}
public static RequestServerRestart requestRestart() {
E.illegalStateIf(!isDev());
throw new RequestServerRestart();
}
public static RequestRefreshClassLoader requestRefreshClassLoader() {
E.illegalStateIf(!isDev());
throw RequestRefreshClassLoader.INSTANCE;
}
public static void hook(App app) {
int port = app.config().httpPort();
network.register(port, new NetworkHandler(app));
List<NamedPort> portList = app.config().namedPorts();
for (NamedPort np : portList) {
network.register(np.port(), new NetworkHandler(app, np));
}
}
public static synchronized void trigger(ActEvent<?> event) {
List<ActEventListener> list = listeners.get(event.getClass());
if (null != list) {
for (ActEventListener l : list) {
try {
l.on(event);
} catch (Exception e) {
logger.error(e, "error calling act event listener %s on event %s", l.id(), event);
}
}
}
}
public static synchronized <T extends ActEvent> void registerEventListener(Class<T> eventClass, ActEventListener<T> listener) {
List<ActEventListener> list = listeners.get(eventClass);
if (null == list) {
list = C.newList();
listeners.put(eventClass, list);
}
if (!list.contains(listener)) {
list.add(listener);
}
}
/**
* Generate custer unique ID via {@link App#cuid()}
*
* @return a cluster unique ID generated
*/
public static String cuid() {
return App.instance().cuid();
}
/**
* Return the {@link App} instance
* @return the App instance
*/
public static App app() {
return App.instance();
}
/**
* Return the {@link App}'s config
* @return the app config
*/
public static AppConfig appConfig() {
return App.instance().config();
}
/**
* Utility method to retrieve singleton instance via {@link App#singleton(Class)} method
* @param singletonClass
* @param <T>
* @return the singleton instance
*/
public static <T> T singleton(Class<T> singletonClass) {
return App.instance().singleton(singletonClass);
}
/**
* Returns the application's {@link App#cache() cache service}
* @return the cache service
*/
public static CacheService cache() {
return App.instance().cache();
}
/**
* Trigger an {@link act.app.event.AppEventId App event}
* @param appEvent the app event
*/
public static void emit(AppEventId appEvent) {
App.instance().emit(appEvent);
}
/**
* Alias of {@link #emit(AppEventId)}
* @param appEventId the app event
*/
public static void trigger(AppEventId appEventId) {
emit(appEventId);
}
/**
* Returns the {@link App app}'s {@link EventBus eventBus}
* @return the eventBus
*/
public static EventBus eventBus() {
return App.instance().eventBus();
}
/**
* Returns the {@link App app}'s {@link AppJobManager}
* @return the app's jobManager
*/
public static AppJobManager jobManager() {
return App.instance().jobManager();
}
/**
* Return an instance with give class name
* @param className the class name
* @param <T> the generic type of the class
* @return the instance of the class
*/
public static <T> T newInstance(String className) {
return App.instance().newInstance(className);
}
/**
* Return an instance with give class
* @param clz the class
* @param <T> the generic type of the class
* @return the instance of the class
*/
public static <T> T newInstance(Class<T> clz) {
return App.instance().newInstance(clz);
}
private static void loadConfig() {
logger.info("loading configuration ...");
String s = SysProps.get("act.mode");
if (null != s) {
mode = Mode.valueOfIgnoreCase(s);
}
logger.info("Act starts in %s mode", mode);
conf = new ActConfLoader().load(null);
}
private static void unloadConfig() {
conf = null;
}
private static void loadPlugins() {
logger.info("scanning plugins ...");
new PluginScanner().scan();
}
private static void unloadPlugins() {
new PluginScanner().unload();
}
private static void initViewManager() {
logger.info("initializing view manager ...");
viewManager = new ViewManager();
}
private static void destroyViewManager() {
if (null != viewManager) {
viewManager.destroy();
viewManager = null;
}
}
private static void initMetricPlugin() {
logger.info("initializing metric plugin ...");
metricPlugin = new SimpleMetricPlugin();
}
private static void destroyMetricPlugin() {
if (null != metricPlugin) {
tryDestroy(metricPlugin);
metricPlugin = null;
}
}
private static void initPluginManager() {
logger.info("initializing generic plugin manager ...");
pluginManager = new GenericPluginManager();
}
private static void destroyPluginManager() {
if (null != pluginManager) {
pluginManager.destroy();
pluginManager = null;
}
}
private static void initAppServicePluginManager() {
logger.info("initializing app service plugin manager ...");
appPluginManager = new AppServicePluginManager();
}
private static void destroyAppServicePluginManager() {
if (null != appPluginManager) {
appPluginManager.destroy();
appPluginManager = null;
}
}
private static void initSessionManager() {
logger.info("initializing session manager ...");
sessionManager = new SessionManager();
}
private static void destroySessionManager() {
if (null != sessionManager) {
sessionManager.destroy();
sessionManager = null;
}
}
private static void initDbManager() {
logger.info("initializing db manager ...");
dbManager = new DbManager();
}
private static void destroyDbManager() {
if (null != dbManager) {
dbManager.destroy();
dbManager = null;
}
}
private static void initAppCodeScannerPluginManager() {
logger.info("initializing app code scanner plugin manager ...");
scannerPluginManager = new AppCodeScannerPluginManager();
}
private static void destroyAppCodeScannerPluginManager() {
if (null != scannerPluginManager) {
scannerPluginManager.destroy();
scannerPluginManager = null;
}
}
static void initEnhancerManager() {
logger.info("initializing byte code enhancer manager ...");
enhancerManager = new BytecodeEnhancerManager();
}
private static void destroyEnhancerManager() {
if (null != enhancerManager) {
enhancerManager.destroy();
enhancerManager = null;
}
}
private static void initNetworkLayer() {
logger.info("initializing network layer ...");
network = new UndertowNetwork();
}
private static void destroyNetworkLayer() {
if (null != network) {
network.destroy();
network = null;
}
}
private static void startNetworkLayer() {
if (network.isDestroyed()) {
return;
}
logger.info("starting network layer ...");
network.start();
}
private static void shutdownNetworkLayer() {
logger.info("shutting down network layer ...");
network.shutdown();
}
protected static void initApplicationManager() {
logger.info("initializing application manager ...");
appManager = AppManager.create();
}
private static void destroyApplicationManager() {
if (null != appManager) {
appManager.destroy();
appManager = null;
}
}
public enum F {
;
public static final $.F0<Mode> MODE_ACCESSOR = new $.F0<Mode>() {
@Override
public Mode apply() throws NotAppliedException, $.Break {
return mode;
}
};
}
}