com.sshtools.commands.AbstractJadaptiveCommand Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of command-utils Show documentation
Show all versions of command-utils Show documentation
Utility classes for creating SSH enabled commands with Maverick Synergy
The newest version!
package com.sshtools.commands;
import java.text.MessageFormat;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.prefs.Preferences;
import com.sshtools.jaul.AppRegistry;
import com.sshtools.jaul.AppRegistry.App;
import com.sshtools.jaul.Phase;
import com.sshtools.jaul.UpdateService;
import com.sshtools.sequins.Terminal;
import picocli.CommandLine.Command;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Option;
import picocli.CommandLine.Spec;
@Command
public abstract class AbstractJadaptiveCommand implements Callable, JadaptiveCommand {
@Option(names = { "-X", "--verbose-exceptions" }, description = "Show full traces for errors.")
boolean verboseExceptions = false;
@Option(names = { "--jaul-register" }, hidden = true, description = "Register this application with the JADAPTIVE update system and exit. Usually only called on installation.")
boolean jaulRegister;
@Option(names = { "--jaul-deregister" }, hidden = true, description = "De-register this application from the JADAPTIVE update system and exit. Usually only called on uninstallation.")
boolean jaulDeregister;
@Spec
CommandSpec spec;
private Terminal terminal;
private UpdateService updateService;
private final Optional defaultPhase;
private Optional app = Optional.empty();
private ScheduledExecutorService scheduler;
protected AbstractJadaptiveCommand(Optional defaultPhase) {
this.defaultPhase = defaultPhase;
scheduler = Executors.newScheduledThreadPool(1);
}
public final Integer call() throws Exception {
if(jaulDeregister) {
AppRegistry.get().deregister(getClass());
return 0;
}
else if(jaulRegister) {
AppRegistry.get().register(getClass());
return 0;
}
else {
app = getApp();
return onCall();
}
}
protected abstract Integer onCall() throws Exception;
@Override
public final boolean isVerboseExceptions() {
return verboseExceptions;
}
@Override
public final Preferences getPreferences() {
return AppRegistry.getBestAppPreferences(app, this);
}
@Override
public Terminal getTerminal() {
if (terminal == null) {
terminal = Terminal.create();
}
return terminal;
}
@Override
public UpdateService getUpdateService() {
if (updateService == null)
updateService = UpdateService.deferrableUpdateService(getPreferences(), defaultPhase, app, getVersion(), getScheduler());
return updateService;
}
@Override
public ScheduledExecutorService getScheduler() {
return scheduler;
}
@Override
public final String getVersion() {
return spec.version()[0];
}
public Optional getApp() {
try {
return Optional.of(AppRegistry.get().launch(this.getClass()));
}
catch(Exception e) {
System.err.println(MessageFormat.format("Failed to determine app installation. No update features will be available, and application preferences root is now determined by the class name {0}. {1}", getClass().getName(), e.getMessage() == null ? "No message supplied." : e.getMessage()));
return Optional.empty();
}
}
}