view src/org/icedrobot/ika/Ika.java @ 0:91ff28c21e0b

initial code drop
author Mario Torre <neugens.limasoftware@gmail.com>
date Fri, 04 Mar 2011 00:37:30 +0100
parents
children
line wrap: on
line source

/*
 * IKA - IcedRobot Kiosk Application
 * Copyright (C) 2011  IcedRobot team
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.icedrobot.ika;

import org.icedrobot.ika.plugins.IkaPluginException;
import org.icedrobot.ika.plugins.IkaPlugin;

import java.io.IOException;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Mario Torre <neugens.limasoftware@gmail.com>
 */
public class Ika {

    public static final String VERSION = "0.1";
    public static final String CORE_PLUGIN_LIST = "/core_plugins.properties";

    private Map<String, IkaPlugin> plugins = new HashMap<String, IkaPlugin>();

    private static final Ika theInstance = new Ika();
    public static Ika getInstance() {
        return theInstance;
    }

    private Ika() {
        /* nothing to do */
    }

    /**
     * Loads the core plugins list from the internal resource file.
     * The plugins are also initialised here, by calling their
     * {@link IkaPlugin#initPlugin() } method.
     */
    private void loadCorePluginsList() {

        Properties corePlugins = new Properties();
        try {
            corePlugins.load(Ika.class.getResourceAsStream(CORE_PLUGIN_LIST));

        } catch (IOException ex) {
            throw new IkaPluginException("cannot load plugin list", ex);
        }

        for (String pluginName : corePlugins.stringPropertyNames()) {
            // try to load the plugin, ne exception will be thrown
            // at this point if the plugin is not found
            // the plugins must all be in the classpath
            String pluginClass = corePlugins.getProperty(pluginName);
            try {
                Logger.getLogger(Ika.class.getName()).log(Level.FINEST,
                                 "loading: {0}", pluginClass);

                Class<IkaPlugin> _plugin =
                        (Class<IkaPlugin>) Class.forName(pluginClass);
                IkaPlugin plugin = _plugin.newInstance();
                
                plugins.put(pluginName, plugin);

            } catch (InstantiationException ex) {
                Logger.getLogger(Ika.class.getName()).log(Level.WARNING,
                                 "cannot instantiate plugin: " +
                                 pluginClass, ex);

            } catch (IllegalAccessException ex) {
                Logger.getLogger(Ika.class.getName()).log(Level.WARNING,
                                 "cannot access plugin: " + pluginClass, ex);

            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Ika.class.getName()).log(Level.WARNING,
                                 "cannot load plugin: " + pluginClass, ex);
            }
        }
    }

    /**
     * Parses the command line and reseach the module to load accordingly
     */
    public void parseArgumentAndInvoke(String ... args) {

        loadCorePluginsList();

        if (args == null || args.length == 0    ||
            "version".equalsIgnoreCase(args[0]) ||
            "--version".equalsIgnoreCase(args[0]))
        {
            // print a short help message and return
            plugins.get("version").execute(args);

        } else {
            // check if there is a plugin with the name passed as
            // argument
            IkaPlugin plugin = plugins.get(args[0]);
            if (plugin != null) {
                plugin.execute(args);

            } else {
                System.err.println("No plugins found: " + args[0]);
                System.err.println("maybe you mean one of:");
                System.err.println("");
                plugins.get("version").execute(new String[]{"--list-plugins"});
            }
        }
    }

    public Map<String, IkaPlugin> getPlugins() {
        return plugins;
    }
}