blob: 03110782d238442bae0b405c878969167ef152d6 (
plain) (
blame)
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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.core;
import org.apache.commons.daemon.Daemon;
import org.apache.commons.daemon.DaemonContext;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
*/
public class BootstrapDaemon implements Daemon {
private static final Logger log = Logger.getLogger(BootstrapDaemon.class.getName());
private final BootstrapLoader loader;
private final boolean privileged;
private String bundleLocation;
static {
// force load slf4j to avoid other logging frameworks from initializing before it
org.slf4j.LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
}
public BootstrapDaemon() {
this(new ApplicationLoader(Main.newOsgiFramework(), Main.newConfigModule()),
Boolean.valueOf(System.getProperty("jdisc.privileged")));
}
BootstrapDaemon(BootstrapLoader loader, boolean privileged) {
this.loader = loader;
this.privileged = privileged;
}
BootstrapLoader loader() {
return loader;
}
@Override
public void init(DaemonContext context) throws Exception {
String[] args = context.getArguments();
if (args == null || args.length != 1 || args[0] == null) {
throw new IllegalArgumentException("Expected 1 argument, got " + Arrays.toString(args) + ".");
}
bundleLocation = args[0];
if (privileged) {
log.finer("Initializing application with privileges.");
loader.init(bundleLocation, true);
}
}
@Override
public void start() throws Exception {
try {
if (!privileged) {
log.finer("Initializing application without privileges.");
loader.init(bundleLocation, false);
}
loader.start();
} catch (Exception e) {
try {
log.log(Level.SEVERE, "Failed starting container", e);
}
finally {
Runtime.getRuntime().halt(1);
}
}
}
@Override
public void stop() throws Exception {
loader.stop();
}
@Override
public void destroy() {
loader.destroy();
}
}
|