Search in sources :

Example 6 with PlatformManagerException

use of com.biglybt.pif.platform.PlatformManagerException in project BiglyBT by BiglySoftware.

the class PlatformManagerImpl method requestUserAttention.

/**
 * If the application is not active causes the application icon at the bottom to bounce until the application becomes active
 * If the application is already active then this method does nothing.
 *
 * Note: This is an undocumented feature from Apple so it's behavior may change without warning
 *
 * @param type one of USER_REQUEST_INFO, USER_REQUEST_WARNING
 */
@Override
public void requestUserAttention(int type, Object data) throws PlatformManagerException {
    if (type == USER_REQUEST_QUESTION) {
        return;
    }
    try {
        Class<?> claNSApplication = Class.forName("com.apple.eawt.Application");
        Method methGetApplication = claNSApplication.getMethod("getApplication");
        Object app = methGetApplication.invoke(null);
        Method methRequestUserAttention = claNSApplication.getMethod("requestUserAttention", new Class[] { Boolean.class });
        if (type == USER_REQUEST_INFO) {
            methRequestUserAttention.invoke(app, false);
        } else if (type == USER_REQUEST_WARNING) {
            methRequestUserAttention.invoke(app, true);
        }
    } catch (Exception e) {
        throw new PlatformManagerException("Failed to request user attention", e);
    }
}
Also used : Method(java.lang.reflect.Method) PlatformManagerException(com.biglybt.pif.platform.PlatformManagerException) PlatformManagerException(com.biglybt.pif.platform.PlatformManagerException)

Example 7 with PlatformManagerException

use of com.biglybt.pif.platform.PlatformManagerException in project BiglyBT by BiglySoftware.

the class PlatformManagerImpl method setExplicitVMOptions.

@Override
public void setExplicitVMOptions(String[] options) throws PlatformManagerException {
    checkCapability(PlatformManagerCapabilities.AccessExplicitVMOptions);
    File local_options = checkAndGetLocalVMOptionFile();
    try {
        if (local_options.exists()) {
            File backup = new File(local_options.getParentFile(), local_options.getName() + ".bak");
            if (backup.exists()) {
                backup.delete();
            }
            if (!local_options.renameTo(backup)) {
                throw (new Exception("Failed to move " + local_options + " to " + backup));
            }
            boolean ok = false;
            try {
                PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(local_options), "UTF-8"));
                try {
                    for (String option : options) {
                        pw.println(option);
                    }
                    ok = true;
                } finally {
                    pw.close();
                }
            } finally {
                if (!ok) {
                    local_options.delete();
                    backup.renameTo(local_options);
                }
            }
        }
    } catch (Throwable e) {
        throw (new PlatformManagerException(MessageText.getString("platform.jvmopt.accesserror", new String[] { Debug.getNestedExceptionMessage(e) })));
    }
}
Also used : PlatformManagerException(com.biglybt.pif.platform.PlatformManagerException) PlatformManagerException(com.biglybt.pif.platform.PlatformManagerException)

Example 8 with PlatformManagerException

use of com.biglybt.pif.platform.PlatformManagerException in project BiglyBT by BiglySoftware.

the class PlatformManagerImpl method checkAndGetLocalVMOptionFile.

private File checkAndGetLocalVMOptionFile() throws PlatformManagerException {
    String vendor = System.getProperty("java.vendor", "<unknown>");
    if (!vendor.toLowerCase().startsWith("sun ") && !vendor.toLowerCase().startsWith("oracle ")) {
        throw (new PlatformManagerException(MessageText.getString("platform.jvmopt.sunonly", new String[] { vendor })));
    }
    File[] option_files = getJVMOptionFiles();
    if (option_files.length != 2) {
        throw (new PlatformManagerException(MessageText.getString("platform.jvmopt.configerror")));
    }
    File shared_options = option_files[0];
    if (shared_options.exists()) {
        try {
            String s_options = FileUtil.readFileAsString(shared_options, -1);
            if (s_options.contains(getJVMOptionRedirect())) {
                File local_options = option_files[1];
                return (local_options);
            } else {
                throw (new PlatformManagerException(MessageText.getString("platform.jvmopt.nolink")));
            }
        } catch (Throwable e) {
            throw (new PlatformManagerException(MessageText.getString("platform.jvmopt.accesserror", new String[] { Debug.getNestedExceptionMessage(e) })));
        }
    } else {
        throw (new PlatformManagerException(MessageText.getString("platform.jvmopt.nolinkfile")));
    }
}
Also used : PlatformManagerException(com.biglybt.pif.platform.PlatformManagerException)

Example 9 with PlatformManagerException

use of com.biglybt.pif.platform.PlatformManagerException in project BiglyBT by BiglySoftware.

the class PluginInitializer method loadPluginFromDir.

private List loadPluginFromDir(File directory, boolean bSkipAlreadyLoaded, boolean loading_for_startup, // initialise setting is used if loading_for_startup isnt
boolean initialise) throws PluginException {
    List loaded_pis = new ArrayList();
    ClassLoader plugin_class_loader = root_class_loader;
    if (!directory.isDirectory()) {
        return (loaded_pis);
    }
    String pluginName = directory.getName();
    File[] pluginContents = directory.listFiles();
    if (pluginContents == null || pluginContents.length == 0) {
        return (loaded_pis);
    }
    // first sanity check - dir must include either a plugin.properties or
    // at least one .jar file
    boolean looks_like_plugin = false;
    for (int i = 0; i < pluginContents.length; i++) {
        String name = pluginContents[i].getName().toLowerCase();
        if (name.endsWith(".jar") || name.equals("plugin.properties")) {
            looks_like_plugin = true;
            break;
        }
    }
    if (!looks_like_plugin) {
        if (Logger.isEnabled())
            Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING, "Plugin directory '" + directory + "' has no plugin.properties " + "or .jar files, skipping"));
        return (loaded_pis);
    }
    // take only the highest version numbers of jars that look versioned
    String[] plugin_version = { null };
    String[] plugin_id = { null };
    pluginContents = PluginLauncherImpl.getHighestJarVersions(pluginContents, plugin_version, plugin_id, true);
    for (int i = 0; i < pluginContents.length; i++) {
        File jar_file = pluginContents[i];
        if (pluginContents.length > 1) {
            String name = jar_file.getName();
            if (name.startsWith("i18nPlugin_")) {
                if (Logger.isEnabled())
                    Logger.log(new LogEvent(LOGID, "renaming '" + name + "' to conform with versioning system"));
                jar_file.renameTo(new File(jar_file.getParent(), "i18nAZ_0.1.jar  "));
                continue;
            }
        }
        plugin_class_loader = PluginLauncherImpl.addFileToClassPath(root_class_loader, plugin_class_loader, jar_file);
    }
    String plugin_class_string = null;
    try {
        Properties props = new Properties();
        File properties_file = new File(directory.toString() + File.separator + "plugin.properties");
        try {
            if (properties_file.exists()) {
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(properties_file);
                    props.load(fis);
                } finally {
                    if (fis != null) {
                        fis.close();
                    }
                }
            } else {
                if (plugin_class_loader instanceof URLClassLoader) {
                    URLClassLoader current = (URLClassLoader) plugin_class_loader;
                    URL url = current.findResource("plugin.properties");
                    if (url != null) {
                        URLConnection connection = url.openConnection();
                        InputStream is = connection.getInputStream();
                        props.load(is);
                    } else {
                        throw (new Exception("failed to load plugin.properties from jars"));
                    }
                } else {
                    throw (new Exception("failed to load plugin.properties from dir or jars"));
                }
            }
        } catch (Throwable e) {
            Debug.printStackTrace(e);
            String msg = "Can't read 'plugin.properties' for plugin '" + pluginName + "': file may be missing";
            Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_ERROR, msg));
            System.out.println(msg);
            throw (new PluginException(msg, e));
        }
        checkJDKVersion(pluginName, props, true);
        checkCoreAppVersion(pluginName, props, true);
        plugin_class_string = (String) props.get("plugin.class");
        if (plugin_class_string == null) {
            plugin_class_string = (String) props.get("plugin.classes");
            if (plugin_class_string == null) {
                // set so we don't bork later will npe
                plugin_class_string = "";
            }
        }
        String plugin_name_string = (String) props.get("plugin.name");
        if (plugin_name_string == null) {
            plugin_name_string = (String) props.get("plugin.names");
        }
        int pos1 = 0;
        int pos2 = 0;
        while (true) {
            int p1 = plugin_class_string.indexOf(";", pos1);
            String plugin_class;
            if (p1 == -1) {
                plugin_class = plugin_class_string.substring(pos1).trim();
            } else {
                plugin_class = plugin_class_string.substring(pos1, p1).trim();
                pos1 = p1 + 1;
            }
            PluginInterfaceImpl existing_pi = getPluginFromClass(plugin_class);
            if (existing_pi != null) {
                if (bSkipAlreadyLoaded) {
                    break;
                }
                // allow user dir entries to override app dir entries without warning
                File this_parent = directory.getParentFile();
                File existing_parent = null;
                if (existing_pi.getInitializerKey() instanceof File) {
                    existing_parent = ((File) existing_pi.getInitializerKey()).getParentFile();
                }
                if (this_parent.equals(FileUtil.getApplicationFile("plugins")) && existing_parent != null && existing_parent.equals(FileUtil.getUserFile("plugins"))) {
                    if (Logger.isEnabled())
                        Logger.log(new LogEvent(LOGID, "Plugin '" + plugin_name_string + "/" + plugin_class + ": shared version overridden by user-specific one"));
                    return (new ArrayList());
                } else {
                    Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_WARNING, "Error loading '" + plugin_name_string + "', plugin class '" + plugin_class + "' is already loaded"));
                }
            } else {
                String plugin_name = null;
                if (plugin_name_string != null) {
                    int p2 = plugin_name_string.indexOf(";", pos2);
                    if (p2 == -1) {
                        plugin_name = plugin_name_string.substring(pos2).trim();
                    } else {
                        plugin_name = plugin_name_string.substring(pos2, p2).trim();
                        pos2 = p2 + 1;
                    }
                }
                Properties new_props = (Properties) props.clone();
                new_props.put("plugin.class", plugin_class);
                if (plugin_name != null) {
                    new_props.put("plugin.name", plugin_name);
                }
                // System.out.println( "loading plugin '" + plugin_class + "' using cl " + classLoader);
                // if the plugin load fails we still need to generate a plugin entry
                // as this drives the upgrade process
                Throwable load_failure = null;
                String pid = plugin_id[0] == null ? directory.getName() : plugin_id[0];
                List<File> verified_files = null;
                Plugin plugin = null;
                if (vc_disabled_plugins.contains(pid)) {
                    log("Plugin '" + pid + "' has been administratively disabled");
                } else {
                    try {
                        String cl_key = "plugin.cl.ext." + pid;
                        String str = COConfigurationManager.getStringParameter(cl_key, null);
                        if (str != null && str.length() > 0) {
                            COConfigurationManager.removeParameter(cl_key);
                            plugin_class_loader = PluginLauncherImpl.extendClassLoader(root_class_loader, plugin_class_loader, new URL(str));
                        }
                    } catch (Throwable e) {
                    }
                    if (pid.endsWith("_v")) {
                        verified_files = new ArrayList<>();
                        // re-verify jar files
                        log("Re-verifying " + pid);
                        for (int i = 0; i < pluginContents.length; i++) {
                            File jar_file = pluginContents[i];
                            if (jar_file.getName().endsWith(".jar")) {
                                try {
                                    log("    verifying " + jar_file);
                                    AEVerifier.verifyData(jar_file);
                                    verified_files.add(jar_file);
                                    log("    OK");
                                } catch (Throwable e) {
                                    String msg = "Error loading plugin '" + pluginName + "' / '" + plugin_class_string + "'";
                                    Logger.log(new LogAlert(LogAlert.UNREPEATABLE, msg, e));
                                    plugin = new FailedPlugin(plugin_name, directory.getAbsolutePath());
                                }
                            }
                        }
                    }
                    if (plugin == null) {
                        plugin = PluginLauncherImpl.getPreloadedPlugin(plugin_class);
                        if (plugin == null) {
                            try {
                                try {
                                    Class<Plugin> c = (Class<Plugin>) PlatformManagerFactory.getPlatformManager().loadClass(plugin_class_loader, plugin_class);
                                    // Class c = plugin_class_loader.loadClass(plugin_class);
                                    plugin = c.newInstance();
                                    try {
                                        if (plugin_class_loader instanceof URLClassLoader) {
                                            URL[] urls = ((URLClassLoader) plugin_class_loader).getURLs();
                                            for (URL u : urls) {
                                                String path = u.getPath();
                                                if (path.endsWith(".jar")) {
                                                    int s1 = path.lastIndexOf('/');
                                                    int s2 = path.lastIndexOf('\\');
                                                    path = path.substring(Math.max(s1, s2) + 1);
                                                    s2 = path.indexOf('_');
                                                    if (s2 > 0) {
                                                        path = path.substring(0, s2);
                                                        path = path.replaceAll("-", "");
                                                        String cl = "plugin.preinit." + pid + ".PI" + path;
                                                        try {
                                                            Class pic = plugin_class_loader.loadClass(cl);
                                                            if (pic != null) {
                                                                pic.newInstance();
                                                            }
                                                        } catch (Throwable e) {
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    } catch (Throwable e) {
                                    }
                                } catch (PlatformManagerException e) {
                                    throw (e.getCause());
                                }
                            } catch (java.lang.UnsupportedClassVersionError e) {
                                plugin = new FailedPlugin(plugin_name, directory.getAbsolutePath());
                                // shorten stack trace
                                load_failure = new UnsupportedClassVersionError(e.getMessage());
                            } catch (Throwable e) {
                                if (e instanceof ClassNotFoundException && props.getProperty("plugin.install_if_missing", "no").equalsIgnoreCase("yes")) {
                                // don't report the failure
                                } else {
                                    load_failure = e;
                                }
                                plugin = new FailedPlugin(plugin_name, directory.getAbsolutePath());
                            }
                        } else {
                            plugin_class_loader = plugin.getClass().getClassLoader();
                        }
                    }
                    MessageText.integratePluginMessages((String) props.get("plugin.langfile"), plugin_class_loader);
                    PluginInterfaceImpl plugin_interface = new PluginInterfaceImpl(plugin, this, directory, plugin_class_loader, verified_files, // key for config values
                    directory.getName(), new_props, directory.getAbsolutePath(), pid, plugin_version[0]);
                    boolean bEnabled = (loading_for_startup) ? plugin_interface.getPluginState().isLoadedAtStartup() : initialise;
                    plugin_interface.getPluginState().setDisabled(!bEnabled);
                    try {
                        Method load_method = plugin.getClass().getMethod("load", new Class[] { PluginInterface.class });
                        load_method.invoke(plugin, new Object[] { plugin_interface });
                    } catch (NoSuchMethodException e) {
                    } catch (Throwable e) {
                        load_failure = e;
                    }
                    loaded_pis.add(plugin_interface);
                    if (load_failure != null) {
                        plugin_interface.setAsFailed();
                        if (!pid.equals(UpdaterUpdateChecker.getPluginID())) {
                            String msg = MessageText.getString("plugin.init.load.failed", new String[] { plugin_name == null ? pluginName : plugin_name, directory.getAbsolutePath() });
                            LogAlert la;
                            if (load_failure instanceof UnsupportedClassVersionError) {
                                la = new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_ERROR, msg + ".\n\n" + MessageText.getString("plugin.install.class_version_error"));
                            } else if (load_failure instanceof ClassNotFoundException) {
                                la = new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_ERROR, msg + ".\n\n" + MessageText.getString("plugin.init.load.failed.classmissing") + "\n\n", load_failure);
                            } else {
                                la = new LogAlert(LogAlert.UNREPEATABLE, msg, load_failure);
                            }
                            Logger.log(la);
                            System.out.println(msg + ": " + load_failure);
                        }
                    }
                }
            }
            if (p1 == -1) {
                break;
            }
        }
        return (loaded_pis);
    } catch (Throwable e) {
        if (e instanceof PluginException) {
            throw ((PluginException) e);
        }
        Debug.printStackTrace(e);
        String msg = "Error loading plugin '" + pluginName + "' / '" + plugin_class_string + "'";
        Logger.log(new LogAlert(LogAlert.UNREPEATABLE, msg, e));
        System.out.println(msg + ": " + e);
        throw (new PluginException(msg, e));
    }
}
Also used : URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) LogEvent(com.biglybt.core.logging.LogEvent) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Method(java.lang.reflect.Method) FileInputStream(java.io.FileInputStream) URLConnection(java.net.URLConnection) PlatformManagerException(com.biglybt.pif.platform.PlatformManagerException) CoreException(com.biglybt.core.CoreException) UtilitiesImpl.runnableWithException(com.biglybt.pifimpl.local.utils.UtilitiesImpl.runnableWithException) LogAlert(com.biglybt.core.logging.LogAlert) URLClassLoader(java.net.URLClassLoader) PlatformManagerException(com.biglybt.pif.platform.PlatformManagerException) File(java.io.File)

Example 10 with PlatformManagerException

use of com.biglybt.pif.platform.PlatformManagerException in project BiglyBT by BiglySoftware.

the class Utils method launch.

public static void launch(String sFileOriginal, boolean sync, boolean force_url, boolean force_anon) {
    String sFileModified = sFileOriginal;
    if (sFileModified == null || sFileModified.trim().length() == 0) {
        return;
    }
    if (!force_url) {
        if (!Constants.isWindows && new File(sFileModified).isDirectory()) {
            PlatformManager mgr = PlatformManagerFactory.getPlatformManager();
            if (mgr.hasCapability(PlatformManagerCapabilities.ShowFileInBrowser)) {
                try {
                    PlatformManagerFactory.getPlatformManager().showFile(sFileModified);
                    return;
                } catch (PlatformManagerException e) {
                }
            }
        }
        sFileModified = sFileModified.replaceAll("&vzemb=1", "");
        String exe = getExplicitLauncher(sFileModified);
        if (exe != null) {
            File file = new File(sFileModified);
            try {
                System.out.println("Launching " + sFileModified + " with " + exe);
                if (Constants.isWindows) {
                    try {
                        PlatformManagerFactory.getPlatformManager().createProcess(exe + " \"" + sFileModified + "\"", false);
                        return;
                    } catch (Throwable e) {
                    }
                } else if (Constants.isOSX && exe.endsWith(".app")) {
                    ProcessBuilder pb = GeneralUtils.createProcessBuilder(file.getParentFile(), new String[] { "open", "-a", exe, file.getName() }, null);
                    pb.start();
                    return;
                }
                ProcessBuilder pb = GeneralUtils.createProcessBuilder(file.getParentFile(), new String[] { exe, file.getName() }, null);
                pb.start();
                return;
            } catch (Throwable e) {
                Debug.out("Launch failed", e);
            }
        }
    }
    String lc_sFile = sFileModified.toLowerCase(Locale.US);
    if (lc_sFile.startsWith("tor:")) {
        force_anon = true;
        lc_sFile = lc_sFile.substring(4);
        sFileModified = lc_sFile;
    }
    if (lc_sFile.startsWith("http:") || lc_sFile.startsWith("https:")) {
        String net_type;
        String eb_choice;
        boolean use_plugins;
        if (force_anon) {
            net_type = AENetworkClassifier.AT_TOR;
            eb_choice = "plugin";
            use_plugins = true;
        } else {
            net_type = AENetworkClassifier.AT_PUBLIC;
            try {
                net_type = AENetworkClassifier.categoriseAddress(new URL(sFileModified).getHost());
            } catch (Throwable e) {
            }
            eb_choice = COConfigurationManager.getStringParameter("browser.external.id", "system");
            use_plugins = COConfigurationManager.getBooleanParameter("browser.external.non.pub", true);
            if (net_type != AENetworkClassifier.AT_PUBLIC && use_plugins) {
                // hack to force to that code leg
                eb_choice = "plugin";
            }
        }
        if (eb_choice.equals("system")) {
        } else if (eb_choice.equals("manual")) {
            String browser_exe = COConfigurationManager.getStringParameter("browser.external.prog", "");
            File bf = new File(browser_exe);
            if (bf.exists()) {
                try {
                    Process proc = Runtime.getRuntime().exec(new String[] { bf.getAbsolutePath(), sFileModified });
                } catch (Throwable e) {
                    Debug.out(e);
                }
            } else {
                Debug.out("Can't launch '" + sFileModified + "' as manual browser '" + bf + " ' doesn't exist");
            }
            return;
        } else {
            handlePluginLaunch(eb_choice, net_type, use_plugins, sFileOriginal, sFileModified, sync, force_url, force_anon);
            return;
        }
    } else if (lc_sFile.startsWith("chat:")) {
        String plug_uri = "azplug:?id=azbuddy&arg=" + UrlUtils.encode(sFileModified);
        try {
            URLConnection connection = new URL(plug_uri).openConnection();
            connection.connect();
            String res = FileUtil.readInputStreamAsString(connection.getInputStream(), 2048);
            return;
        } catch (Throwable e) {
        }
    }
    boolean launched = Program.launch(sFileModified);
    if (!launched && Constants.isUnix) {
        sFileModified = sFileModified.replaceAll(" ", "\\ ");
        if (!Program.launch("xdg-open " + sFileModified)) {
            if (!Program.launch("htmlview " + sFileModified)) {
                Debug.out("Failed to launch '" + sFileModified + "'");
            }
        }
    }
}
Also used : PlatformManager(com.biglybt.platform.PlatformManager) PlatformManagerException(com.biglybt.pif.platform.PlatformManagerException) URL(java.net.URL) URLConnection(java.net.URLConnection)

Aggregations

PlatformManagerException (com.biglybt.pif.platform.PlatformManagerException)21 PlatformManager (com.biglybt.platform.PlatformManager)5 LogAlert (com.biglybt.core.logging.LogAlert)4 Method (java.lang.reflect.Method)4 File (java.io.File)3 LogEvent (com.biglybt.core.logging.LogEvent)2 PlatformManagerPingCallback (com.biglybt.platform.PlatformManagerPingCallback)2 LinkLabel (com.biglybt.ui.swt.components.LinkLabel)2 URL (java.net.URL)2 URLConnection (java.net.URLConnection)2 GridData (org.eclipse.swt.layout.GridData)2 CoreException (com.biglybt.core.CoreException)1 ParameterListener (com.biglybt.core.config.ParameterListener)1 CacheFile (com.biglybt.core.diskmanager.cache.CacheFile)1 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)1 UtilitiesImpl.runnableWithException (com.biglybt.pifimpl.local.utils.UtilitiesImpl.runnableWithException)1 AEWin32Access (com.biglybt.platform.win32.access.AEWin32Access)1 MessageBoxShell (com.biglybt.ui.swt.shells.MessageBoxShell)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1