in juneau-microservice/juneau-microservice-core/src/main/java/org/apache/juneau/microservice/Microservice.java [576:706]
protected Microservice(Builder builder) throws IOException, ParseException {
setInstance(this);
this.builder = builder.copy();
this.workingDir = builder.workingDir;
this.configName = builder.configName;
this.args = builder.args != null ? builder.args : new Args(new String[0]);
// --------------------------------------------------------------------------------
// Try to get the manifest file if it wasn't already set.
// --------------------------------------------------------------------------------
ManifestFile manifest = builder.manifest;
if (manifest == null) {
Manifest m = new Manifest();
// If running within an eclipse workspace, need to get it from the file system.
File f = resolveFile("META-INF/MANIFEST.MF");
if (f.exists() && f.canRead()) {
try (FileInputStream fis = new FileInputStream(f)) {
m.read(fis);
} catch (IOException e) {
throw new IOException("Problem detected in MANIFEST.MF. Contents below:\n"+read(f), e);
}
} else {
// Otherwise, read from manifest file in the jar file containing the main class.
URL url = getClass().getResource("META-INF/MANIFEST.MF");
if (url != null) {
try {
m.read(url.openStream());
} catch (IOException e) {
throw new IOException("Problem detected in MANIFEST.MF. Contents below:\n"+read(url.openStream()), e);
}
}
}
manifest = new ManifestFile(m);
}
ManifestFileVar.init(manifest);
this.manifest = manifest;
// --------------------------------------------------------------------------------
// Try to resolve the configuration if not specified.
// --------------------------------------------------------------------------------
Config config = builder.config;
Config.Builder configBuilder = builder.configBuilder.varResolver(builder.varResolver.build()).store(MemoryStore.DEFAULT);
if (config == null) {
ConfigStore store = builder.configStore;
FileStore cfs = workingDir == null ? FileStore.DEFAULT : FileStore.create().directory(workingDir).build();
for (String name : getCandidateConfigNames()) {
if (store != null) {
if (store.exists(name)) {
configBuilder.store(store).name(name);
break;
}
} else {
if (cfs.exists(name)) {
configBuilder.store(cfs).name(name);
break;
}
if (ClasspathStore.DEFAULT.exists(name)) {
configBuilder.store(ClasspathStore.DEFAULT).name(name);
break;
}
}
}
config = configBuilder.build();
}
this.config = config;
Config.setSystemDefault(this.config);
this.config.addListener(this);
//-------------------------------------------------------------------------------------------------------------
// Var resolver.
//-------------------------------------------------------------------------------------------------------------
this.varResolver = builder.varResolver.bean(Config.class, config).build();
// --------------------------------------------------------------------------------
// Initialize console commands.
// --------------------------------------------------------------------------------
this.consoleEnabled = ObjectUtils.firstNonNull(builder.consoleEnabled, config.get("Console/enabled").asBoolean().orElse(false));
if (consoleEnabled) {
Console c = System.console();
this.consoleReader = ObjectUtils.firstNonNull(builder.consoleReader, new Scanner(c == null ? new InputStreamReader(System.in) : c.reader()));
this.consoleWriter = ObjectUtils.firstNonNull(builder.consoleWriter, c == null ? new PrintWriter(System.out, true) : c.writer());
for (ConsoleCommand cc : builder.consoleCommands) {
consoleCommandMap.put(cc.getName(), cc);
}
for (String s : config.get("Console/commands").asStringArray().orElse(new String[0])) {
ConsoleCommand cc;
try {
cc = (ConsoleCommand)Class.forName(s).getDeclaredConstructor().newInstance();
consoleCommandMap.put(cc.getName(), cc);
} catch (Exception e) {
getConsoleWriter().println("Could not create console command '"+s+"', " + e.getLocalizedMessage());
}
}
consoleThread = new Thread("ConsoleThread") {
@Override /* Thread */
public void run() {
Scanner in = getConsoleReader();
PrintWriter out = getConsoleWriter();
out.println(messages.getString("ListOfAvailableCommands"));
for (ConsoleCommand cc : new TreeMap<>(getConsoleCommands()).values())
out.append("\t").append(cc.getName()).append(" -- ").append(cc.getInfo()).println();
out.println();
while (true) {
String line = null;
out.append("> ").flush();
line = in.nextLine();
Args args = new Args(line);
if (! args.isEmpty())
executeCommand(args, in, out);
}
}
};
consoleThread.setDaemon(true);
} else {
this.consoleReader = null;
this.consoleWriter = null;
this.consoleThread = null;
}
//-------------------------------------------------------------------------------------------------------------
// Other
//-------------------------------------------------------------------------------------------------------------
this.listener = builder.listener != null ? builder.listener : new BasicMicroserviceListener();
init();
}