-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathSirius.java
643 lines (569 loc) · 23.6 KB
/
Sirius.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
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
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.kernel;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import sirius.kernel.async.Future;
import sirius.kernel.async.Operation;
import sirius.kernel.async.Tasks;
import sirius.kernel.commons.Strings;
import sirius.kernel.commons.Value;
import sirius.kernel.commons.Watch;
import sirius.kernel.di.Injector;
import sirius.kernel.di.std.Part;
import sirius.kernel.di.std.PriorityParts;
import sirius.kernel.health.Exceptions;
import sirius.kernel.health.Log;
import sirius.kernel.nls.NLS;
import sirius.kernel.settings.ExtendedSettings;
import javax.annotation.Nullable;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.net.URLConnection;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Loads and initializes the framework.
* <p>
* This can be considered the <tt>stage2</tt> when booting the framework, as it is responsible to discover and
* initialize all components. Call {@link #start(Setup)} to initialize the framework.
* <p>
* To make a jar or other classpath-root visible to SIRIUS an empty file called "component.marker" must be placed in
* its root directory.
*/
public class Sirius {
/**
* Contains the name of the system property which is used to select which scenario to execute.
*/
public static final String SIRIUS_TEST_SCENARIO_PROPERTY = "SIRIUS_TEST_SCENARIO";
private static final String CONFIG_KEY_CUSTOMIZATIONS = "sirius.customizations";
private static final String SEPARATOR_LINE = "---------------------------------------------------------";
private static Setup setup;
private static Config config;
private static ExtendedSettings settings;
private static Map<String, Boolean> frameworks = new HashMap<>();
private static List<String> customizations = new ArrayList<>();
private static Classpath classpath;
private static volatile boolean started = false;
private static volatile boolean initialized = false;
private static final long START_TIMESTAMP = System.currentTimeMillis();
protected static final Log LOG = Log.get("sirius");
private static final String CONFIG_INSTANCE = "instance";
@PriorityParts(Startable.class)
private static List<Startable> lifecycleStartParticipants;
@PriorityParts(Stoppable.class)
private static List<Stoppable> lifecycleStopParticipants;
@PriorityParts(Killable.class)
private static List<Killable> lifecycleKillParticipants;
@Part
private static Tasks tasks;
private Sirius() {
}
/**
* Determines if the framework is running in development mode.
*
* @return {@code true} if the framework runs in development mode, {@code false} otherwise.
*/
public static boolean isDev() {
return setup != null && setup.getMode() == Setup.Mode.DEVELOP;
}
/**
* Determines if the framework is running in test mode.
*
* @return {@code true} if the framework runs in test mode, {@code false} otherwise.
*/
public static boolean isTest() {
return setup != null && setup.getMode() == Setup.Mode.TEST;
}
/**
* Determines if the framework is running in staging mode.
*
* @return {@code true} if the framework runs in staging mode, {@code false} otherwise
*/
public static boolean isStaging() {
return setup != null && setup.getMode() == Setup.Mode.STAGING;
}
/**
* Determines if the framework was started as test run (JUNIT or the like).
*
* @return <tt>true</tt> if the framework was started as test, <tt>false</tt> otherwise
*/
public static boolean isStartedAsTest() {
return setup != null && setup.getMode() == Setup.Mode.TEST;
}
/**
* Determines if the framework is running in production mode.
*
* @return {@code true} if the framework runs in production mode, {@code false} otherwise.
*/
public static boolean isProd() {
return setup != null && setup.getMode() == Setup.Mode.PROD;
}
/**
* Determines if the framework is up and running.
* <p>
* This flag will be set to <tt>true</tt> once the framework is being setup and will be immediately
* set to <tt>false</tt> one the framework starts to shut down.
*
* @return <tt>true</tt> once the framework is setup and running and not shutting down yet.
* @see Tasks#isRunning() Provides a similar flag with slightly different semantics
*/
public static boolean isRunning() {
return started;
}
/*
* Once the configuration is loaded, this method applies the log level to all log4j and java.util.logging
* loggers
*/
private static void setupLogLevels() {
if (config.hasPath("log")) {
LOG.WARN("Found 'log' in the system configuration - use 'logging' to configure loggers!");
}
if (config.hasPath("logger")) {
LOG.WARN("Found 'logger' in the system configuration - use 'logging' to configure loggers!");
}
if (config.hasPath("logs")) {
LOG.WARN("Found 'logs' in the system configuration - use 'logging' to configure loggers!");
}
if (!config.hasPath("logging")) {
LOG.INFO("No 'logger' section in the system config - using defaults...");
return;
}
LOG.INFO("Initializing the log system:");
Config logging = config.getConfig("logging");
for (Map.Entry<String, com.typesafe.config.ConfigValue> entry : logging.entrySet()) {
LOG.INFO("* Setting %s to: %s", entry.getKey(), logging.getString(entry.getKey()));
Log.setLevel(entry.getKey(), Log.parseLevel(logging.getString(entry.getKey())));
}
}
/*
* Scans the system config (sirius.frameworks) and determines which frameworks are enabled. This will affect
* which classes are loaded into the component model.
*/
private static void setupFrameworks() {
Config frameworkConfig = config.getConfig("sirius.frameworks");
Map<String, Boolean> frameworkStatus = new HashMap<>();
int total = 0;
int numEnabled = 0;
LOG.DEBUG_INFO("Scanning framework status (sirius.frameworks):");
for (Map.Entry<String, com.typesafe.config.ConfigValue> entry : frameworkConfig.entrySet()) {
String framework = entry.getKey();
try {
boolean enabled = Value.of(entry.getValue().unwrapped()).asBoolean(false);
frameworkStatus.put(framework, enabled);
total++;
numEnabled += enabled ? 1 : 0;
LOG.DEBUG_INFO(Strings.apply(" * %s: %b", framework, enabled));
} catch (Exception exception) {
Exceptions.ignore(exception);
LOG.WARN("Cannot convert status '%s' of framework '%s' to a boolean! Framework will be disabled.",
entry.getValue().render(),
framework);
frameworkStatus.put(framework, false);
}
}
LOG.INFO("Enabled %d of %d frameworks...", numEnabled, total);
// Although customizations are loaded in setupConfiguration, we output the status here,
// as this seems more intiutive for the customer (the poor guy reading the logs...)
LOG.INFO("Active Customizations: %s", customizations);
frameworks = frameworkStatus;
}
/*
* Starts all framework components
*/
private static void startComponents() {
if (started) {
stop();
}
boolean startFailed = false;
for (final Startable lifecycle : lifecycleStartParticipants) {
Future future = tasks.defaultExecutor().fork(() -> startLifecycle(lifecycle));
if (!future.await(Duration.ofMinutes(1))) {
LOG.WARN("Lifecycle '%s' did not start within one minute....", lifecycle.getClass().getName());
startFailed = true;
}
}
if (startFailed) {
outputActiveOperations();
}
started = true;
}
private static void startLifecycle(Startable lifecycle) {
LOG.INFO("Starting: %s", lifecycle.getClass().getName());
try {
lifecycle.started();
} catch (Exception exception) {
Exceptions.handle()
.error(exception)
.to(LOG)
.withSystemErrorMessage("Startup of: %s failed!", lifecycle.getClass().getName())
.handle();
}
}
/**
* Discovers all components in the class path and initializes the {@link Injector}
*/
private static void init() {
if (initialized) {
return;
}
initialized = true;
setupLocalConfig();
setupClasspath();
setupApplicationAndSystemConfig();
LOG.INFO(SEPARATOR_LINE);
// Setup log-system based on configuration
setupLogLevels();
// Output enabled frameworks...
setupFrameworks();
// Setup native language support
NLS.init(classpath);
// Initialize dependency injection...
Injector.init(classpath);
startComponents();
// Start resource monitoring...
NLS.startMonitoring(classpath);
}
/**
* Loads the local configuration.
* <p>
* These are <tt>settings.conf</tt>, <tt>develop.conf</tt>, <tt>test.conf</tt>
* and finally, <tt>instance.conf</tt>.
*/
protected static void setupLocalConfig() {
LOG.INFO("Loading local config...");
LOG.INFO(SEPARATOR_LINE);
config = ConfigFactory.empty();
Config instanceConfig = null;
if (isStartedAsTest()) {
config = setup.applyTestConfig(config);
config = setup.applyTestScenarioConfig(System.getProperty(SIRIUS_TEST_SCENARIO_PROPERTY), config);
} else {
// instance.conf and develop.conf are not used to tests to permit uniform behaviour on local
// machines and build servers...
config = setup.applyConfig(config, setup.getMode().toString().toLowerCase());
instanceConfig = setup.applyConfig(config, CONFIG_INSTANCE);
}
// Setup customer customizations...
if (instanceConfig != null && instanceConfig.hasPath(CONFIG_KEY_CUSTOMIZATIONS)) {
customizations = instanceConfig.getStringList(CONFIG_KEY_CUSTOMIZATIONS);
} else if (config.hasPath(CONFIG_KEY_CUSTOMIZATIONS)) {
customizations = config.getStringList(CONFIG_KEY_CUSTOMIZATIONS);
}
// Load settings.conf for customizations...
for (String conf : customizations) {
if (Sirius.class.getResource("/customizations/" + conf + "/settings.conf") != null) {
LOG.INFO("loading settings.conf for customization '" + conf + "'");
String configName = "customizations/" + conf + "/settings.conf";
try {
config = ConfigFactory.parseResources(setup.getLoader(), configName).withFallback(config);
} catch (Exception exception) {
handleConfigError(configName, exception);
}
} else {
LOG.INFO("customization '" + conf + "' has no settings.conf...");
}
}
// Apply instance config at last for override all other configs...
if (instanceConfig != null) {
config = instanceConfig.withFallback(config);
}
// Apply environment settings last, as these are often used in docker(-compose) setups
config = setup.applyEnvironment(config);
LOG.INFO(SEPARATOR_LINE);
}
/**
* Loads the application config files.
* <p>
* These are <tt>component-XXX.conf</tt>, <tt>component-test-XXX.conf</tt>, <tt>application-XXX.conf</tt>
* and finally, <tt>application.conf</tt>.
*/
private static void setupApplicationAndSystemConfig() {
LOG.INFO("Loading system config...");
LOG.INFO(SEPARATOR_LINE);
config = config.withFallback(setup.loadApplicationConfig());
if (isStartedAsTest()) {
// Load test configurations (will override component configs)
classpath.find(Pattern.compile("component-test-([^.]*?)\\.conf"))
.map(Matcher::group)
.sorted(Comparator.reverseOrder())
.forEach(configPath -> {
try {
LOG.INFO("Loading test config: %s", configPath);
config = config.withFallback(ConfigFactory.parseResources(setup.getLoader(), configPath));
} catch (Exception exception) {
handleConfigError(configPath, exception);
}
});
}
// Load component configurations
classpath.find(Pattern.compile("component-([^\\-]*?)([^.]*?)\\.conf"))
.map(Matcher::group)
.filter(configPath -> !"test".equals(configPath))
.sorted(Comparator.reverseOrder())
.forEach(configPath -> {
try {
LOG.INFO("Loading config: %s", configPath);
config = config.withFallback(ConfigFactory.parseResources(setup.getLoader(), configPath));
} catch (Exception exception) {
handleConfigError(configPath, exception);
}
});
config = config.resolve();
LOG.INFO(SEPARATOR_LINE);
}
private static void setupClasspath() {
if (Sirius.isDev()) {
// in a local dev environment we disable caching jar connections to hotswap libs especially templates
URLConnection.setDefaultUseCaches("jar", false);
}
classpath = new Classpath(setup.getLoader(), "component.marker", customizations);
classpath.getComponentRoots().forEach(url -> LOG.INFO("Classpath: %s", url));
}
private static void handleConfigError(String file, Exception exception) {
Exceptions.ignore(exception);
Sirius.LOG.WARN("Cannot load %s: %s", file, exception.getMessage());
}
/**
* Provides access to the classpath used to load the framework.
*
* @return the classpath used to load the framework
*/
public static Classpath getClasspath() {
return classpath;
}
/**
* Stops the framework.
*/
public static void stop() {
if (!started) {
return;
}
started = false;
LOG.INFO("Stopping Sirius");
LOG.INFO(SEPARATOR_LINE);
outputActiveOperations();
stopLifecycleParticipants();
outputActiveOperations();
waitForLifecycleParticipants();
outputThreadState();
initialized = false;
settings = null;
}
private static void outputThreadState() {
LOG.INFO("System halted! - Thread State");
LOG.INFO(SEPARATOR_LINE);
LOG.INFO("%-15s %10s %53s", "STATE", "ID", "NAME");
for (ThreadInfo info : ManagementFactory.getThreadMXBean().dumpAllThreads(false, false)) {
LOG.INFO("%-15s %10s %53s", info.getThreadState().name(), info.getThreadId(), info.getThreadName());
}
LOG.INFO(SEPARATOR_LINE);
}
private static void waitForLifecycleParticipants() {
LOG.INFO("Awaiting system halt...");
LOG.INFO(SEPARATOR_LINE);
for (int i = lifecycleKillParticipants.size() - 1; i >= 0; i--) {
Killable killable = lifecycleKillParticipants.get(i);
try {
Watch w = Watch.start();
killable.awaitTermination();
LOG.INFO("Terminated: %s (Took: %s)", killable.getClass().getName(), w.duration());
} catch (Exception exception) {
Exceptions.handle()
.error(exception)
.to(LOG)
.withSystemErrorMessage("Termination of: %s failed!", killable.getClass().getName())
.handle();
}
}
}
private static void stopLifecycleParticipants() {
LOG.INFO("Stopping lifecycles...");
LOG.INFO(SEPARATOR_LINE);
for (int i = lifecycleStopParticipants.size() - 1; i >= 0; i--) {
Stoppable stoppable = lifecycleStopParticipants.get(i);
Future future = tasks.defaultExecutor().fork(() -> stopLifecycle(stoppable));
if (!future.await(Duration.ofSeconds(10))) {
LOG.WARN("Lifecycle '%s' did not stop within 10 seconds....", stoppable.getClass().getName());
}
}
LOG.INFO(SEPARATOR_LINE);
}
private static void stopLifecycle(Stoppable lifecycle) {
LOG.INFO("Stopping: %s", lifecycle.getClass().getName());
try {
lifecycle.stopped();
} catch (Exception exception) {
Exceptions.handle()
.error(exception)
.to(LOG)
.withSystemErrorMessage("Stop of: %s failed!", lifecycle.getClass().getName())
.handle();
}
}
private static void outputActiveOperations() {
if (!Operation.getActiveOperations().isEmpty()) {
LOG.INFO("Active Operations");
LOG.INFO(SEPARATOR_LINE);
for (Operation op : Operation.getActiveOperations()) {
LOG.INFO(op.toString());
}
LOG.INFO(SEPARATOR_LINE);
}
}
/**
* Initializes the framework.
* <p>
* This is called by <tt>IPL.main</tt> once the class loader is fully populated.
*
* @param setup the setup class used to configure the framework
*/
public static void start(Setup setup) {
Watch w = Watch.start();
Sirius.setup = setup;
setup.init();
LOG.INFO(SEPARATOR_LINE);
LOG.INFO("System is STARTING...");
LOG.INFO(SEPARATOR_LINE);
init();
LOG.INFO(SEPARATOR_LINE);
LOG.INFO("System is UP and RUNNING - %s", w.duration());
LOG.INFO(SEPARATOR_LINE);
Runtime.getRuntime().addShutdownHook(new Thread(Sirius::stop));
}
/**
* Determines if the framework with the given name is enabled.
* <p>
* Frameworks can be enabled or disabled using the config path <tt>sirius.framework.[name]</tt>. This is
* intensively used by the app part, as it provides a lot of basic frameworks which can be turned off or
* on as required.
*
* @param framework the framework to check
* @return <tt>true</tt> if the framework is enabled, <tt>false</tt> otherwise
*/
public static boolean isFrameworkEnabled(String framework) {
if (Strings.isEmpty(framework)) {
return true;
}
if (Sirius.isDev() && !frameworks.containsKey(framework)) {
LOG.WARN("Status of unknown framework '%s' requested. Will report as disabled framework.", framework);
}
return Boolean.TRUE.equals(frameworks.get(framework));
}
/**
* Returns a list of all active customer configurations.
* <p>
* A customer configuration can be used to override basic functionality with more specialized classes or resources
* which were adapted based on customer needs.
* <p>
* As often groups of customers share the same requirements, not only a single configuration can be activated
* but a list. Within this list each configuration may override classes and resources of all former
* configurations. Therefore the last configuration will always "win".
* <p>
* Note that classes must be placed in the package: <b>configuration.[name]</b> (with arbitrary sub packages).
* Also resources must be placed in: <b>configuration/[name]/resource-path</b>.
*
* @return a list of all active configurations
*/
public static List<String> getActiveConfigurations() {
return Collections.unmodifiableList(customizations);
}
/**
* Determines if the given config is active (or null which is considered active)
*
* @param configName the name of the config to check. <tt>null</tt> will be considered as active
* @return <tt>true</tt> if the named customization is active, <tt>false</tt> otherwise
*/
public static boolean isActiveCustomization(@Nullable String configName) {
return configName == null || Sirius.getActiveConfigurations().contains(configName);
}
/**
* Determines if the given resource is part of a customization.
*
* @param resource the resource path to check
* @return <tt>true</tt> if the given resource is part of a customization, <tt>false</tt> otherwise
*/
public static boolean isCustomizationResource(@Nullable String resource) {
return resource != null && resource.startsWith("customizations");
}
/**
* Extracts the customization name from a resource.
* <p>
* Valid names are paths like "customizations/[name]/..." or classes like "customizations.[name]...".
*
* @param resource the name of the resource
* @return the name of the customizations or <tt>null</tt> if no config name is contained
*/
@Nullable
public static String getCustomizationName(@Nullable String resource) {
if (resource == null) {
return null;
}
if (resource.startsWith("customizations/")) {
return Strings.split(resource.substring(15), "/").getFirst();
} else if (resource.startsWith("customizations.")) {
return Strings.split(resource.substring(15), ".").getFirst();
} else {
return null;
}
}
/**
* Compares the two given customizations according to the order given in the system config.
*
* @param configA the name of the first customization
* @param configB the name of the second customization
* @return an int value which can be used to compare the order of the two given configs.
*/
public static int compareCustomizations(@Nullable String configA, @Nullable String configB) {
if (configA == null) {
if (configB == null) {
return 0;
}
return 1;
}
if (configB == null) {
return -1;
}
return customizations.indexOf(configA) - customizations.indexOf(configB);
}
/**
* Returns the system config based on the current instance.conf (file system), application.conf (classpath) and
* all component-XXX.conf wrapped as <tt>Settings</tt>
*
* @return the initialized settings object or <tt>null</tt> if the framework is not setup yet.
*/
public static ExtendedSettings getSettings() {
if (settings == null && config != null) {
settings = new ExtendedSettings(config, true);
}
return settings;
}
/**
* Provides access to the setup instance which was used to configure Sirius.
*
* @return the setup instance used to configure Sirius.+
*/
public static Setup getSetup() {
return setup;
}
/**
* Returns the up time of the system in milliseconds.
*
* @return the number of milliseconds the system is running
*/
public static long getUptimeInMilliseconds() {
return System.currentTimeMillis() - START_TIMESTAMP;
}
}