use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloader in project BiglyBT by BiglySoftware.
the class PluginInstallerImpl method uninstall.
@Override
public UpdateCheckInstance uninstall(final PluginInterface[] pis, final PluginInstallationListener listener_maybe_null, final Map<Integer, Object> properties) throws PluginException {
properties.put(UpdateCheckInstance.PT_UNINSTALL_RESTART_REQUIRED, false);
for (int i = 0; i < pis.length; i++) {
PluginInterface pi = pis[i];
if (pi.getPluginState().isMandatory()) {
throw (new PluginException("Plugin '" + pi.getPluginID() + "' is mandatory, can't uninstall"));
}
if (pi.getPluginState().isBuiltIn()) {
throw (new PluginException("Plugin '" + pi.getPluginID() + "' is built-in, can't uninstall"));
}
String plugin_dir = pi.getPluginDirectoryName();
if (plugin_dir == null || !new File(plugin_dir).exists()) {
throw (new PluginException("Plugin '" + pi.getPluginID() + "' is not loaded from the file system, can't uninstall"));
}
}
try {
UpdateManager uman = manager.getDefaultPluginInterface().getUpdateManager();
UpdateCheckInstance inst = uman.createEmptyUpdateCheckInstance(UpdateCheckInstance.UCI_UNINSTALL, "update.instance.uninstall");
final int[] rds_added = { 0 };
final AESemaphore rd_waiter_sem = new AESemaphore("uninst:rd:wait");
for (int i = 0; i < pis.length; i++) {
final PluginInterface pi = pis[i];
final String plugin_dir = pi.getPluginDirectoryName();
inst.addUpdatableComponent(new UpdatableComponent() {
@Override
public String getName() {
return (pi.getPluginName());
}
@Override
public int getMaximumCheckTime() {
return (0);
}
@Override
public void checkForUpdate(final UpdateChecker checker) {
try {
ResourceDownloader rd = manager.getDefaultPluginInterface().getUtilities().getResourceDownloaderFactory().create(new File(plugin_dir));
// the plugin may have > 1 plugin interfaces, make the name up appropriately
String update_name = "";
PluginInterface[] ifs = manager.getPluginInterfaces();
Arrays.sort(ifs, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
return (((PluginInterface) o1).getPluginName().compareTo(((PluginInterface) o2).getPluginName()));
}
});
for (int i = 0; i < ifs.length; i++) {
if (ifs[i].getPluginID().equals(pi.getPluginID())) {
update_name += (update_name.length() == 0 ? "" : ",") + ifs[i].getPluginName();
}
}
boolean unloadable = pi.getPluginState().isUnloadable();
if (!unloadable) {
properties.put(UpdateCheckInstance.PT_UNINSTALL_RESTART_REQUIRED, true);
}
final Update update = checker.addUpdate(update_name, new String[] { "Uninstall: " + plugin_dir }, pi.getPluginVersion(), pi.getPluginVersion(), rd, unloadable ? Update.RESTART_REQUIRED_NO : Update.RESTART_REQUIRED_YES);
synchronized (rds_added) {
rds_added[0]++;
}
rd.addListener(new ResourceDownloaderAdapter() {
@Override
public boolean completed(ResourceDownloader downloader, InputStream data) {
try {
try {
if (pi.getPluginState().isUnloadable()) {
pi.getPluginState().unload();
if (!FileUtil.recursiveDelete(new File(plugin_dir))) {
update.setRestartRequired(Update.RESTART_REQUIRED_YES);
properties.put(UpdateCheckInstance.PT_UNINSTALL_RESTART_REQUIRED, true);
checker.reportProgress("Failed to remove plugin, restart will be required");
}
}
UpdateInstaller installer = checker.createInstaller();
installer.addRemoveAction(new File(plugin_dir).getCanonicalPath());
update.complete(true);
try {
PluginInitializer.fireEvent(PluginEvent.PEV_PLUGIN_UNINSTALLED, pi.getPluginID());
} catch (Throwable e) {
Debug.out(e);
}
} catch (Throwable e) {
update.complete(false);
Debug.printStackTrace(e);
Logger.log(new LogAlert(LogAlert.REPEATABLE, "Plugin uninstall failed", e));
}
return (true);
} finally {
rd_waiter_sem.release();
}
}
@Override
public void failed(ResourceDownloader downloader, ResourceDownloaderException e) {
try {
update.complete(false);
if (!downloader.isCancelled()) {
Logger.log(new LogAlert(LogAlert.REPEATABLE, "Plugin uninstall failed", e));
}
} finally {
rd_waiter_sem.release();
}
}
});
} finally {
checker.completed();
}
}
}, false);
}
if (listener_maybe_null != null) {
inst.addListener(new UpdateCheckInstanceListener() {
@Override
public void cancelled(UpdateCheckInstance instance) {
listener_maybe_null.cancelled();
}
@Override
public void complete(UpdateCheckInstance instance) {
// needs to be async in the case of the caller of the uninstall needing access to the
// updatecheckinstance and this is a sync callback
new AEThread2("Uninstall:async") {
@Override
public void run() {
int wait_count;
synchronized (rds_added) {
wait_count = rds_added[0];
}
for (int i = 0; i < wait_count; i++) {
rd_waiter_sem.reserve();
}
listener_maybe_null.completed();
}
}.start();
}
});
}
inst.start();
return (inst);
} catch (Throwable e) {
PluginException pe;
if (e instanceof PluginException) {
pe = (PluginException) e;
} else {
pe = new PluginException("Uninstall failed", e);
}
if (listener_maybe_null != null) {
listener_maybe_null.failed(pe);
}
throw (pe);
}
}
use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloader 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));
}
}
}
use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloader in project BiglyBT by BiglySoftware.
the class SimpleInstallUI method build.
protected void build(Composite parent) {
parent.setLayout(new FormLayout());
Button cancel_button = new Button(parent, SWT.NULL);
cancel_button.setText("Cancel");
cancel_button.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event arg0) {
synchronized (SimpleInstallUI.this) {
cancelled = true;
if (current_downloader != null) {
current_downloader.cancel();
}
}
instance.cancel();
}
});
FormData data = new FormData();
data.right = new FormAttachment(100, 0);
data.top = new FormAttachment(0, 0);
data.bottom = new FormAttachment(100, 0);
cancel_button.setLayoutData(data);
final Label label = new Label(parent, SWT.NULL);
label.setText("blah blah ");
data = new FormData();
data.left = new FormAttachment(0, 0);
data.top = new FormAttachment(cancel_button, 0, SWT.CENTER);
label.setLayoutData(data);
final ProgressBar progress = new ProgressBar(parent, SWT.NULL);
progress.setMinimum(0);
progress.setMaximum(100);
progress.setSelection(0);
data = new FormData();
data.left = new FormAttachment(label, 4);
data.top = new FormAttachment(cancel_button, 0, SWT.CENTER);
data.right = new FormAttachment(cancel_button, -4);
progress.setLayoutData(data);
parent.layout(true, true);
new AEThread2("SimpleInstallerUI", true) {
@Override
public void run() {
try {
Update[] updates = instance.getUpdates();
for (Update update : updates) {
String name = update.getName();
int pos = name.indexOf('/');
if (pos >= 0) {
name = name.substring(pos + 1);
}
setLabel(name);
ResourceDownloader[] downloaders = update.getDownloaders();
for (ResourceDownloader downloader : downloaders) {
synchronized (SimpleInstallUI.this) {
if (cancelled) {
return;
}
current_downloader = downloader;
}
setProgress(0);
downloader.addListener(new ResourceDownloaderAdapter() {
@Override
public void reportPercentComplete(ResourceDownloader downloader, int percentage) {
setProgress(percentage);
}
@Override
public void reportAmountComplete(ResourceDownloader downloader, long amount) {
}
});
downloader.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) {
monitor.handleRestart();
}
} catch (Throwable e) {
Debug.out("Install failed", e);
instance.cancel();
}
}
protected void setLabel(final String str) {
Utils.execSWTThread(new Runnable() {
@Override
public void run() {
if (label != null && !label.isDisposed()) {
label.setText(str);
label.getParent().layout();
}
}
});
}
protected void setProgress(final int percent) {
Utils.execSWTThread(new Runnable() {
@Override
public void run() {
if (progress != null && !progress.isDisposed()) {
progress.setSelection(percent);
}
}
});
}
}.start();
}
use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloader in project BiglyBT by BiglySoftware.
the class UpdateAutoDownloader method start.
private void start() {
for (int i = 0; i < updates.length; i++) {
Update update = updates[i];
ResourceDownloader[] rds = update.getDownloaders();
Collections.addAll(downloaders, rds);
}
iterDownloaders = downloaders.iterator();
nextUpdate();
}
use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloader in project BiglyBT by BiglySoftware.
the class UpdateWindow method update.
private void update() {
btnOk.setEnabled(false);
Messages.setLanguageText(btnCancel, "UpdateWindow.cancel");
table.setEnabled(false);
link_area.reset();
if (browser != null) {
browser.setVisible(false);
}
link_area.getComponent().setVisible(true);
TableItem[] items = table.getItems();
totalDownloadSize = 0;
downloaders = new ArrayList();
for (int i = 0; i < items.length; i++) {
if (!items[i].getChecked())
continue;
Update update = (Update) items[i].getData();
ResourceDownloader[] rds = update.getDownloaders();
for (int j = 0; j < rds.length; j++) {
downloaders.add(rds[j]);
try {
totalDownloadSize += rds[j].getSize();
} catch (Exception e) {
link_area.addLine(MessageText.getString("UpdateWindow.no_size") + rds[j].getName());
}
}
}
downloadersToData = new HashMap();
iterDownloaders = downloaders.iterator();
nextUpdate();
}
Aggregations