001package de.deepamehta.core.impl;
002
003import de.deepamehta.core.service.PluginInfo;
004import de.deepamehta.core.util.JavaUtils;
005
006import org.codehaus.jettison.json.JSONObject;
007import org.codehaus.jettison.json.JSONException;
008
009import org.osgi.framework.Bundle;
010
011import java.util.ArrayList;
012import java.util.Enumeration;
013import java.util.List;
014import java.util.logging.Logger;
015
016
017
018class PluginInfoImpl implements PluginInfo {
019
020    // ------------------------------------------------------------------------------------------------------- Constants
021
022    private static final String PLUGIN_FILE           = "/web/script/plugin.js";
023    private static final String PLUGIN_RENDERERS_PATH = "/web/script/renderers/";
024    private static final String PLUGIN_HELPER_PATH    = "/web/script/helper/";
025    private static final String PLUGIN_STYLE_PATH     = "/web/style/";
026
027    // ---------------------------------------------------------------------------------------------- Instance Variables
028
029    private Bundle pluginBundle;
030    private JSONObject pluginInfo = new JSONObject();
031
032    private Logger logger = Logger.getLogger(getClass().getName());
033
034    // ---------------------------------------------------------------------------------------------------- Constructors
035
036    PluginInfoImpl(String pluginUri, Bundle pluginBundle) {
037        this.pluginBundle = pluginBundle;
038        try {
039            pluginInfo.put("plugin_uri", pluginUri);
040            pluginInfo.put("has_plugin_file", hasPluginFile());
041            pluginInfo.put("stylesheets", getStylesheets());
042            pluginInfo.put("renderers", getRenderers());
043            pluginInfo.put("helper", getHelper());
044        } catch (Exception e) {
045            throw new RuntimeException("Serialization failed (" + this + ")", e);
046        }
047    }
048
049    // -------------------------------------------------------------------------------------------------- Public Methods
050
051    @Override
052    public JSONObject toJSON() {
053        return pluginInfo;
054    }
055
056    // ------------------------------------------------------------------------------------------------- Private Methods
057
058    private boolean hasPluginFile() {
059        return pluginBundle.getEntry(PLUGIN_FILE) != null;
060    }
061
062    private List<String> getStylesheets() {
063        return getFilenames(PLUGIN_STYLE_PATH);
064    }
065
066    private JSONObject getRenderers() throws JSONException {
067        JSONObject renderers = new JSONObject();
068        renderers.put("page_renderers",   getRenderers("page_renderers"));
069        renderers.put("simple_renderers", getRenderers("simple_renderers"));
070        renderers.put("multi_renderers",  getRenderers("multi_renderers"));
071        // ### TODO: renderers.put("topicmap_renderers", getRenderers("topicmap_renderers"));
072        return renderers;
073    }
074
075    private List<String> getHelper() {
076        return getFilenames(PLUGIN_HELPER_PATH);
077    }
078
079    // ---
080
081    private List<String> getRenderers(String renderersDir) {
082        return getFilenames(PLUGIN_RENDERERS_PATH + renderersDir);
083    }
084
085    private List<String> getFilenames(String path) {
086        List<String> filenames = new ArrayList();
087        Enumeration<String> e = pluginBundle.getEntryPaths(path);
088        if (e != null) {
089            while (e.hasMoreElements()) {
090                String entryPath = e.nextElement();
091                // ignore directories ### TODO: to be dropped? Use helper/ instead? See Webclient page_renderers
092                if (entryPath.endsWith("/")) {
093                    continue;
094                }
095                //
096                filenames.add(JavaUtils.getFilename(entryPath));
097            }
098        }
099        return filenames;
100    }
101}