forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAct.java
More file actions
1138 lines (1009 loc) · 35.3 KB
/
Act.java
File metadata and controls
1138 lines (1009 loc) · 35.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
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package act;
/*-
* #%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 static act.Destroyable.Util.tryDestroy;
import act.app.*;
import act.app.event.SysEventId;
import act.app.util.NamedPort;
import act.boot.BootstrapClassLoader;
import act.boot.PluginClassProvider;
import act.boot.app.FullStackAppBootstrapClassLoader;
import act.boot.app.RunApp;
import act.conf.*;
import act.controller.meta.ActionMethodMetaInfo;
import act.controller.meta.CatchMethodMetaInfo;
import act.controller.meta.InterceptorMethodMetaInfo;
import act.crypto.AppCrypto;
import act.db.DbManager;
import act.event.ActEvent;
import act.event.ActEventListener;
import act.event.EventBus;
import act.handler.RequestHandlerBase;
import act.handler.SimpleRequestHandler;
import act.handler.builtin.controller.*;
import act.handler.builtin.controller.impl.ReflectedHandlerInvoker;
import act.inject.DependencyInjector;
import act.internal.util.AppDescriptor;
import act.job.JobManager;
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.route.RouteSource;
import act.sys.Env;
import act.util.AppCodeScannerPluginManager;
import act.util.Banner;
import act.util.ClassInfoRepository;
import act.util.SysProps;
import act.view.ViewManager;
import act.xio.Network;
import act.xio.NetworkHandler;
import act.xio.undertow.UndertowNetwork;
import org.joda.time.*;
import org.osgl.$;
import org.osgl.cache.CacheService;
import org.osgl.exception.NotAppliedException;
import org.osgl.exception.UnexpectedException;
import org.osgl.http.H;
import org.osgl.logging.LogManager;
import org.osgl.logging.Logger;
import org.osgl.util.*;
import org.osgl.util.converter.TypeConverterRegistry;
import osgl.version.Version;
import osgl.version.Versioned;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.reflect.Method;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
/**
* The Act runtime and facade
*/
@Versioned
public final class Act {
public static final Version VERSION = Version.of(Act.class);
public static final Logger LOGGER = LogManager.get(Act.class);
/**
* Used to set/get system property to communicate the app jar file if
* app is loaded from jar
*/
public static final String PROP_APP_JAR_FILE = "act_app_jar_file";
/**
* The manifest attributes property to fetch app jar file
*/
private static final String ATTR_APP_JAR = "App-Jar";
public enum Mode {
PROD,
/**
* DEV mode is special as Act might load classes
* directly from srccode code when running in this mode
*/
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() + ".";
/**
* Returns if the current mode is {@link #DEV dev mode}
*
* @return `true` if the current mode is dev
*/
public boolean isDev() {
return DEV == this;
}
/**
* Returns if the current mode is {@link #PROD prod mode}
*
* @return `true` if the current mode is product mode
*/
public boolean isProd() {
return PROD == 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());
}
}
private static ActConfig conf;
private static Mode mode = Mode.PROD;
private static String nodeGroup = "";
private static AppManager appManager;
private static ViewManager viewManager;
private static Network network;
private static MetricPlugin metricPlugin;
private static BytecodeEnhancerManager enhancerManager;
private static AppCodeScannerPluginManager scannerPluginManager;
private static DbManager dbManager;
private static GenericPluginManager pluginManager;
private static AppServicePluginManager appPluginManager;
private static Map<String, Plugin> genericPluginRegistry = new HashMap<>();
private static Map<Class<? extends ActEvent>, List<ActEventListener>> listeners = new HashMap<>();
public static Mode mode() {
return mode;
}
public static boolean isProd() {
return mode.isProd();
}
public static boolean isDev() {
return mode.isDev();
}
/**
* Return the current profile name
*/
public static String profile() {
return ConfLoader.confSetName();
}
public static String nodeGroup() {
return nodeGroup;
}
public static ActConfig conf() {
return conf;
}
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 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 AppServicePluginManager appServicePluginManager() {
return appPluginManager;
}
public static DbManager dbManager() {
return dbManager;
}
public static BytecodeEnhancerManager enhancerManager() {
return enhancerManager;
}
public static GenericPluginManager pluginManager() {
return pluginManager;
}
public static MetricPlugin metricPlugin() {
return metricPlugin;
}
public static AppCodeScannerPluginManager scannerPluginManager() {
return scannerPluginManager;
}
public static AppManager applicationManager() {
return appManager;
}
public static ViewManager viewManager() {
return viewManager;
}
public static Network network() {
return network;
}
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 startup(AppDescriptor descriptor) {
processEnvironment(descriptor);
Banner.print(descriptor);
registerTypeConverters();
loadConfig();
initMetricPlugin();
initPluginManager();
initAppServicePluginManager();
initDbManager();
initEnhancerManager();
initViewManager();
initAppCodeScannerPluginManager();
loadPlugins();
enhancerManager().registered(); // so it can order the enhancers
initNetworkLayer();
initApplicationManager();
LOGGER.info("loading application(s) ...");
appManager.loadSingleApp(descriptor);
startNetworkLayer();
Thread.currentThread().setContextClassLoader(Act.class.getClassLoader());
App app = app();
if (null == app) {
shutdownNetworkLayer();
throw new UnexpectedException("App not found. Please make sure your app start directory is correct");
}
emit(SysEventId.ACT_START);
writePidFile();
int port = httpPort();
String urlContext = appConfig().urlContext();
if (null == urlContext) {
LOGGER.info("app is ready at: http://%s:%s", getLocalIpAddr(), port);
} else {
LOGGER.info("app is ready at: http://%s:%s%s", getLocalIpAddr(), port, urlContext);
}
}
public static void shutdown(final App app) {
shutdown(app, 0);
}
public static void shutdown(final App app, final int exitCode) {
if (null == appManager) {
return;
}
new Thread() {
@Override
public void run() {
if (!appManager.unload(app)) {
app.destroy();
}
shutdownAct(exitCode);
}
}.start();
}
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();
NetworkHandler networkHandler = new NetworkHandler(app);
network.register(port, false, networkHandler);
if (app.config().supportSsl()) {
network.register(appConfig().httpsPort(), true, networkHandler);
}
List<NamedPort> portList = app.config().namedPorts();
for (NamedPort np : portList) {
network.register(np.port(), false, 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 = new ArrayList<>();
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();
}
/**
* Returns the current {@link App application's} crypto service
*
* @return an {@link AppCrypto} instance
*/
public static AppCrypto crypto() {
return app().crypto();
}
/**
* Return the {@link App} instance
*
* @return the App instance
*/
public static App app() {
return App.instance();
}
/**
* Returns the app version
*
* @return
* the app version
*/
public static Version appVersion() {
return app().version();
}
/**
* 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.
*
* This method is deprecated. Please use {@link #getInstance(Class)} instead
*
* @param singletonClass
* @param <T>
* @return the singleton instance
*/
@Deprecated
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 SysEventId App event}
*
* @param sysEvent the app event
*/
public static void emit(SysEventId sysEvent) {
App.instance().emit(sysEvent);
}
/**
* Alias of {@link #emit(SysEventId)}
*
* @param sysEventId the app event
*/
public static void trigger(SysEventId sysEventId) {
emit(sysEventId);
}
/**
* 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 JobManager}
*
* @return the app's jobManager
*/
public static JobManager jobManager() {
return App.instance().jobManager();
}
/**
* Returns the {@link App app}'s {@link DependencyInjector}
*
* @param <DI> the generic type of injector
* @return the app's injector
*/
public static <DI extends DependencyInjector> DI injector() {
return App.instance().injector();
}
/**
* Call {@link App#classForName(String)} method on the current {@link #app() app instance}
*
* @param className the class name
* @return the class corresponding to the name specified
*/
public static Class<?> appClassForName(String className) {
return app().classForName(className);
}
/**
* 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 getInstance(String className) {
return app().getInstance(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 getInstance(Class<? extends T> clz) {
return app().getInstance(clz);
}
/**
* Find a resource by given name.
*
* This call delegate to {@link App#getResource(String)} on
* {@link #app()}.
*
* @param name
* the resource name
* @return the `URL` if found, or `null` if resource not found
*/
public static URL getResource(String name) {
return app().getResource(name);
}
/**
* Returns an input stream for reading the specified resource.
*
* This will call {@link App#getResourceAsStream(String)} on
* {@link #app()}.
*
* @param name
* The resource name
* @return the input stream to the resource or `null` if resource not found
*/
public static InputStream getResourceAsStream(String name) {
return app().getResourceAsStream(name);
}
// --- Spark style API for application to hook action handler to a certain http request endpoint
public static void get(String url, SimpleRequestHandler handler) {
get(url, RequestHandlerBase.wrap(handler));
}
public static void getNonblock(String url, SimpleRequestHandler handler) {
get(url, RequestHandlerBase.wrap(handler).setExpress());
}
public static void post(String url, SimpleRequestHandler handler) {
post(url, RequestHandlerBase.wrap(handler));
}
public static void put(String url, SimpleRequestHandler handler) {
put(url, RequestHandlerBase.wrap(handler));
}
public static void delete(String url, SimpleRequestHandler handler) {
delete(url, RequestHandlerBase.wrap(handler));
}
public static void get(String url, RequestHandlerBase handler) {
app().router().addMapping(H.Method.GET, url, handler, RouteSource.APP_CONFIG);
}
public static void post(String url, RequestHandlerBase handler) {
app().router().addMapping(H.Method.POST, url, handler, RouteSource.APP_CONFIG);
}
public static void put(String url, RequestHandlerBase handler) {
app().router().addMapping(H.Method.PUT, url, handler, RouteSource.APP_CONFIG);
}
public static void delete(String url, RequestHandlerBase handler) {
app().router().addMapping(H.Method.DELETE, url, handler, RouteSource.APP_CONFIG);
}
// --- ActFramework entry methods
/**
* Start Act application
*
* @throws Exception
* any exception raised during app start
*/
public static void start() throws Exception {
bootstrap(AppDescriptor.of(getCallerClass()));
}
/**
* Start Act application with specified app name
*
* @param appName
* the app name, optional
* @throws Exception
* any exception thrown out
*/
public static void start(final String appName) throws Exception {
bootstrap(AppDescriptor.of(appName, getCallerClass()));
}
/**
* Start Act application with specified app name and scan package.
*
* If there are multiple packages, they should be joined in a single string
* by comma `,`. And the first package name will be used to explore the
* `.version` file in the class path
*
* @param appName
* the app name, optional
* @param scanPackage
* the scan package
* @throws Exception
* any exception raised during act start up
*/
public static void start(String appName, String scanPackage) throws Exception {
bootstrap(AppDescriptor.of(appName, scanPackage));
}
/**
* Start Act application with specified name and scan package specified by a class
*
* @param appName
* the app name
* @param anyAppClass
* specifies the scan package
* @throws Exception
* any exception raised during act start up
*/
public static void start(String appName, Class<?> anyAppClass) throws Exception {
bootstrap(AppDescriptor.of(appName, anyAppClass));
}
/**
* Start Act application with scan package specified by a class
*
* @param anyAppClass
* specifies the scan package
* @throws Exception
* any exception raised during act start up
*/
public static void start(Class<?> anyAppClass) throws Exception {
bootstrap(AppDescriptor.of(anyAppClass));
}
/**
* Start Act application with specified app name, app version and
* scan page via an app class
*
* @param appName
* the app name
* @param anyAppClass
* specifies the scan package
* @param appVersion
* the app version tag
* @throws Exception
* any exception raised during act start up
*/
public static void start(String appName, Class<?> anyAppClass, Version appVersion) throws Exception {
bootstrap(AppDescriptor.of(appName, anyAppClass, appVersion));
}
/**
* Start Act application with specified app name, app version and scan package
*
* @param appName
* the app name
* @param scanPackage
* the scan package, the package could be separated by {@link Constants#LIST_SEPARATOR}
* @param appVersion
* the app version tag
* @throws Exception any exception raised during act start up
*/
public static void start(String appName, String scanPackage, Version appVersion) throws Exception {
bootstrap(AppDescriptor.of(appName, scanPackage, appVersion));
}
static int classCacheSize() {
return ((FullStackAppBootstrapClassLoader) Act.class.getClassLoader()).libBCSize();
}
private static String appJar() {
URL url = Act.class.getClassLoader().getResource("META-INF/MANIFEST.MF");
if (null != url) {
try {
Manifest manifest = new Manifest(url.openStream());
Attributes attributes = manifest.getMainAttributes();
if (null != attributes) {
return attributes.getValue(ATTR_APP_JAR);
}
} catch (IOException e) {
LOGGER.warn(e, "cannot open manifest resource: %s", url);
}
}
return null;
}
private static void processEnvironment(AppDescriptor descriptor) {
boolean traceEnabled = LOGGER.isTraceEnabled();
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
List<String> cp = S.fastSplit(runtimeMXBean.getClassPath(), File.pathSeparator);
boolean hasClassesDir = false;
String artifactId = descriptor.getVersion().getArtifactId();
String appJar = appJar();
String appJarFile = null;
for (String cpItem : cp) {
if (!cpItem.endsWith(".jar")) {
if (traceEnabled) {
LOGGER.trace("found class path that are not jar file: %s", cpItem);
}
hasClassesDir = true;
break;
} else if (null == appJarFile) {
if (null != appJar && cpItem.contains(appJar)) {
appJarFile = cpItem;
} else if (cpItem.contains(artifactId)) {
appJarFile = cpItem;
}
}
}
String s = System.getProperty("app.mode");
if (!hasClassesDir) {
mode = Mode.PROD;
if (null != appJarFile) {
System.setProperty(PROP_APP_JAR_FILE, appJarFile);
}
} else {
if (null != s) {
mode = Mode.valueOfIgnoreCase(s);
if (traceEnabled) {
LOGGER.trace("set app mode to user specified: %s", s);
}
} else {
String profile = SysProps.get(AppConfigKey.PROFILE.key());
if (S.eq("prod", profile, S.IGNORECASE)) {
mode = Mode.PROD;
if (traceEnabled) {
LOGGER.trace("set app mode to prod based on profile setting");
}
} else if (S.eq("dev", profile, S.IGNORECASE)) {
mode = Mode.DEV;
if (traceEnabled) {
LOGGER.trace("set app mode to dev based on profile setting");
}
} else {
mode = hasClassesDir ? Mode.DEV : Mode.PROD;
if (traceEnabled) {
LOGGER.trace("set app mode to system determined: %s", mode);
}
}
}
}
s = System.getProperty("app.nodeGroup");
if (null != s) {
nodeGroup = s;
if (traceEnabled) {
LOGGER.trace("set node group to %s", s);
}
}
}
public static void registerTypeConverters() {
TypeConverterRegistry.INSTANCE.register(new $.TypeConverter<ReadableInstant, Long>() {
@Override
public Long convert(ReadableInstant o) {
return o.getMillis();
}
}).register(new $.TypeConverter<Long, DateTime>() {
@Override
public DateTime convert(Long o) {
return new DateTime().withMillis(o);
}
}).register(new $.TypeConverter<DateTime, LocalDateTime>() {
@Override
public LocalDateTime convert(DateTime o) {
return o.toLocalDateTime();
}
}).register(new $.TypeConverter<DateTime, LocalDate>() {
@Override
public LocalDate convert(DateTime o) {
return o.toLocalDate();
}
}).register(new $.TypeConverter<DateTime, LocalTime>() {
@Override
public LocalTime convert(DateTime o) {
return o.toLocalTime();
}
})
;
}
private static void loadConfig() {
LOGGER.debug("loading configuration ...");
String s = SysProps.get("act.mode");
if (null != s) {
mode = Mode.valueOfIgnoreCase(s);
}
LOGGER.debug("Act starts in %s mode", mode);
conf = new ActConfLoader().load(null);
}
private static void unloadConfig() {
conf = null;
}
private static void loadPlugins() {
LOGGER.debug("scanning plugins ...");
long ts = $.ms();
new PluginScanner().scan();
LOGGER.debug("plugin scanning finished in %sms", $.ms() - ts);
}
private static void unloadPlugins() {
new PluginScanner().unload();
}
private static void initViewManager() {
LOGGER.debug("initializing view manager ...");
viewManager = new ViewManager();
}
private static void destroyViewManager() {
if (null != viewManager) {
viewManager.destroy();
viewManager = null;
}
}
private static void initMetricPlugin() {
LOGGER.debug("initializing metric plugin ...");
metricPlugin = new SimpleMetricPlugin();
}
private static void destroyMetricPlugin() {
if (null != metricPlugin) {
tryDestroy(metricPlugin);
metricPlugin = null;
}
}
private static void initPluginManager() {
LOGGER.debug("initializing generic plugin manager ...");
pluginManager = new GenericPluginManager();
}
private static void destroyPluginManager() {
if (null != pluginManager) {
pluginManager.destroy();
pluginManager = null;
}
}
private static void initAppServicePluginManager() {
LOGGER.debug("initializing app service plugin manager ...");
appPluginManager = new AppServicePluginManager();
}
private static void destroyAppServicePluginManager() {
if (null != appPluginManager) {
appPluginManager.destroy();
appPluginManager = null;
}
}
private static void initDbManager() {
LOGGER.debug("initializing db manager ...");
dbManager = new DbManager();
}
private static void destroyDbManager() {
if (null != dbManager) {
dbManager.destroy();
dbManager = null;
}
}
private static void initAppCodeScannerPluginManager() {
LOGGER.debug("initializing app code scanner plugin manager ...");
scannerPluginManager = new AppCodeScannerPluginManager();
}
private static void destroyAppCodeScannerPluginManager() {
if (null != scannerPluginManager) {
scannerPluginManager.destroy();
scannerPluginManager = null;
}
}
static void initEnhancerManager() {
LOGGER.debug("initializing byte code enhancer manager ...");
enhancerManager = new BytecodeEnhancerManager();
}
private static void destroyEnhancerManager() {
if (null != enhancerManager) {
enhancerManager.destroy();
enhancerManager = null;
}
}
private static void initNetworkLayer() {
LOGGER.debug("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.debug("starting network layer ...");
network.start();
}
private static void shutdownNetworkLayer() {
LOGGER.debug("shutting down network layer ...");
network.shutdown();
}
protected static void initApplicationManager() {
LOGGER.debug("initializing application manager ...");
appManager = AppManager.create();
}
private static Class<?> getCallerClass() {
StackTraceElement[] sa = new RuntimeException().getStackTrace();
E.unexpectedIf(sa.length < 3, "Whoops!");
StackTraceElement ste = sa[2];
String className = ste.getClassName();
if ("act.Act$start".equals(className)) {
// groovy launched app
ste = sa[6];
className = ste.getClassName();
}
E.unexpectedIf(!className.contains("."), "The main class must have package name to use Act");
return $.classForName(className);
}
private static void bootstrap(AppDescriptor appDescriptor) throws Exception {
long ts = $.ms();
String profile = SysProps.get(AppConfigKey.PROFILE.key());
if (S.blank(profile)) {
profile = "";
} else {
profile = "using profile[" + profile + "]";
}
String packageName = appDescriptor.getPackageName();
LOGGER.debug("run fullstack application with package[%s] %s", packageName, profile);
final String SCAN_PACKAGE = AppConfigKey.SCAN_PACKAGE_SYS.key();
if (S.notBlank(packageName)) {
System.setProperty(SCAN_PACKAGE, packageName);
}
FullStackAppBootstrapClassLoader classLoader = new FullStackAppBootstrapClassLoader(RunApp.class.getClassLoader());
Thread.currentThread().setContextClassLoader(classLoader);
Class<?> actClass = classLoader.loadClass("act.Act");
Method m = actClass.getDeclaredMethod("startup", byte[].class);
m.setAccessible(true);
$.invokeStatic(m, appDescriptor.toByteArray());