Search in sources :

Example 6 with ResourceDownloaderAdapter

use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter in project BiglyBT by BiglySoftware.

the class PlatformManagerUpdateChecker method checkForUpdate.

@Override
public void checkForUpdate(final UpdateChecker checker) {
    try {
        SFPluginDetails sf_details = SFPluginDetailsLoaderFactory.getSingleton().getPluginDetails(plugin_interface.getPluginID());
        String current_version = plugin_interface.getPluginVersion();
        if (Logger.isEnabled())
            Logger.log(new LogEvent(LOGID, "PlatformManager:Win32 update check starts: current = " + current_version));
        boolean current_az_is_cvs = Constants.isCVSVersion();
        String sf_plugin_version = sf_details.getVersion();
        String sf_comp_version = sf_plugin_version;
        if (current_az_is_cvs) {
            String sf_cvs_version = sf_details.getCVSVersion();
            if (sf_cvs_version.length() > 0) {
                // sf cvs version ALWAYS entry in _CVS
                sf_plugin_version = sf_cvs_version;
                sf_comp_version = sf_plugin_version.substring(0, sf_plugin_version.length() - 4);
            }
        }
        String target_version = null;
        if (sf_comp_version.length() == 0 || !Character.isDigit(sf_comp_version.charAt(0))) {
            if (Logger.isEnabled())
                Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING, "PlatformManager:Win32 no valid version to check against (" + sf_comp_version + ")"));
        } else if (Constants.compareVersions(current_version, sf_comp_version) < 0) {
            target_version = sf_comp_version;
        }
        checker.reportProgress("Win32: current = " + current_version + ", latest = " + sf_comp_version);
        if (Logger.isEnabled())
            Logger.log(new LogEvent(LOGID, "PlatformManager:Win32 update required = " + (target_version != null)));
        if (target_version != null) {
            String target_download = sf_details.getDownloadURL();
            if (current_az_is_cvs) {
                String sf_cvs_version = sf_details.getCVSVersion();
                if (sf_cvs_version.length() > 0) {
                    target_download = sf_details.getCVSDownloadURL();
                }
            }
            ResourceDownloaderFactory rdf = ResourceDownloaderFactoryImpl.getSingleton();
            ResourceDownloader direct_rdl = rdf.create(new URL(target_download));
            String torrent_download = Constants.URL_PLUGINS_TORRENT_BASE;
            int slash_pos = target_download.lastIndexOf("/");
            if (slash_pos == -1) {
                torrent_download += target_download;
            } else {
                torrent_download += target_download.substring(slash_pos + 1);
            }
            torrent_download += ".torrent";
            if (I2PHelpers.isI2PInstalled()) {
                torrent_download += "?i2p=1";
            }
            ResourceDownloader torrent_rdl = rdf.create(new URL(torrent_download));
            torrent_rdl = rdf.getSuffixBasedDownloader(torrent_rdl);
            // create an alternate downloader with torrent attempt first
            ResourceDownloader alternate_rdl = rdf.getAlternateDownloader(new ResourceDownloader[] { torrent_rdl, direct_rdl });
            // get size here so it is cached
            rdf.getTimeoutDownloader(rdf.getRetryDownloader(alternate_rdl, RD_SIZE_RETRIES), RD_SIZE_TIMEOUT).getSize();
            List update_desc = new ArrayList();
            List desc_lines = HTMLUtils.convertHTMLToText("", sf_details.getDescription());
            update_desc.addAll(desc_lines);
            List comment_lines = HTMLUtils.convertHTMLToText("    ", sf_details.getComment());
            update_desc.addAll(comment_lines);
            String[] update_d = new String[update_desc.size()];
            update_desc.toArray(update_d);
            final Update update = checker.addUpdate(UPDATE_NAME, update_d, current_version, target_version, alternate_rdl, Update.RESTART_REQUIRED_YES);
            update.setDescriptionURL(sf_details.getInfoURL());
            alternate_rdl.addListener(new ResourceDownloaderAdapter() {

                @Override
                public boolean completed(final ResourceDownloader downloader, InputStream data) {
                    installUpdate(checker, update, downloader, data);
                    return (true);
                }

                @Override
                public void failed(ResourceDownloader downloader, ResourceDownloaderException e) {
                    // Debug.out( downloader.getName() + " failed", e );
                    update.complete(false);
                }
            });
        }
    } catch (Throwable e) {
        Debug.printStackTrace(e);
        checker.reportProgress("Failed to load plugin details for the platform manager: " + Debug.getNestedExceptionMessage(e));
        checker.failed();
    } finally {
        checker.completed();
    }
}
Also used : LogEvent(com.biglybt.core.logging.LogEvent) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ResourceDownloaderException(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException) ArrayList(java.util.ArrayList) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) Update(com.biglybt.pif.update.Update) URL(java.net.URL) ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) ResourceDownloaderFactory(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory) ArrayList(java.util.ArrayList) List(java.util.List) SFPluginDetails(com.biglybt.pifimpl.update.sf.SFPluginDetails)

Example 7 with ResourceDownloaderAdapter

use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter in project BiglyBT by BiglySoftware.

the class PluginUpdatePlugin method addUpdate.

public Update addUpdate(final PluginInterface pi_for_update, final UpdateChecker checker, final String update_name, final String[] update_details, final String old_version, final String new_version, final ResourceDownloader resource_downloader, final boolean is_jar, final int restart_type, final boolean verify) {
    final Update update = checker.addUpdate(update_name, update_details, old_version, new_version, resource_downloader, restart_type);
    update.setUserObject(pi_for_update);
    resource_downloader.addListener(new ResourceDownloaderAdapter() {

        @Override
        public boolean completed(final ResourceDownloader downloader, InputStream data) {
            // during the update phase report any messages
            // to the downloader
            LoggerChannelListener list = new LoggerChannelListener() {

                @Override
                public void messageLogged(int type, String content) {
                    downloader.reportActivity(content);
                }

                @Override
                public void messageLogged(String str, Throwable error) {
                    downloader.reportActivity(str);
                }
            };
            try {
                log.addListener(list);
                installUpdate(checker, update, pi_for_update, restart_type == Update.RESTART_REQUIRED_NO, is_jar, old_version, new_version, data, verify);
                return (true);
            } finally {
                log.removeListener(list);
            }
        }

        @Override
        public void failed(ResourceDownloader downloader, ResourceDownloaderException e) {
            if (!downloader.isCancelled()) {
                Debug.out(downloader.getName() + " failed", e);
            }
            update.complete(false);
        }
    });
    return (update);
}
Also used : ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) LoggerChannelListener(com.biglybt.pif.logging.LoggerChannelListener) ZipInputStream(java.util.zip.ZipInputStream) ResourceDownloaderException(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader)

Example 8 with ResourceDownloaderAdapter

use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter in project BiglyBT by BiglySoftware.

the class SimplePluginInstaller method install.

public boolean install() {
    try {
        installer = CoreFactory.getSingleton().getPluginManager().getPluginInstaller();
        StandardPlugin sp = installer.getStandardPlugin(plugin_id);
        if (sp == null) {
            throw (new Exception("Unknown plugin"));
        }
        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 Object[] result = new Object[] { null };
        instance = installer.install(new InstallablePlugin[] { sp }, false, properties, new PluginInstallationListener() {

            @Override
            public void completed() {
                synchronized (SimplePluginInstaller.this) {
                    completed = true;
                }
                result[0] = true;
                if (listener != null) {
                    listener.finished();
                }
                sem.release();
            }

            @Override
            public void cancelled() {
                result[0] = new Exception("Cancelled");
                if (listener != null) {
                    listener.finished();
                }
                sem.release();
            }

            @Override
            public void failed(PluginException e) {
                result[0] = e;
                if (listener != null) {
                    listener.finished();
                }
                sem.release();
            }
        });
        boolean kill_it;
        synchronized (this) {
            kill_it = cancelled;
        }
        if (kill_it) {
            instance.cancel();
            action_listener.actionComplete(new Exception("Cancelled"));
            return (false);
        }
        instance.addListener(new UpdateCheckInstanceListener() {

            @Override
            public void cancelled(UpdateCheckInstance instance) {
            }

            @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) {
                            }

                            @Override
                            public void reportPercentComplete(ResourceDownloader downloader, int percentage) {
                                if (listener != null) {
                                    listener.progress(percentage);
                                }
                            }
                        });
                    }
                }
            }
        });
        sem.reserve();
        action_listener.actionComplete(result[0]);
        return (result[0] instanceof Boolean);
    } catch (Throwable e) {
        if (listener != null) {
            listener.finished();
        }
        action_listener.actionComplete(e);
    }
    return false;
}
Also used : UpdateCheckInstance(com.biglybt.pif.update.UpdateCheckInstance) UpdateCheckInstanceListener(com.biglybt.pif.update.UpdateCheckInstanceListener) HashMap(java.util.HashMap) PluginException(com.biglybt.pif.PluginException) StandardPlugin(com.biglybt.pif.installer.StandardPlugin) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) AESemaphore(com.biglybt.core.util.AESemaphore) Update(com.biglybt.pif.update.Update) PluginException(com.biglybt.pif.PluginException) InstallablePlugin(com.biglybt.pif.installer.InstallablePlugin) ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) PluginInstallationListener(com.biglybt.pif.installer.PluginInstallationListener)

Example 9 with ResourceDownloaderAdapter

use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter 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);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ResourceDownloaderException(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) LogAlert(com.biglybt.core.logging.LogAlert) ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) VuzeFile(com.biglybt.core.vuzefile.VuzeFile) File(java.io.File)

Example 10 with ResourceDownloaderAdapter

use of com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter 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));
        }
    }
}
Also used : UpdateCheckInstance(com.biglybt.pif.update.UpdateCheckInstance) HashMap(java.util.HashMap) Update(com.biglybt.pif.update.Update) AESemaphore(com.biglybt.core.util.AESemaphore) PluginManager(com.biglybt.pif.PluginManager) PluginInstallationListener(com.biglybt.pif.installer.PluginInstallationListener) TreeSet(java.util.TreeSet) SFPluginDetails(com.biglybt.pifimpl.update.sf.SFPluginDetails) UpdateCheckInstanceListener(com.biglybt.pif.update.UpdateCheckInstanceListener) PluginInterface(com.biglybt.pif.PluginInterface) PluginException(com.biglybt.pif.PluginException) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) StandardPlugin(com.biglybt.pif.installer.StandardPlugin) AEThread2(com.biglybt.core.util.AEThread2) InstallablePlugin(com.biglybt.pif.installer.InstallablePlugin) PluginInstaller(com.biglybt.pif.installer.PluginInstaller) ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) UpdateManager(com.biglybt.pif.update.UpdateManager) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ResourceDownloader (com.biglybt.pif.utils.resourcedownloader.ResourceDownloader)12 ResourceDownloaderAdapter (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter)12 Update (com.biglybt.pif.update.Update)7 ResourceDownloaderException (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException)7 InputStream (java.io.InputStream)6 URL (java.net.URL)5 ZipInputStream (java.util.zip.ZipInputStream)5 ResourceDownloaderFactory (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory)4 ArrayList (java.util.ArrayList)4 LogEvent (com.biglybt.core.logging.LogEvent)3 AESemaphore (com.biglybt.core.util.AESemaphore)3 PluginException (com.biglybt.pif.PluginException)3 InstallablePlugin (com.biglybt.pif.installer.InstallablePlugin)3 PluginInstallationListener (com.biglybt.pif.installer.PluginInstallationListener)3 StandardPlugin (com.biglybt.pif.installer.StandardPlugin)3 UpdateCheckInstance (com.biglybt.pif.update.UpdateCheckInstance)3 UpdateCheckInstanceListener (com.biglybt.pif.update.UpdateCheckInstanceListener)3 SFPluginDetails (com.biglybt.pifimpl.update.sf.SFPluginDetails)3 File (java.io.File)3 HashMap (java.util.HashMap)3