Ignore:
Timestamp:
2010-01-12T18:27:40+01:00 (14 years ago)
Author:
Gubaer
Message:

fixed an ugly hack in the plugin bootstrap procedure

Location:
trunk/src/org/openstreetmap/josm/plugins
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/plugins/Plugin.java

    r2817 r2830  
    1515
    1616/**
    17  * All plugins *must* have an standard constructor taking no arguments.
    18  *
    19  * This constructor is called at JOSM startup, after all Main-objects have been initialized.
    2017 * For all purposes of loading dynamic resources, the Plugin's class loader should be used
    2118 * (or else, the plugin jar will not be within the class path).
     
    4744     * is a bit hacky, but it works).
    4845     */
    49     public final PluginInformation info = PluginInformation.currentPluginInitialization;
     46    private PluginInformation info = null;
     47
     48    /**
     49     * The no-arg constructor is deprecated.
     50     *
     51     * @deprecated use {@see Plugin(PluginInformation)} instead
     52     */
     53    @Deprecated
     54    public Plugin() {
     55
     56    }
     57    /**
     58     * Creates the plugin
     59     *
     60     * @param info the plugin information describing the plugin.
     61     */
     62    public Plugin(PluginInformation info) {
     63        this.info = info;
     64    }
     65
     66    /**
     67     * Replies the plugin information object for this plugin
     68     *
     69     * @return the plugin information object
     70     */
     71    public PluginInformation getPluginInformation() {
     72        return info;
     73    }
     74
     75    /**
     76     * Sets the plugin information object for this plugin
     77     *
     78     * @parma info the plugin information object
     79     */
     80    public void setPluginInformation(PluginInformation info) {
     81        this.info = info;
     82    }
    5083
    5184    /**
     
    79112     */
    80113    public void copy(String from, String to) throws FileNotFoundException, IOException {
    81         String pluginDirName = Main.pref.getPreferencesDir()+"plugins/"+info.name+"/";
     114        String pluginDirName = Main.pref.getPluginsDirectory() + "/" + info.name + "/";
    82115        File pluginDir = new File(pluginDirName);
    83116        if (!pluginDir.exists()) {
  • trunk/src/org/openstreetmap/josm/plugins/PluginHandler.java

    r2817 r2830  
    1 //License: GPL. Copyright 2007 by Immanuel Scholz and others
    21package org.openstreetmap.josm.plugins;
    32
     
    598597    public static Object getPlugin(String name) {
    599598        for (PluginProxy plugin : pluginList)
    600             if(plugin.info.name.equals(name))
     599            if(plugin.getPluginInformation().name.equals(name))
    601600                return plugin.plugin;
    602601        return null;
     
    666665            for (PluginProxy p : pluginList)
    667666            {
    668                 String baseClass = p.info.className;
     667                String baseClass = p.getPluginInformation().className;
    669668                int i = baseClass.lastIndexOf(".");
    670669                baseClass = baseClass.substring(0, i);
     
    693692            dialog.setContent(
    694693                    tr("<html>") +
    695                     tr("An unexpected exception occurred that may have come from the ''{0}'' plugin.", plugin.info.name)
     694                    tr("An unexpected exception occurred that may have come from the ''{0}'' plugin.", plugin.getPluginInformation().name)
    696695                    + "<br>"
    697                     + (plugin.info.author != null
    698                             ? tr("According to the information within the plugin, the author is {0}.", plugin.info.author)
     696                    + (plugin.getPluginInformation().author != null
     697                            ? tr("According to the information within the plugin, the author is {0}.", plugin.getPluginInformation().author)
    699698                                    : "")
    700699                                    + "<br>"
     
    709708            if (answer == 1) {
    710709                List<String> plugins = new ArrayList<String>(Main.pref.getCollection("plugins", Collections.<String>emptyList()));
    711                 if (plugins.contains(plugin.info.name)) {
    712                     while (plugins.remove(plugin.info.name)) {}
     710                if (plugins.contains(plugin.getPluginInformation().name)) {
     711                    while (plugins.remove(plugin.getPluginInformation().name)) {}
    713712                    Main.pref.putCollection("plugins", plugins);
    714713                    JOptionPane.showMessageDialog(Main.parent,
     
    738737        for (final PluginProxy pp : pluginList) {
    739738            text += "Plugin "
    740                 + pp.info.name
    741                 + (pp.info.version != null && !pp.info.version.equals("") ? " Version: " + pp.info.version + "\n"
     739                + pp.getPluginInformation().name
     740                + (pp.getPluginInformation().version != null && !pp.getPluginInformation().version.equals("") ? " Version: " + pp.getPluginInformation().version + "\n"
    742741                        : "\n");
    743742        }
     
    748747        JPanel pluginTab = new JPanel(new GridBagLayout());
    749748        for (final PluginProxy p : pluginList) {
    750             String name = p.info.name
    751             + (p.info.version != null && !p.info.version.equals("") ? " Version: " + p.info.version : "");
     749            final PluginInformation info = p.getPluginInformation();
     750            String name = info.name
     751            + (info.version != null && !info.version.equals("") ? " Version: " + info.version : "");
    752752            pluginTab.add(new JLabel(name), GBC.std());
    753753            pluginTab.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL));
     
    755755                public void actionPerformed(ActionEvent event) {
    756756                    StringBuilder b = new StringBuilder();
    757                     for (Entry<String, String> e : p.info.attr.entrySet()) {
     757                    for (Entry<String, String> e : info.attr.entrySet()) {
    758758                        b.append(e.getKey());
    759759                        b.append(": ");
     
    769769            }), GBC.eol());
    770770
    771             JTextArea description = new JTextArea((p.info.description == null ? tr("no description available")
    772                     : p.info.description));
     771            JTextArea description = new JTextArea((info.description == null ? tr("no description available")
     772                    : info.description));
    773773            description.setEditable(false);
    774774            description.setFont(new JLabel().getFont().deriveFont(Font.ITALIC));
  • trunk/src/org/openstreetmap/josm/plugins/PluginInformation.java

    r2817 r2830  
    88import java.io.IOException;
    99import java.io.InputStream;
     10import java.lang.reflect.Constructor;
     11import java.lang.reflect.InvocationTargetException;
     12import java.lang.reflect.Method;
    1013import java.net.MalformedURLException;
    1114import java.net.URL;
     
    4851
    4952    public final Map<String, String> attr = new TreeMap<String, String>();
    50 
    51     /**
    52      * Used in the Plugin constructor to make the information of the plugin
    53      * that is currently initializing available.
    54      *
    55      * If you think this is hacky, you are probably right. But it is
    56      * convinient anyway ;-)
    57      */
    58     static PluginInformation currentPluginInitialization = null;
    5953
    6054    /**
     
    193187    /**
    194188     * Load and instantiate the plugin
     189     *
     190     * @param the plugin class
     191     * @return the instantiated and initialized plugin
    195192     */
    196193    public PluginProxy load(Class<?> klass) throws PluginException{
    197194        try {
    198             currentPluginInitialization = this;
    199             return new PluginProxy(klass.newInstance(), this);
     195            try {
     196                Constructor<?> c = klass.getDeclaredConstructor(PluginInformation.class);
     197                Object plugin = c.newInstance(this);
     198                return new PluginProxy(plugin, this);
     199            } catch(NoSuchMethodException e) {
     200                // do nothing - try again with the noarg constructor for legacy support
     201            }
     202            // FIXME: This is legacy support. It is necessary because of a former ugly hack in the
     203            // plugin bootstrap procedure. Plugins should be migrated to the new required
     204            // constructor with PluginInformation as argument, new plugins should only use this
     205            // constructor. The following is legacy support and should be removed by Q2/2010.
     206            // Note that this is not fool proof because it isn't
     207            // completely equivalent with the former hack: plugins derived from the Plugin
     208            // class can't access their local "info" object any more from within the noarg-
     209            // constructor. It is only set *after* constructor invocation.
     210            //
     211            Constructor<?> c = klass.getConstructor();
     212            Object plugin = c.newInstance();
     213            if (plugin instanceof Plugin) {
     214                Method m = klass.getMethod("setPluginInformation", PluginInformation.class);
     215                m.invoke(plugin, this);
     216            }
     217            return new PluginProxy(plugin, this);
     218        } catch(NoSuchMethodException e) {
     219            throw new PluginException(name, e);
    200220        } catch(IllegalAccessException e) {
    201221            throw new PluginException(name, e);
    202222        } catch (InstantiationException e) {
    203223            throw new PluginException(name, e);
     224        } catch(InvocationTargetException e) {
     225            throw new PluginException(name, e);
    204226        }
    205227    }
     
    207229    /**
    208230     * Load the class of the plugin
     231     *
     232     * @param classLoader the class loader to use
     233     * @return the loaded class
    209234     */
    210235    public Class<?> loadClass(ClassLoader classLoader) throws PluginException {
     
    215240            return realClass;
    216241        } catch (ClassNotFoundException e) {
     242            throw new PluginException(name, e);
     243        } catch(ClassCastException e) {
    217244            throw new PluginException(name, e);
    218245        }
  • trunk/src/org/openstreetmap/josm/plugins/PluginProxy.java

    r1326 r2830  
    1919
    2020    public final Object plugin;
    21     public final PluginInformation info;
    2221
    2322    public PluginProxy(Object plugin, PluginInformation info) {
     23        super(info);
    2424        this.plugin = plugin;
    25         this.info = info;
    2625    }
    2726
     
    3130        } catch (NoSuchMethodException e) {
    3231        } catch (Exception e) {
    33             BugReportExceptionHandler.handleException(new PluginException(this, info.name, e));
     32            BugReportExceptionHandler.handleException(new PluginException(this, getPluginInformation().name, e));
    3433        }
    3534    }
     
    4140            return null;
    4241        } catch (Exception e) {
    43             BugReportExceptionHandler.handleException(new PluginException(this, info.name, e));
     42            BugReportExceptionHandler.handleException(new PluginException(this, getPluginInformation().name, e));
    4443        }
    4544        return null;
     
    5251            // ignore
    5352        } catch (Exception e) {
    54             BugReportExceptionHandler.handleException(new PluginException(this, info.name, e));
     53            BugReportExceptionHandler.handleException(new PluginException(this, getPluginInformation().name, e));
    5554        }
    5655    }
  • trunk/src/org/openstreetmap/josm/plugins/ReadLocalPluginInformationTask.java

    r2819 r2830  
    161161    protected void analyseInProcessPlugins() {
    162162        for (PluginProxy proxy : PluginHandler.pluginList) {
     163            PluginInformation info = proxy.getPluginInformation();
    163164            if (canceled)return;
    164             if (!availablePlugins.containsKey(proxy.info.name)) {
    165                 availablePlugins.put(proxy.info.name, proxy.info);
     165            if (!availablePlugins.containsKey(info.name)) {
     166                availablePlugins.put(info.name, info);
    166167            } else {
    167                 availablePlugins.get(proxy.info.name).localversion = proxy.info.version;
     168                availablePlugins.get(info.name).localversion = info.version;
    168169            }
    169170        }
Note: See TracChangeset for help on using the changeset viewer.