use of com.biglybt.pif.update.UpdateManager in project BiglyBT by BiglySoftware.
the class UI method processArgs.
@Override
public String[] processArgs(CommandLine commands, String[] args) {
boolean showMainWindow = args.length == 0 || COConfigurationManager.getBooleanParameter("Activate Window On External Download");
boolean open = true;
if (commands.hasOption("closedown") || commands.hasOption("shutdown")) {
try {
UpdateManager um = core.getPluginManager().getDefaultPluginInterface().getUpdateManager();
UpdateInstaller[] installers = um.getInstallers();
for (UpdateInstaller installer : installers) {
installer.destroy();
}
} catch (Throwable e) {
}
UIFunctions uiFunctions = UIFunctionsManager.getUIFunctions();
if (uiFunctions != null) {
uiFunctions.dispose(false, false);
}
return null;
}
if (commands.hasOption("restart")) {
UIFunctions uiFunctions = UIFunctionsManager.getUIFunctions();
if (uiFunctions != null) {
uiFunctions.dispose(true, false);
}
return null;
}
if (commands.hasOption("share")) {
showMainWindow = true;
open = false;
}
if (commands.hasOption("open")) {
showMainWindow = true;
}
String[] rest = commands.getArgs();
for (int i = 0; i < rest.length; i++) {
String filename = rest[i];
File file = new File(filename);
boolean isURI;
if (!file.exists() && !isURI(filename)) {
String magnet_uri = UrlUtils.normaliseMagnetURI(filename);
isURI = magnet_uri != null;
if (isURI) {
filename = magnet_uri;
}
} else {
isURI = isURI(filename);
}
if (isURI) {
if (Logger.isEnabled())
Logger.log(new LogEvent(LOGID, "StartServer: args[" + i + "] handling as a URI: " + filename));
} else {
try {
if (!file.exists()) {
throw (new Exception("File '" + file + "' not found"));
}
filename = file.getCanonicalPath();
Logger.log(new LogEvent(LOGID, "StartServer: file = " + filename));
} catch (Throwable e) {
Logger.log(new LogAlert(LogAlert.REPEATABLE, LogAlert.AT_ERROR, "Failed to access torrent file '" + filename + "'. Ensure sufficient temporary " + "file space available (check browser cache usage)."));
}
}
boolean queued = false;
try {
this_mon.enter();
if (queueTorrents) {
queued_torrents.add(new Object[] { filename, Boolean.valueOf(open) });
queued = true;
}
} finally {
this_mon.exit();
}
if (!queued) {
handleFile(filename, open);
}
}
if (showMainWindow) {
showMainWindow();
}
return args;
}
use of com.biglybt.pif.update.UpdateManager in project BiglyBT by BiglySoftware.
the class PlatformManagerUnixPlugin method checkStartupScript.
/**
* @since 3.0.1.7
*/
private void checkStartupScript() {
COConfigurationManager.setIntDefault("unix.script.lastaskversion", -1);
int lastAskedVersion = COConfigurationManager.getIntParameter("unix.script.lastaskversion");
String sVersion = System.getProperty(SystemProperties.SYSPROP_SCRIPT_VERSION, "0");
int version = 0;
try {
version = Integer.parseInt(sVersion);
} catch (Throwable t) {
}
Pattern pat = Pattern.compile("SCRIPT_VERSION=([0-9]+)", Pattern.CASE_INSENSITIVE);
File oldFilePath;
String sScriptFile = System.getProperty(SystemProperties.SYSPROP_APP_SCRIPT, null);
if (sScriptFile != null && new File(sScriptFile).exists()) {
oldFilePath = new File(sScriptFile);
} else {
oldFilePath = new File(SystemProperties.getApplicationPath(), Constants.APP_NAME.toLowerCase());
if (!oldFilePath.exists()) {
return;
}
}
final String oldFilePathString = oldFilePath.getAbsolutePath();
String oldStartupScript;
try {
oldStartupScript = FileUtil.readFileAsString(oldFilePath, 65535, "utf8");
} catch (IOException e) {
oldStartupScript = "";
}
// the second restart message
if (version == 0) {
Matcher matcher = pat.matcher(oldStartupScript);
if (matcher.find()) {
String sScriptVersion = matcher.group(1);
try {
version = Integer.parseInt(sScriptVersion);
} catch (Throwable t) {
}
}
}
if (version <= lastAskedVersion) {
return;
}
InputStream stream = getClass().getResourceAsStream("startupScript");
try {
String startupScript = FileUtil.readInputStreamAsString(stream, 65535, "utf8");
Matcher matcher = pat.matcher(startupScript);
if (matcher.find()) {
String sScriptVersion = matcher.group(1);
int latestVersion = 0;
try {
latestVersion = Integer.parseInt(sScriptVersion);
} catch (Throwable t) {
}
if (latestVersion > version) {
boolean autoUpdateScript = oldStartupScript.indexOf("AUTOUPDATE_SCRIPT=1") > 0;
boolean bInformUserManual = true;
if (autoUpdateScript) {
if (version >= 1) {
UpdateManager um = PluginInitializer.getDefaultInterface().getUpdateManager();
UpdateInstaller installer = um.createInstaller();
installer.addResource("biglybt.new", new ByteArrayInputStream(startupScript.getBytes()));
String target = new File(installer.getInstallDir(), "biglybt").getAbsolutePath();
installer.addMoveAction("biglybt.new", target);
installer.addChangeRightsAction("+x", target);
bInformUserManual = false;
} else {
// overwrite!
try {
FileUtil.writeBytesAsFile(oldFilePathString, startupScript.getBytes());
Runtime.getRuntime().exec(new String[] { findCommand("chmod"), "+x", oldStartupScript });
bInformUserManual = false;
} catch (Throwable t) {
}
}
}
if (bInformUserManual) {
final String newFilePath = new File(SystemProperties.getApplicationPath(), "biglybt.new").getAbsolutePath();
FileUtil.writeBytesAsFile(newFilePath, startupScript.getBytes());
showScriptManualUpdateDialog(newFilePath, oldFilePathString, latestVersion);
} else {
showScriptAutoUpdateDialog();
}
}
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
try {
stream.close();
} catch (Throwable e) {
}
}
}
use of com.biglybt.pif.update.UpdateManager in project BiglyBT by BiglySoftware.
the class ConsoleInput method registerUpdateChecker.
protected void registerUpdateChecker() {
boolean check_at_start = COConfigurationManager.getBooleanParameter("update.start", true);
if (!check_at_start) {
return;
}
// we've got to disable the auto-update components as we're not using them (yet...)
PluginManager pm = core.getPluginManager();
pm.getPluginInstaller().addListener(new PluginInstallerListener() {
@Override
public boolean installRequest(String reason, InstallablePlugin plugin) throws PluginException {
out.println("Plugin installation request for '" + plugin.getName() + "' - " + reason);
String desc = plugin.getDescription();
String[] bits = desc.split("\n");
for (int i = 0; i < bits.length; i++) {
out.println("\t" + bits[i]);
}
return (true);
}
});
PluginInterface pi = pm.getPluginInterfaceByClass(CorePatchChecker.class);
if (pi != null) {
pi.getPluginState().setDisabled(true);
}
pi = pm.getPluginInterfaceByClass(UpdaterUpdateChecker.class);
if (pi != null) {
pi.getPluginState().setDisabled(true);
}
UpdateManager update_manager = core.getPluginManager().getDefaultPluginInterface().getUpdateManager();
final UpdateCheckInstance checker = update_manager.createUpdateCheckInstance();
checker.addListener(new UpdateCheckInstanceListener() {
@Override
public void cancelled(UpdateCheckInstance instance) {
}
@Override
public void complete(UpdateCheckInstance instance) {
int num_updates = 0;
Update[] updates = instance.getUpdates();
for (int i = 0; i < updates.length; i++) {
Update update = updates[i];
num_updates++;
out.println("Update available for '" + update.getName() + "', new version = " + update.getNewVersion());
String[] descs = update.getDescription();
for (int j = 0; j < descs.length; j++) {
out.println("\t" + descs[j]);
}
if (update.isMandatory()) {
out.println("**** This is a mandatory update, other updates can not proceed until this is performed ****");
}
}
// need to cancel this otherwise it sits there blocking other installer operations
checker.cancel();
if (num_updates > 0) {
out.println("Apply these updates with the 'plugin update' command");
}
}
});
checker.start();
}
use of com.biglybt.pif.update.UpdateManager in project BiglyBT by BiglySoftware.
the class Plugin method execute.
@Override
public void execute(String commandName, final ConsoleInput ci, List args) {
if (args.isEmpty()) {
printHelpExtra(ci.out, args);
return;
}
String subcmd = (String) args.get(0);
if (!java.util.Arrays.asList(new String[] { "location", "list", "listall", "status", "startup", "install", "uninstall", "update" }).contains(subcmd)) {
ci.out.println("Invalid subcommand: " + subcmd);
ci.out.println();
return;
}
PluginManager plugin_manager = ci.getCore().getPluginManager();
if (subcmd.equals("list") || subcmd.equals("listall")) {
boolean all_plugins = subcmd.equals("listall");
ci.out.println("> -----");
PluginInterface[] plugins = plugin_manager.getPluginInterfaces();
TreeSet plugin_ids = new TreeSet(String.CASE_INSENSITIVE_ORDER);
for (int i = 0; i < plugins.length; i++) {
if (!all_plugins && !plugins[i].getPluginState().isOperational()) {
continue;
}
String plugin_id = plugins[i].getPluginID();
plugin_ids.add(plugin_id);
}
TextWrap.printList(plugin_ids.iterator(), ci.out, " ");
ci.out.println("> -----");
return;
}
if (subcmd.equals("location")) {
// Taken from ConfigSectionPlugins.
File fUserPluginDir = FileUtil.getUserFile("plugins");
String sep = File.separator;
String sUserPluginDir;
try {
sUserPluginDir = fUserPluginDir.getCanonicalPath();
} catch (Throwable e) {
sUserPluginDir = fUserPluginDir.toString();
}
if (!sUserPluginDir.endsWith(sep)) {
sUserPluginDir += sep;
}
File fAppPluginDir = FileUtil.getApplicationFile("plugins");
String sAppPluginDir;
try {
sAppPluginDir = fAppPluginDir.getCanonicalPath();
} catch (Throwable e) {
sAppPluginDir = fAppPluginDir.toString();
}
if (!sAppPluginDir.endsWith(sep)) {
sAppPluginDir += sep;
}
ci.out.println("Shared plugin location:");
ci.out.println(" " + sAppPluginDir);
ci.out.println("User plugin location:");
ci.out.println(" " + sUserPluginDir);
ci.out.println();
return;
}
if (subcmd.equals("update")) {
if (args.size() != 1) {
ci.out.println("Usage: update");
return;
}
UpdateManager update_manager = plugin_manager.getDefaultPluginInterface().getUpdateManager();
final UpdateCheckInstance checker = update_manager.createUpdateCheckInstance();
checker.addListener(new UpdateCheckInstanceListener() {
@Override
public void cancelled(UpdateCheckInstance instance) {
}
@Override
public void complete(UpdateCheckInstance instance) {
Update[] updates = instance.getUpdates();
try {
for (Update update : updates) {
ci.out.println("Updating " + update.getName());
for (ResourceDownloader rd : update.getDownloaders()) {
rd.addListener(new ResourceDownloaderAdapter() {
@Override
public void reportActivity(ResourceDownloader downloader, String activity) {
ci.out.println("\t" + activity);
}
@Override
public void reportPercentComplete(ResourceDownloader downloader, int percentage) {
ci.out.println("\t" + percentage + "%");
}
});
rd.download();
}
}
boolean restart_required = false;
for (int i = 0; i < updates.length; i++) {
if (updates[i].getRestartRequired() == Update.RESTART_REQUIRED_YES) {
restart_required = true;
}
}
if (restart_required) {
ci.out.println("**** Restart required to complete update ****");
}
} catch (Throwable e) {
ci.out.println("Plugin update failed: " + Debug.getNestedExceptionMessage(e));
}
}
});
checker.start();
return;
}
if (subcmd.equals("install")) {
if (args.size() == 1) {
ci.out.println("Contacting plugin repository for list of available plugins...");
try {
SFPluginDetails[] plugins = SFPluginDetailsLoaderFactory.getSingleton().getPluginDetails();
for (SFPluginDetails p : plugins) {
String category = p.getCategory();
if (category != null) {
if (category.equalsIgnoreCase("hidden") || (category.equalsIgnoreCase("core"))) {
continue;
}
}
String id = p.getId();
if (plugin_manager.getPluginInterfaceByID(id, false) == null) {
String desc = p.getDescription();
int pos = desc.indexOf("<br");
if (pos > 0) {
desc = desc.substring(0, pos);
}
ci.out.println("\t" + id + ": \t\t" + desc);
}
}
} catch (Throwable e) {
ci.out.println("Failed to list plugins: " + Debug.getNestedExceptionMessage(e));
}
} else {
String target_id = (String) args.get(1);
if (plugin_manager.getPluginInterfaceByID(target_id, false) != null) {
ci.out.println("Plugin '" + target_id + "' already installed");
return;
}
final PluginInstaller installer = plugin_manager.getPluginInstaller();
try {
final StandardPlugin sp = installer.getStandardPlugin(target_id);
if (sp == null) {
ci.out.println("Plugin '" + target_id + "' is unknown");
return;
}
new AEThread2("Plugin Installer") {
@Override
public void run() {
try {
Map<Integer, Object> properties = new HashMap<>();
properties.put(UpdateCheckInstance.PT_UI_STYLE, UpdateCheckInstance.PT_UI_STYLE_NONE);
properties.put(UpdateCheckInstance.PT_UI_DISABLE_ON_SUCCESS_SLIDEY, true);
final AESemaphore sem = new AESemaphore("plugin-install");
final boolean[] restart_required = { false };
UpdateCheckInstance instance = installer.install(new InstallablePlugin[] { sp }, false, properties, new PluginInstallationListener() {
@Override
public void completed() {
ci.out.println("Installation complete");
sem.release();
}
@Override
public void cancelled() {
ci.out.println("Installation cancelled");
sem.release();
}
@Override
public void failed(PluginException e) {
ci.out.println("Installation failed: " + Debug.getNestedExceptionMessage(e));
sem.release();
}
});
instance.addListener(new UpdateCheckInstanceListener() {
@Override
public void cancelled(UpdateCheckInstance instance) {
ci.out.println("Installation cancelled");
}
@Override
public void complete(UpdateCheckInstance instance) {
Update[] updates = instance.getUpdates();
for (final Update update : updates) {
ResourceDownloader[] rds = update.getDownloaders();
for (ResourceDownloader rd : rds) {
rd.addListener(new ResourceDownloaderAdapter() {
@Override
public void reportActivity(ResourceDownloader downloader, String activity) {
ci.out.println("\t" + activity);
}
@Override
public void reportPercentComplete(ResourceDownloader downloader, int percentage) {
ci.out.println("\t" + percentage + "%");
}
});
try {
rd.download();
} catch (Throwable e) {
}
}
if (update.getRestartRequired() != Update.RESTART_REQUIRED_NO) {
restart_required[0] = true;
}
}
}
});
sem.reserve();
if (restart_required[0]) {
ci.out.println("**** Restart required to complete installation ****");
}
} catch (Throwable e) {
ci.out.println("Install failed: " + Debug.getNestedExceptionMessage(e));
}
}
}.start();
} catch (Throwable e) {
ci.out.println("Install failed: " + Debug.getNestedExceptionMessage(e));
}
}
return;
}
// Commands from this point require a plugin ID.
if (args.size() == 1) {
ci.out.println("No plugin ID given.");
ci.out.println();
return;
}
String plugin_id = (String) args.get(1);
PluginInterface plugin = plugin_manager.getPluginInterfaceByID(plugin_id, false);
if (plugin == null) {
ci.out.println("Invalid plugin ID: " + plugin_id);
ci.out.println();
return;
}
if (subcmd.equals("status")) {
ci.out.println("ID : " + plugin.getPluginID());
ci.out.println("Name : " + plugin.getPluginName());
ci.out.println("Version: " + plugin.getPluginVersion());
ci.out.println("Running: " + plugin.getPluginState().isOperational());
ci.out.println("Runs at startup: " + plugin.getPluginState().isLoadedAtStartup());
if (!plugin.getPluginState().isBuiltIn()) {
ci.out.println("Location: " + plugin.getPluginDirectoryName());
}
ci.out.println();
return;
}
if (subcmd.equals("startup")) {
if (args.size() == 2) {
ci.out.println("Need to pass either \"on\" or \"off\"");
ci.out.println();
return;
}
String enabled_mode = (String) args.get(2);
if (enabled_mode.equals("on")) {
plugin.getPluginState().setLoadedAtStartup(true);
} else if (enabled_mode.equals("off")) {
plugin.getPluginState().setLoadedAtStartup(false);
} else {
ci.out.println("Need to pass either \"on\" or \"off\"");
ci.out.println();
return;
}
ci.out.println("Done.");
ci.out.println();
return;
}
if (subcmd.equals("uninstall")) {
PluginInterface pi = plugin_manager.getPluginInterfaceByID(plugin_id, false);
if (pi == null) {
ci.out.println("Plugin '" + plugin_id + "' is not installed");
return;
}
final PluginInstaller installer = plugin_manager.getPluginInstaller();
try {
final StandardPlugin sp = installer.getStandardPlugin(plugin_id);
if (sp == null) {
ci.out.println("Plugin '" + plugin_id + "' is not a standard plugin");
return;
}
final PluginInstaller uninstaller = plugin_manager.getPluginInstaller();
Map<Integer, Object> properties = new HashMap<>();
final AESemaphore sem = new AESemaphore("plugin-uninstall");
UpdateCheckInstance instance = uninstaller.uninstall(new PluginInterface[] { pi }, new PluginInstallationListener() {
@Override
public void completed() {
ci.out.println("Uninstallation complete");
sem.release();
}
@Override
public void cancelled() {
ci.out.println("Uninstallation cancelled");
sem.release();
}
@Override
public void failed(PluginException e) {
ci.out.println("Uninstallation failed: " + Debug.getNestedExceptionMessage(e));
sem.release();
}
}, properties);
instance.addListener(new UpdateCheckInstanceListener() {
@Override
public void cancelled(UpdateCheckInstance instance) {
ci.out.println("InsUninstallationtallation cancelled");
}
@Override
public void complete(UpdateCheckInstance instance) {
Update[] updates = instance.getUpdates();
for (final Update update : updates) {
ResourceDownloader[] rds = update.getDownloaders();
for (ResourceDownloader rd : rds) {
try {
rd.download();
} catch (Throwable e) {
}
}
}
}
});
sem.reserve();
Object obj = properties.get(UpdateCheckInstance.PT_UNINSTALL_RESTART_REQUIRED);
if (obj instanceof Boolean && (Boolean) obj) {
ci.out.println("**** Restart required to complete uninstallation ****");
}
} catch (Throwable e) {
ci.out.println("Uninstall failed: " + Debug.getNestedExceptionMessage(e));
}
}
}
Aggregations