Author: jflesch
Date: 2007-12-03 19:04:21 +0000 (Mon, 03 Dec 2007)
New Revision: 16233

Modified:
   trunk/apps/Thaw/src/thaw/core/ConfigWindow.java
   trunk/apps/Thaw/src/thaw/core/Core.java
   trunk/apps/Thaw/src/thaw/core/PluginManager.java
Log:
Plugin manager : Improvement of the synchronization ; should avoid that some 
plugins are loaded many times

Modified: trunk/apps/Thaw/src/thaw/core/ConfigWindow.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/ConfigWindow.java     2007-12-03 18:30:07 UTC 
(rev 16232)
+++ trunk/apps/Thaw/src/thaw/core/ConfigWindow.java     2007-12-03 19:04:21 UTC 
(rev 16233)
@@ -206,9 +206,11 @@
                        setChanged();
                        notifyObservers(okButton);

-                       Reloader reloader = new Reloader(needConnectionReset);
-                       Thread reload = new ThawThread(reloader, "Config 
reloader", this);
-                       reload.start();
+                       synchronized(PluginManager.pluginLock) {
+                               Reloader reloader = new 
Reloader(needConnectionReset);
+                               Thread reload = new ThawThread(reloader, 
"Config reloader", this);
+                               reload.start();
+                       }

                        needConnectionReset = false;
                }
@@ -232,7 +234,7 @@
                        this.running = true;
                }

-               public void run() {
+               public void apply() {
                        JDialog dialog = null;

                        if (resetConnection) {
@@ -303,6 +305,12 @@
                        if (resetConnection)
                                dialog.setVisible(false);
                }
+               
+               public void run() {
+                       synchronized(PluginManager.pluginLock) {
+                               apply();
+                       }
+               }

                public void stop() {
                        running = false;

Modified: trunk/apps/Thaw/src/thaw/core/Core.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/Core.java     2007-12-03 18:30:07 UTC (rev 
16232)
+++ trunk/apps/Thaw/src/thaw/core/Core.java     2007-12-03 19:04:21 UTC (rev 
16233)
@@ -610,43 +610,45 @@
                }

                public void run() {
-                       Logger.notice(this, "Starting reconnection process !");
-
-                       getMainWindow().setStatus(IconBox.blueBunny,
-                                                 
I18n.getMessage("thaw.statusBar.connecting"), java.awt.Color.RED);
-                       getPluginManager().stopPlugins(); /* don't forget there 
is the status bar plugin */
-                       getMainWindow().setStatus(IconBox.blueBunny,
-                                                 
I18n.getMessage("thaw.statusBar.connecting"), java.awt.Color.RED);
-
-                       subDisconnect();
-
-                       while(running) {
-                               try {
-                                       
Thread.sleep(Core.TIME_BETWEEN_EACH_TRY);
-                               } catch(final java.lang.InterruptedException e) 
{
-                                       // brouzouf
+                       synchronized(PluginManager.pluginLock) {
+                               Logger.notice(this, "Starting reconnection 
process !");
+       
+                               getMainWindow().setStatus(IconBox.blueBunny,
+                                                         
I18n.getMessage("thaw.statusBar.connecting"), java.awt.Color.RED);
+                               getPluginManager().stopPlugins(); /* don't 
forget there is the status bar plugin */
+                               getMainWindow().setStatus(IconBox.blueBunny,
+                                                         
I18n.getMessage("thaw.statusBar.connecting"), java.awt.Color.RED);
+       
+                               subDisconnect();
+       
+                               while(running) {
+                                       try {
+                                               
Thread.sleep(Core.TIME_BETWEEN_EACH_TRY);
+                                       } catch(final 
java.lang.InterruptedException e) {
+                                               // brouzouf
+                                       }
+       
+                                       Logger.notice(this, "Trying to 
reconnect ...");
+                                       if(initConnection())
+                                               break;
                                }
-
-                               Logger.notice(this, "Trying to reconnect ...");
-                               if(initConnection())
-                                       break;
+       
+                               if (running) {
+                                       
getMainWindow().setStatus(IconBox.minConnectAction,
+                                                                 
I18n.getMessage("thaw.statusBar.ready"));
+                               } else {
+                                       
getMainWindow().setStatus(IconBox.minDisconnectAction,
+                                                                 
I18n.getMessage("thaw.statusBar.disconnected"), java.awt.Color.RED);
+                               }
+       
+                               if (running) {
+                                       getPluginManager().loadAndRunPlugins();
+                               }
+       
+                               reconnectionManager = null;
+       
+                               getMainWindow().connectionHasChanged();
                        }
-
-                       if (running) {
-                               
getMainWindow().setStatus(IconBox.minConnectAction,
-                                                         
I18n.getMessage("thaw.statusBar.ready"));
-                       } else {
-                               
getMainWindow().setStatus(IconBox.minDisconnectAction,
-                                                         
I18n.getMessage("thaw.statusBar.disconnected"), java.awt.Color.RED);
-                       }
-
-                       if (running) {
-                               getPluginManager().loadAndRunPlugins();
-                       }
-
-                       reconnectionManager = null;
-
-                       getMainWindow().connectionHasChanged();
                }

                public void stop() {
@@ -659,13 +661,15 @@
         * use Thread => will also do all the work related to the plugins
         */
        public void reconnect() {
-               if (reconnectionManager == null) {
-                       reconnectionManager = new ReconnectionManager();
-                       final Thread th = new ThawThread(reconnectionManager,
-                                                        "Reconnection 
manager", this);
-                       th.start();
-               } else {
-                       Logger.warning(this, "Already trying to reconnect !");
+               synchronized(this) {
+                       if (reconnectionManager == null) {
+                               reconnectionManager = new ReconnectionManager();
+                               final Thread th = new 
ThawThread(reconnectionManager,
+                                                                "Reconnection 
manager", this);
+                               th.start();
+                       } else {
+                               Logger.warning(this, "Already trying to 
reconnect !");
+                       }
                }
        }


Modified: trunk/apps/Thaw/src/thaw/core/PluginManager.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/PluginManager.java    2007-12-03 18:30:07 UTC 
(rev 16232)
+++ trunk/apps/Thaw/src/thaw/core/PluginManager.java    2007-12-03 19:04:21 UTC 
(rev 16233)
@@ -48,6 +48,7 @@
        // LinkedHashMap because I want to keep a predictible plugin order.
        private LinkedHashMap plugins = null; // String (pluginName) -> Plugin

+       public final static Object pluginLock = new Object();

        /**
         * Need a ref to the core to pass it to the plugins (and to access 
config)
@@ -75,38 +76,40 @@
         * Load plugin from config or from default list.
         * Reload if already loaded.
         */
-       public synchronized boolean loadAndRunPlugins() {
-               plugins = new LinkedHashMap();
+       public boolean loadAndRunPlugins() {
+               synchronized(pluginLock) {
+                       plugins = new LinkedHashMap();

-               Vector pluginNames;
+                       Vector pluginNames;

-               if(core.getConfig().getPluginNames().size() == 0) {
-                       Logger.notice(this, "Loading default plugin list");
-                       /* Then we load the config with the default plugins */
-                       for(int i = 0 ; i < PluginManager.defaultPlugins.length 
; i++) {
-                               
core.getConfig().addPlugin(PluginManager.defaultPlugins[i]);
+                       if(core.getConfig().getPluginNames().size() == 0) {
+                               Logger.notice(this, "Loading default plugin 
list");
+                               /* Then we load the config with the default 
plugins */
+                               for(int i = 0 ; i < 
PluginManager.defaultPlugins.length ; i++) {
+                                       
core.getConfig().addPlugin(PluginManager.defaultPlugins[i]);
+                               }
                        }
-               }

-               /* we duplicate the vector to avoid collisions */
-               /* (remember : plugins can load other plugins */
-               pluginNames = new Vector(core.getConfig().getPluginNames());
+                       /* we duplicate the vector to avoid collisions */
+                       /* (remember : plugins can load other plugins */
+                       pluginNames = new 
Vector(core.getConfig().getPluginNames());

-               final Iterator pluginIt = pluginNames.iterator();
+                       final Iterator pluginIt = pluginNames.iterator();

-               final int progressJump = (100-40) / pluginNames.size();
-               core.getSplashScreen().setProgression(40);
+                       final int progressJump = (100-40) / pluginNames.size();
+                       core.getSplashScreen().setProgression(40);

-               while(pluginIt.hasNext()) {
-                       final String pluginName = (String)pluginIt.next();
+                       while(pluginIt.hasNext()) {
+                               final String pluginName = 
(String)pluginIt.next();

-                       
core.getSplashScreen().setProgressionAndStatus(core.getSplashScreen().getProgression()+progressJump,
-                                                                      "Loading 
plugin '"+pluginName.replaceFirst("thaw.plugins.", "")+"' ...");
+                               
core.getSplashScreen().setProgressionAndStatus(core.getSplashScreen().getProgression()+progressJump,
+                                                                               
                                                "Loading plugin 
'"+pluginName.replaceFirst("thaw.plugins.", "")+"' ...");

-                       if (loadPlugin(pluginName) == null) {
-                               Logger.notice(this, "Plugin alread loaded");
-                       } else {
-                               runPlugin(pluginName);
+                               if (loadPlugin(pluginName) == null) {
+                                       Logger.notice(this, "Plugin alread 
loaded");
+                               } else {
+                                       runPlugin(pluginName);
+                               }
                        }
                }

@@ -117,136 +120,145 @@
        /**
         * Stop all plugins.
         */
-       public synchronized boolean stopPlugins() {
-               Iterator pluginIt;
+       public boolean stopPlugins() {
+               synchronized(pluginLock) {
+                       Iterator pluginIt;

-               if (plugins == null) {
-                       Logger.error(this, "No plugin to stop ?!");
-                       return false;
-               }
+                       if (plugins == null) {
+                               Logger.error(this, "No plugin to stop ?!");
+                               return false;
+                       }


-               pluginIt = plugins.values().iterator();
+                       pluginIt = plugins.values().iterator();

-               while(pluginIt.hasNext()) {
-                       final Plugin plugin = (Plugin)pluginIt.next();
+                       while(pluginIt.hasNext()) {
+                               final Plugin plugin = (Plugin)pluginIt.next();

-                       try {
-                               Logger.info(this, "Stopping plugin 
'"+plugin.getClass().getName()+"'");
+                               try {
+                                       Logger.info(this, "Stopping plugin 
'"+plugin.getClass().getName()+"'");

-                               if (plugin != null)
-                                       plugin.stop();
-                               else
-                                       Logger.error(this, "Plugin == null !?");
-                       } catch(final Exception e) {
-                               Logger.error(this, "Unable to stop the plugin "+
-                                            
"'"+plugin.getClass().getName()+"'"+
-                                            ", because: "+e.toString());
-                               e.printStackTrace();
+                                       if (plugin != null)
+                                               plugin.stop();
+                                       else
+                                               Logger.error(this, "Plugin == 
null !?");
+                               } catch(final Exception e) {
+                                       Logger.error(this, "Unable to stop the 
plugin "+
+                                                       
"'"+plugin.getClass().getName()+"'"+
+                                                       ", because: 
"+e.toString());
+                                       e.printStackTrace();
+                               }
                        }
+
+                       return true;
                }
-
-               return true;
        }

-
        /**
         * Load a given plugin (without adding it to the config or running it).
         */
-       public synchronized Plugin loadPlugin(final String className) {
-               Plugin plugin = null;
-
-               Logger.info(this, "Loading plugin: '"+className+"'");
-
-               try {
-                       if ( plugins.get(className) != null) {
-                               Logger.debug(this, "loadPlugin(): Plugin 
'"+className+"' already loaded");
+       public Plugin loadPlugin(final String className) {
+               synchronized(pluginLock) {
+                       Plugin plugin = null;
+       
+                       Logger.info(this, "Loading plugin: '"+className+"'");
+       
+                       try {
+                               if ( plugins.get(className) != null) {
+                                       Logger.debug(this, "loadPlugin(): 
Plugin '"+className+"' already loaded");
+                                       return null;
+                               }
+       
+                               //Logger.info(this, "Loading plugin 
'"+className+"'");
+       
+                               plugin = 
(Plugin)Class.forName(className).newInstance();
+       
+                               plugins.put(className, plugin);
+       
+                       } catch(final Exception e) {
+                               Logger.error(this, 
"loadPlugin('"+className+"'): Exception: "+e);
+                               e.printStackTrace();
                                return null;
                        }
-
-                       //Logger.info(this, "Loading plugin '"+className+"'");
-
-                       plugin = (Plugin)Class.forName(className).newInstance();
-
-                       plugins.put(className, plugin);
-
-               } catch(final Exception e) {
-                       Logger.error(this, "loadPlugin('"+className+"'): 
Exception: "+e);
-                       e.printStackTrace();
-                       return null;
+       
+                       return plugin;
                }
-
-               return plugin;
        }


        /**
         * Run a given plugin.
         */
-       public synchronized boolean runPlugin(final String className) {
-               Logger.info(this, "Starting plugin: '"+className+"'");
-
-               try {
-                       Plugin plugin = (Plugin)plugins.get(className);
-
-                       javax.swing.ImageIcon icon;
-
-                       if ((icon = plugin.getIcon()) != null)
-                               core.getSplashScreen().addIcon(icon);
-                       else
-                               
core.getSplashScreen().addIcon(thaw.gui.IconBox.add);
-
-                       plugin.run(core);
-
-               } catch(final Exception e) {
-                       Logger.error(this, "runPlugin('"+className+"'): 
Exception: "+e);
-                       e.printStackTrace();
-                       return false;
+       public boolean runPlugin(final String className) {
+               synchronized(pluginLock) {
+                       Logger.info(this, "Starting plugin: '"+className+"'");
+       
+                       try {
+                               Plugin plugin = (Plugin)plugins.get(className);
+       
+                               javax.swing.ImageIcon icon;
+       
+                               if ((icon = plugin.getIcon()) != null)
+                                       core.getSplashScreen().addIcon(icon);
+                               else
+                                       
core.getSplashScreen().addIcon(thaw.gui.IconBox.add);
+       
+                               plugin.run(core);
+       
+                       } catch(final Exception e) {
+                               Logger.error(this, "runPlugin('"+className+"'): 
Exception: "+e);
+                               e.printStackTrace();
+                               return false;
+                       }
+       
+                       return true;
                }
-
-               return true;
        }


        /**
         * Stop a given plugin.
         */
-       public synchronized boolean stopPlugin(final String className) {
-               Logger.info(this, "Stopping plugin: '"+className+"'");
-
-               try {
-                       ((Plugin)plugins.get(className)).stop();
-
-               } catch(final Exception e) {
-                       Logger.error(this, "stopPlugin('"+className+"'): 
Exception: "+e);
-                       e.printStackTrace();
-                       return false;
+       public boolean stopPlugin(final String className) {
+               synchronized(pluginLock) {
+                       Logger.info(this, "Stopping plugin: '"+className+"'");
+       
+                       try {
+                               ((Plugin)plugins.get(className)).stop();
+       
+                       } catch(final Exception e) {
+                               Logger.error(this, 
"stopPlugin('"+className+"'): Exception: "+e);
+                               e.printStackTrace();
+                               return false;
+                       }
+       
+                       return true;
                }
-
-               return true;
        }


        /**
         * Unload a given plugin (without adding it to the config or running 
it).
         */
-       public synchronized boolean unloadPlugin(final String className) {
-               try {
-                       if(plugins.get(className) == null) {
-                               Logger.notice(this, "unloadPlugin(): Plugin 
'"+className+"' already unloaded");
+       public boolean unloadPlugin(final String className) {
+               synchronized(pluginLock) {
+                       try {
+                               if(plugins.get(className) == null) {
+                                       Logger.notice(this, "unloadPlugin(): 
Plugin '"+className+"' already unloaded");
+                                       return false;
+                               }
+
+                               plugins.remove(className);
+                               core.getConfig().removePlugin(className);
+
+                       } catch(final Exception e) {
+                               Logger.error(this, 
"unloadPlugin('"+className+"'): Exception: "+e);
+                               e.printStackTrace();
                                return false;
                        }

-                       plugins.remove(className);
-                       core.getConfig().removePlugin(className);
-
-               } catch(final Exception e) {
-                       Logger.error(this, "unloadPlugin('"+className+"'): 
Exception: "+e);
-                       e.printStackTrace();
-                       return false;
+                       return true;
                }
-
-               return true;
        }

        public Plugin getPlugin(final String className) {


Reply via email to