From 8a44b789348d4ae9ee6a37300d9f9da5ba359592 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Wed, 10 Jun 2009 15:57:08 +0000 Subject: [PATCH] + startPlugin method throws exceptions --- tools/cooja/java/se/sics/cooja/GUI.java | 184 ++++++++++-------- .../se/sics/cooja/plugins/EventListener.java | 12 +- .../se/sics/cooja/plugins/Visualizer.java | 12 +- 3 files changed, 119 insertions(+), 89 deletions(-) diff --git a/tools/cooja/java/se/sics/cooja/GUI.java b/tools/cooja/java/se/sics/cooja/GUI.java index 10ef164ec..54fe7d15a 100644 --- a/tools/cooja/java/se/sics/cooja/GUI.java +++ b/tools/cooja/java/se/sics/cooja/GUI.java @@ -24,7 +24,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $Id: GUI.java,v 1.132 2009/06/09 09:47:04 fros4943 Exp $ + * $Id: GUI.java,v 1.133 2009/06/10 15:57:08 fros4943 Exp $ */ package se.sics.cooja; @@ -120,6 +120,7 @@ import se.sics.cooja.dialogs.CreateSimDialog; import se.sics.cooja.dialogs.ExternalToolsDialog; import se.sics.cooja.dialogs.MessageList; import se.sics.cooja.dialogs.ProjectDirectoriesDialog; +import se.sics.cooja.plugins.MoteInterfaceViewer; import se.sics.cooja.plugins.MoteTypeInformation; import se.sics.cooja.plugins.ScriptRunner; import se.sics.cooja.plugins.SimControl; @@ -367,37 +368,26 @@ public class GUI extends Observable { });*/ // Register default project directories - String defaultProjectDirs = getExternalToolsSetting( - "DEFAULT_PROJECTDIRS", null); - if (defaultProjectDirs != null) { - if (!isVisualizedInApplet()) { - String[] defaultProjectDirsArr = defaultProjectDirs.split(";"); - if (defaultProjectDirsArr.length > 0) { - for (String defaultProjectDir : defaultProjectDirsArr) { - File projectDir = new File(defaultProjectDir); - if (projectDir.exists() && projectDir.isDirectory()) { - currentProjectDirs.add(projectDir); - } - } - } + String defaultProjectDirs = getExternalToolsSetting("DEFAULT_PROJECTDIRS", null); + if (defaultProjectDirs != null && defaultProjectDirs.length() > 0) { + String[] arr = defaultProjectDirs.split(";"); + for (String p : arr) { + File projectDir = new File(p); + currentProjectDirs.add(projectDir); } // Load extendable parts (using current project config) try { reparseProjectConfig(); } catch (ParseProjectsException e) { - logger.fatal("Error when loading projects: " + e.getMessage()); if (isVisualized()) { JOptionPane.showMessageDialog(GUI.getTopParentContainer(), - "Default projects could not load, try to reconfigure project directories:" + + "Default projects could not load, reconfigure project directories:" + "\n\tMenu->Settings->Manage project directories" + "\n\nSee console for stack trace with more information.", "Project loading error", JOptionPane.ERROR_MESSAGE); - } else { - logger.fatal("Loading project directories failed"); - logger.fatal("Stack trace:"); } - e.printStackTrace(); + logger.fatal("Error when loading projects", e); } } @@ -405,7 +395,7 @@ public class GUI extends Observable { for (Class pluginClass : pluginClasses) { int pluginType = pluginClass.getAnnotation(PluginType.class).value(); if (pluginType == PluginType.COOJA_STANDARD_PLUGIN) { - startPlugin(pluginClass, this, null, null); + tryStartPlugin(pluginClass, this, null, null); } } } @@ -1256,8 +1246,8 @@ public class GUI extends Observable { } // Backup temporary plugins - Vector> oldTempPlugins = (Vector>) pluginClassesTemporary - .clone(); + Vector> oldTempPlugins = + (Vector>) pluginClassesTemporary.clone(); // Reset current configuration unregisterMoteTypes(); @@ -1541,83 +1531,109 @@ public class GUI extends Observable { } /** - * Starts a plugin of given plugin class with given arguments. - * - * @param pluginClass - * Plugin class - * @param gui - * GUI passed as argument to all plugins - * @param simulation - * Simulation passed as argument to mote and simulation plugins - * @param mote - * Mote passed as argument to mote plugins - * @return Start plugin if any + * Same as the {@link #startPlugin(Class, GUI, Simulation, Mote)} method, + * but does not throw exceptions. If COOJA is visualised, an error dialog + * is shown if plugin could not be started. + * + * @see #startPlugin(Class, GUI, Simulation, Mote) + * @param pluginClass Plugin class + * @param argGUI Plugin GUI argument + * @param argSimulation Plugin simulation argument + * @param argMote Plugin mote argument + * @return Started plugin + */ + public Plugin tryStartPlugin(final Class pluginClass, + final GUI argGUI, final Simulation argSimulation, final Mote argMote) { + try { + return startPlugin(pluginClass, argGUI, argSimulation, argMote); + } catch (PluginConstructionException ex) { + if (GUI.isVisualized()) { + GUI.showErrorDialog(GUI.getTopParentContainer(), "Error when starting plugin", ex, false); + } else { + logger.fatal("Error when starting plugin", ex); + } + } + return null; + } + + /** + * Starts given plugin. If visualized, the plugin is also shown. + * + * @see PluginType + * @param pluginClass Plugin class + * @param argGUI Plugin GUI argument + * @param argSimulation Plugin simulation argument + * @param argMote Plugin mote argument + * @return Started plugin + * @throws PluginConstructionException At errors */ public Plugin startPlugin(final Class pluginClass, - final GUI gui, final Simulation simulation, final Mote mote) { + final GUI argGUI, final Simulation argSimulation, final Mote argMote) + throws PluginConstructionException + { // Check that plugin class is registered if (!pluginClasses.contains(pluginClass)) { - logger.fatal("Plugin class not registered: " + pluginClass); - return null; + throw new PluginConstructionException("Plugin class not registered: " + pluginClass); } // Construct plugin depending on plugin type int pluginType = pluginClass.getAnnotation(PluginType.class).value(); - Plugin plugin = null; + Plugin plugin; try { if (pluginType == PluginType.MOTE_PLUGIN) { - if (mote == null) { - logger.fatal("Can't start mote plugin (no mote selected)"); - return null; + if (argGUI == null) { + throw new PluginConstructionException("No GUI argument for mote plugin"); + } + if (argSimulation == null) { + throw new PluginConstructionException("No simulation argument for mote plugin"); + } + if (argMote == null) { + throw new PluginConstructionException("No mote argument for mote plugin"); } - plugin = pluginClass.getConstructor( - new Class[] { Mote.class, Simulation.class, GUI.class }) - .newInstance(mote, simulation, gui); + plugin = + pluginClass.getConstructor(new Class[] { Mote.class, Simulation.class, GUI.class }) + .newInstance(argMote, argSimulation, argGUI); + + /* Tag plugin with mote */ + plugin.tagWithObject(argMote); - // Tag plugin with mote - plugin.tagWithObject(mote); } else if (pluginType == PluginType.SIM_PLUGIN || pluginType == PluginType.SIM_STANDARD_PLUGIN) { - if (simulation == null) { - logger.fatal("Can't start simulation plugin (no simulation)"); - return null; + if (argGUI == null) { + throw new PluginConstructionException("No GUI argument for simulation plugin"); + } + if (argSimulation == null) { + throw new PluginConstructionException("No simulation argument for simulation plugin"); } - plugin = pluginClass.getConstructor( - new Class[] { Simulation.class, GUI.class }).newInstance( - simulation, gui); + plugin = + pluginClass.getConstructor(new Class[] { Simulation.class, GUI.class }) + .newInstance(argSimulation, argGUI); + } else if (pluginType == PluginType.COOJA_PLUGIN || pluginType == PluginType.COOJA_STANDARD_PLUGIN) { - if (gui == null) { - logger.fatal("Can't start COOJA plugin (no GUI)"); - return null; + if (argGUI == null) { + throw new PluginConstructionException("No GUI argument for GUI plugin"); } - plugin = pluginClass.getConstructor(new Class[] { GUI.class }).newInstance(gui); + plugin = + pluginClass.getConstructor(new Class[] { GUI.class }) + .newInstance(argGUI); + + } else { + throw new PluginConstructionException("Bad plugin type: " + pluginType); } } catch (PluginRequiresVisualizationException e) { - logger.info("Plugin not started (requires visualization): " + pluginClass.getName()); - return null; - } catch (InvocationTargetException e) { - if (e.getCause() != null && - e.getCause().getClass().equals(PluginRequiresVisualizationException.class)) { - logger.info("Plugin not started (requires visualization): " + pluginClass.getName()); - } else { - logger.fatal("Exception thrown when starting plugin: " + e); - e.printStackTrace(); - } - return null; + PluginConstructionException ex = new PluginConstructionException("Plugin class requires visualization: " + pluginClass.getName()); + ex.initCause(e); + throw ex; } catch (Exception e) { - logger.fatal("Exception thrown when starting plugin: " + e); - e.printStackTrace(); - return null; - } - - if (plugin == null) { - return null; + PluginConstructionException ex = new PluginConstructionException("Construction error for plugin of class: " + pluginClass.getName()); + ex.initCause(e); + throw ex; } // Add to active plugins list @@ -1759,7 +1775,7 @@ public class GUI extends Observable { menuItem = new JMenuItem(description); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - startPlugin(newPluginClass, myGUI, mySimulation, null); + tryStartPlugin(newPluginClass, myGUI, null, null); } }); } else if (pluginType == PluginType.SIM_PLUGIN || pluginType == PluginType.SIM_STANDARD_PLUGIN) { @@ -1875,7 +1891,7 @@ public class GUI extends Observable { for (Class pluginClass : pluginClasses) { int pluginType = pluginClass.getAnnotation(PluginType.class).value(); if (pluginType == PluginType.SIM_STANDARD_PLUGIN) { - startPlugin(pluginClass, this, sim, null); + tryStartPlugin(pluginClass, this, sim, null); } } } @@ -2950,7 +2966,10 @@ public class GUI extends Observable { File scriptFile = new File(config.substring(0, config.length()-4) + ".js"); if (scriptFile.exists()) { logger.info("Detected old simulation test, starting test editor manually from: " + scriptFile); - ScriptRunner plugin = (ScriptRunner) gui.startPlugin(ScriptRunner.class, gui, sim, null); + ScriptRunner plugin = (ScriptRunner) gui.tryStartPlugin(ScriptRunner.class, gui, sim, null); + if (plugin == null) { + System.exit(1); + } plugin.updateScript(scriptFile); plugin.setScriptActive(true); sim.setDelayTime(0); @@ -3352,9 +3371,8 @@ public class GUI extends Observable { } /* Start plugin */ - final Plugin startedPlugin = startPlugin(pluginClass, this, simulation, mote); + final Plugin startedPlugin = tryStartPlugin(pluginClass, this, simulation, mote); if (startedPlugin == null) { - logger.warn("Could not start plugin of class: " + pluginClass); continue; } @@ -3439,6 +3457,12 @@ public class GUI extends Observable { } } + public class PluginConstructionException extends Exception { + public PluginConstructionException(String message) { + super(message); + } + } + /** * A simple error dialog with compilation output and stack trace. * @@ -3909,7 +3933,7 @@ public class GUI extends Observable { Class pluginClass = (Class) ((JMenuItem) e.getSource()).getClientProperty("class"); Mote mote = (Mote) ((JMenuItem) e.getSource()).getClientProperty("mote"); - startPlugin(pluginClass, myGUI, mySimulation, mote); + tryStartPlugin(pluginClass, myGUI, mySimulation, mote); } public boolean shouldBeEnabled() { return getSimulation() != null; diff --git a/tools/cooja/java/se/sics/cooja/plugins/EventListener.java b/tools/cooja/java/se/sics/cooja/plugins/EventListener.java index 4822d230a..52ccd5b2f 100644 --- a/tools/cooja/java/se/sics/cooja/plugins/EventListener.java +++ b/tools/cooja/java/se/sics/cooja/plugins/EventListener.java @@ -24,7 +24,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $Id: EventListener.java,v 1.8 2009/03/09 15:39:33 fros4943 Exp $ + * $Id: EventListener.java,v 1.9 2009/06/10 15:57:08 fros4943 Exp $ */ package se.sics.cooja.plugins; @@ -37,6 +37,7 @@ import org.apache.log4j.Logger; import org.jdom.Element; import se.sics.cooja.*; +import se.sics.cooja.GUI.PluginConstructionException; import se.sics.cooja.contikimote.ContikiMoteType; import se.sics.cooja.interfaces.*; @@ -115,11 +116,10 @@ public class EventListener extends VisPlugin { + myParent.mySimulation.getSimulationTime(), new AbstractAction( "View interface visualizer") { public void actionPerformed(ActionEvent e) { - MoteInterfaceViewer plugin = (MoteInterfaceViewer) mySimulation - .getGUI().startPlugin(MoteInterfaceViewer.class, - mySimulation.getGUI(), mySimulation, myMote); - plugin.setSelectedInterface(GUI.getDescriptionOf(moteInterface - .getClass())); + MoteInterfaceViewer plugin = + (MoteInterfaceViewer) mySimulation.getGUI().tryStartPlugin( + MoteInterfaceViewer.class, mySimulation.getGUI(), mySimulation, myMote); + plugin.setSelectedInterface(GUI.getDescriptionOf(moteInterface.getClass())); } }); } diff --git a/tools/cooja/java/se/sics/cooja/plugins/Visualizer.java b/tools/cooja/java/se/sics/cooja/plugins/Visualizer.java index 2334095ce..ef2b04c7b 100644 --- a/tools/cooja/java/se/sics/cooja/plugins/Visualizer.java +++ b/tools/cooja/java/se/sics/cooja/plugins/Visualizer.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: Visualizer.java,v 1.6 2009/05/18 13:57:51 nifi Exp $ + * $Id: Visualizer.java,v 1.7 2009/06/10 15:57:08 fros4943 Exp $ */ package se.sics.cooja.plugins; @@ -1008,11 +1008,14 @@ public class Visualizer extends VisPlugin { String desc = GUI.getDescriptionOf(mote.getInterfaces().getLED()); MoteInterfaceViewer viewer = - (MoteInterfaceViewer) simulation.getGUI().startPlugin( + (MoteInterfaceViewer) simulation.getGUI().tryStartPlugin( MoteInterfaceViewer.class, simulation.getGUI(), simulation, mote); + if (viewer == null) { + return; + } viewer.setSelectedInterface(desc); viewer.pack(); } @@ -1056,11 +1059,14 @@ public class Visualizer extends VisPlugin { String desc = GUI.getDescriptionOf(serialPort); MoteInterfaceViewer viewer = - (MoteInterfaceViewer) simulation.getGUI().startPlugin( + (MoteInterfaceViewer) simulation.getGUI().tryStartPlugin( MoteInterfaceViewer.class, simulation.getGUI(), simulation, mote); + if (viewer == null) { + return; + } viewer.setSelectedInterface(desc); viewer.pack(); }