Search in sources :

Example 11 with ResourceDownloaderFactory

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

the class SWTUpdateChecker method checkForUpdate.

@Override
public void checkForUpdate(final UpdateChecker checker) {
    try {
        SWTVersionGetter versionGetter = new SWTVersionGetter(checker);
        boolean update_required = System.getProperty(SystemProperties.SYSPROP_SKIP_SWTCHECK) == null && versionGetter.needsUpdate();
        if (update_required) {
            int update_prevented_version = COConfigurationManager.getIntParameter("swt.update.prevented.version", -1);
            try {
                URL swt_url = SWT.class.getClassLoader().getResource("org/eclipse/swt/SWT.class");
                if (swt_url != null) {
                    String url_str = swt_url.toExternalForm();
                    if (url_str.startsWith("jar:file:")) {
                        File jar_file = FileUtil.getJarFileFromURL(url_str);
                        File expected_dir = new File(checker.getCheckInstance().getManager().getInstallDir());
                        File jar_file_dir = jar_file.getParentFile();
                        if (expected_dir.exists() && jar_file_dir.exists()) {
                            expected_dir = expected_dir.getCanonicalFile();
                            jar_file_dir = jar_file_dir.getCanonicalFile();
                            if (Constants.isUnix) {
                                if (expected_dir.equals(jar_file_dir)) {
                                    // user put it there, so skip everything
                                    return;
                                }
                                // For unix, when swt.jar is in the appdir/swt
                                expected_dir = new File(expected_dir, "swt");
                            }
                            if (expected_dir.equals(jar_file_dir)) {
                                if (update_prevented_version != -1) {
                                    update_prevented_version = -1;
                                    COConfigurationManager.setParameter("swt.update.prevented.version", update_prevented_version);
                                }
                            } else {
                                // we need to periodically remind the user there's a problem as they need to realise that
                                // it is causing ALL updates (core/plugin) to fail
                                String alert = MessageText.getString("swt.alert.cant.update", new String[] { String.valueOf(versionGetter.getCurrentVersion()), String.valueOf(versionGetter.getLatestVersion()), jar_file_dir.toString(), expected_dir.toString() });
                                checker.reportProgress(alert);
                                long last_prompt = COConfigurationManager.getLongParameter("swt.update.prevented.version.time", 0);
                                long now = SystemTime.getCurrentTime();
                                boolean force = now < last_prompt || now - last_prompt > 7 * 24 * 60 * 60 * 1000;
                                if (!checker.getCheckInstance().isAutomatic()) {
                                    force = true;
                                }
                                if (force || update_prevented_version != versionGetter.getCurrentVersion()) {
                                    Logger.log(new LogAlert(LogAlert.REPEATABLE, LogEvent.LT_ERROR, alert));
                                    update_prevented_version = versionGetter.getCurrentVersion();
                                    COConfigurationManager.setParameter("swt.update.prevented.version", update_prevented_version);
                                    COConfigurationManager.setParameter("swt.update.prevented.version.time", now);
                                }
                            }
                        }
                    }
                }
            } catch (Throwable e) {
                Debug.printStackTrace(e);
            }
            if (update_prevented_version == versionGetter.getCurrentVersion()) {
                Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, "SWT update aborted due to previously reported issues regarding its install location"));
                checker.failed();
                checker.getCheckInstance().cancel();
                return;
            }
            String[] mirrors = versionGetter.getMirrors();
            ResourceDownloader swtDownloader = null;
            ResourceDownloaderFactory factory = ResourceDownloaderFactoryImpl.getSingleton();
            List<ResourceDownloader> downloaders = new ArrayList<>();
            for (int i = 0; i < mirrors.length; i++) {
                try {
                    downloaders.add(factory.getSuffixBasedDownloader(factory.create(new URL(mirrors[i]))));
                } catch (MalformedURLException e) {
                    // Do nothing
                    if (Logger.isEnabled())
                        Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING, "Cannot use URL " + mirrors[i] + " (not valid)"));
                }
            }
            for (int i = 0; i < mirrors.length; i++) {
                try {
                    downloaders.add(factory.getSuffixBasedDownloader(factory.createWithAutoPluginProxy(new URL(mirrors[i]))));
                } catch (MalformedURLException e) {
                }
            }
            ResourceDownloader[] resourceDownloaders = (ResourceDownloader[]) downloaders.toArray(new ResourceDownloader[downloaders.size()]);
            swtDownloader = factory.getAlternateDownloader(resourceDownloaders);
            try {
                swtDownloader.getSize();
            } catch (ResourceDownloaderException e) {
                Debug.printStackTrace(e);
            }
            String extra = "";
            if (Constants.isWindows && Constants.is64Bit) {
                extra = " (64-bit)";
            }
            final Update update = checker.addUpdate("SWT Library for " + versionGetter.getPlatform() + extra, new String[] { "SWT is the graphical library used by " + Constants.APP_NAME }, "" + versionGetter.getCurrentVersion(), "" + versionGetter.getLatestVersion(), swtDownloader, Update.RESTART_REQUIRED_YES);
            update.setDescriptionURL(versionGetter.getInfoURL());
            swtDownloader.addListener(new ResourceDownloaderAdapter() {

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

                @Override
                public void failed(ResourceDownloader downloader, ResourceDownloaderException e) {
                    Debug.out(downloader.getName() + " failed", e);
                    update.complete(false);
                }
            });
        }
    } catch (Throwable e) {
        Logger.log(new LogAlert(LogAlert.UNREPEATABLE, "SWT Version check failed", e));
        checker.failed();
    } finally {
        checker.completed();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ResourceDownloaderException(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) Update(com.biglybt.pif.update.Update) URL(java.net.URL) SWT(org.eclipse.swt.SWT) ResourceDownloaderAdapter(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter) ResourceDownloaderFactory(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory) File(java.io.File)

Aggregations

ResourceDownloader (com.biglybt.pif.utils.resourcedownloader.ResourceDownloader)11 ResourceDownloaderFactory (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory)11 InputStream (java.io.InputStream)8 URL (java.net.URL)8 ResourceDownloaderException (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException)7 ResourceDownloaderAdapter (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderAdapter)4 Update (com.biglybt.pif.update.Update)3 SFPluginDetails (com.biglybt.pifimpl.update.sf.SFPluginDetails)3 ArrayList (java.util.ArrayList)3 ZipInputStream (java.util.zip.ZipInputStream)3 LogEvent (com.biglybt.core.logging.LogEvent)2 WebEngine (com.biglybt.core.metasearch.impl.web.WebEngine)2 PluginProxy (com.biglybt.core.proxy.AEProxyFactory.PluginProxy)2 VuzeFile (com.biglybt.core.vuzefile.VuzeFile)2 List (java.util.List)2 Engine (com.biglybt.core.metasearch.Engine)1 SearchException (com.biglybt.core.metasearch.SearchException)1 SearchLoginException (com.biglybt.core.metasearch.SearchLoginException)1 SearchParameter (com.biglybt.core.metasearch.SearchParameter)1 PluginEngine (com.biglybt.core.metasearch.impl.plugin.PluginEngine)1