Search in sources :

Example 1 with LaunchablePlugin

use of com.biglybt.pif.LaunchablePlugin in project BiglyBT by BiglySoftware.

the class PluginLauncherImpl method findLaunchablePlugins.

private static LaunchablePlugin[] findLaunchablePlugins(LoggerChannelListener listener) {
    // CAREFUL - this is called BEFORE any AZ initialisation has been performed and must
    // therefore NOT use anything that relies on this (such as logging, debug....)
    List res = new ArrayList();
    File app_dir = getApplicationFile("plugins");
    if (!(app_dir.exists()) && app_dir.isDirectory()) {
        listener.messageLogged(LoggerChannel.LT_ERROR, "Application dir '" + app_dir + "' not found");
        return (new LaunchablePlugin[0]);
    }
    File[] plugins = app_dir.listFiles();
    if (plugins == null || plugins.length == 0) {
        listener.messageLogged(LoggerChannel.LT_ERROR, "Application dir '" + app_dir + "' empty");
        return (new LaunchablePlugin[0]);
    }
    for (int i = 0; i < plugins.length; i++) {
        File plugin_dir = plugins[i];
        if (!plugin_dir.isDirectory()) {
            continue;
        }
        try {
            ClassLoader classLoader = PluginLauncherImpl.class.getClassLoader();
            ClassLoader root_cl = classLoader;
            File[] contents = plugin_dir.listFiles();
            if (contents == null || contents.length == 0) {
                continue;
            }
            // take only the highest version numbers of jars that look versioned
            String[] plugin_version = { null };
            String[] plugin_id = { null };
            contents = getHighestJarVersions(contents, plugin_version, plugin_id, true);
            for (int j = 0; j < contents.length; j++) {
                classLoader = addFileToClassPath(root_cl, classLoader, contents[j]);
            }
            Properties props = new Properties();
            File properties_file = new File(plugin_dir, "plugin.properties");
            if (properties_file.exists()) {
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(properties_file);
                    props.load(fis);
                } finally {
                    if (fis != null) {
                        fis.close();
                    }
                }
            } else {
                if (classLoader instanceof URLClassLoader) {
                    URLClassLoader current = (URLClassLoader) classLoader;
                    URL url = current.findResource("plugin.properties");
                    if (url != null) {
                        props.load(url.openStream());
                    }
                }
            }
            String plugin_class = (String) props.get("plugin.class");
            if (plugin_class == null || plugin_class.indexOf(';') != -1) {
                continue;
            }
            Class c = classLoader.loadClass(plugin_class);
            Plugin plugin = (Plugin) c.newInstance();
            if (plugin instanceof LaunchablePlugin) {
                preloaded_plugins.put(plugin_class, plugin);
                res.add(plugin);
            }
        } catch (Throwable e) {
            listener.messageLogged("Load of plugin in '" + plugin_dir + "' fails", e);
        }
    }
    LaunchablePlugin[] x = new LaunchablePlugin[res.size()];
    res.toArray(x);
    return (x);
}
Also used : SystemProperties(com.biglybt.core.util.SystemProperties) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) LaunchablePlugin(com.biglybt.pif.LaunchablePlugin) Plugin(com.biglybt.pif.Plugin) LaunchablePlugin(com.biglybt.pif.LaunchablePlugin)

Example 2 with LaunchablePlugin

use of com.biglybt.pif.LaunchablePlugin in project BiglyBT by BiglySoftware.

the class PluginLauncherImpl method launch.

public static void launch(String[] args) {
    if (Launcher.checkAndLaunch(PluginLauncherImpl.class, args))
        return;
    // This *has* to be done first as it sets system properties that are read and cached by Java
    COConfigurationManager.preInitialise();
    final LoggerChannelListener listener = new LoggerChannelListener() {

        @Override
        public void messageLogged(int type, String content) {
            log(content, false);
        }

        @Override
        public void messageLogged(String str, Throwable error) {
            log(str, true);
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            error.printStackTrace(pw);
            pw.flush();
            log(sw.toString(), true);
        }

        protected synchronized void log(String str, boolean stdout) {
            File log_file = getApplicationFile("launch.log");
            PrintWriter pw = null;
            try {
                pw = new PrintWriter(new FileWriter(log_file, true));
                if (str.endsWith("\n")) {
                    if (stdout) {
                        System.err.print("PluginLauncher: " + str);
                    }
                    pw.print(str);
                } else {
                    if (stdout) {
                        System.err.println("PluginLauncher: " + str);
                    }
                    pw.println(str);
                }
            } catch (Throwable e) {
            } finally {
                if (pw != null) {
                    pw.close();
                }
            }
        }
    };
    LaunchablePlugin[] launchables = findLaunchablePlugins(listener);
    if (launchables.length == 0) {
        listener.messageLogged(LoggerChannel.LT_ERROR, "No launchable plugins found");
        return;
    } else if (launchables.length > 1) {
        listener.messageLogged(LoggerChannel.LT_ERROR, "Multiple launchable plugins found, running first");
    }
    try {
        // set default details for restarter
        SystemProperties.setApplicationEntryPoint("com.biglybt.pif.PluginLauncher");
        launchables[0].setDefaults(args);
        if (PluginSingleInstanceHandler.process(listener, args)) {
            return;
        }
        // we have to run the core startup on a separate thread and then effectively pass "this thread"
        // through to the launchable "process" method
        Thread core_thread = new Thread("PluginLauncher") {

            @Override
            public void run() {
                try {
                    // give 'process' call below some time to start up
                    Thread.sleep(500);
                    Core core = CoreFactory.create();
                    core.start();
                } catch (Throwable e) {
                    listener.messageLogged("PluginLauncher: launch fails", e);
                }
            }
        };
        core_thread.setDaemon(true);
        core_thread.start();
        boolean restart = false;
        boolean process_succeeded = false;
        try {
            restart = launchables[0].process();
            process_succeeded = true;
        } finally {
            try {
                if (restart) {
                    CoreFactory.getSingleton().restart();
                } else {
                    CoreFactory.getSingleton().stop();
                }
            } catch (Throwable e) {
                if (process_succeeded) {
                    throw (e);
                }
            }
        }
    } catch (Throwable e) {
        listener.messageLogged("PluginLauncher: launch fails", e);
    }
}
Also used : LoggerChannelListener(com.biglybt.pif.logging.LoggerChannelListener) LaunchablePlugin(com.biglybt.pif.LaunchablePlugin) Core(com.biglybt.core.Core)

Aggregations

LaunchablePlugin (com.biglybt.pif.LaunchablePlugin)2 Core (com.biglybt.core.Core)1 SystemProperties (com.biglybt.core.util.SystemProperties)1 Plugin (com.biglybt.pif.Plugin)1 LoggerChannelListener (com.biglybt.pif.logging.LoggerChannelListener)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1