+ startPlugin method throws exceptions

This commit is contained in:
fros4943 2009-06-10 15:57:08 +00:00
parent 53a8acc49a
commit 8a44b78934
3 changed files with 119 additions and 89 deletions

View file

@ -24,7 +24,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * 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; 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.ExternalToolsDialog;
import se.sics.cooja.dialogs.MessageList; import se.sics.cooja.dialogs.MessageList;
import se.sics.cooja.dialogs.ProjectDirectoriesDialog; import se.sics.cooja.dialogs.ProjectDirectoriesDialog;
import se.sics.cooja.plugins.MoteInterfaceViewer;
import se.sics.cooja.plugins.MoteTypeInformation; import se.sics.cooja.plugins.MoteTypeInformation;
import se.sics.cooja.plugins.ScriptRunner; import se.sics.cooja.plugins.ScriptRunner;
import se.sics.cooja.plugins.SimControl; import se.sics.cooja.plugins.SimControl;
@ -367,37 +368,26 @@ public class GUI extends Observable {
});*/ });*/
// Register default project directories // Register default project directories
String defaultProjectDirs = getExternalToolsSetting( String defaultProjectDirs = getExternalToolsSetting("DEFAULT_PROJECTDIRS", null);
"DEFAULT_PROJECTDIRS", null); if (defaultProjectDirs != null && defaultProjectDirs.length() > 0) {
if (defaultProjectDirs != null) { String[] arr = defaultProjectDirs.split(";");
if (!isVisualizedInApplet()) { for (String p : arr) {
String[] defaultProjectDirsArr = defaultProjectDirs.split(";"); File projectDir = new File(p);
if (defaultProjectDirsArr.length > 0) {
for (String defaultProjectDir : defaultProjectDirsArr) {
File projectDir = new File(defaultProjectDir);
if (projectDir.exists() && projectDir.isDirectory()) {
currentProjectDirs.add(projectDir); currentProjectDirs.add(projectDir);
} }
}
}
}
// Load extendable parts (using current project config) // Load extendable parts (using current project config)
try { try {
reparseProjectConfig(); reparseProjectConfig();
} catch (ParseProjectsException e) { } catch (ParseProjectsException e) {
logger.fatal("Error when loading projects: " + e.getMessage());
if (isVisualized()) { if (isVisualized()) {
JOptionPane.showMessageDialog(GUI.getTopParentContainer(), 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\tMenu->Settings->Manage project directories" +
"\n\nSee console for stack trace with more information.", "\n\nSee console for stack trace with more information.",
"Project loading error", JOptionPane.ERROR_MESSAGE); "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<? extends Plugin> pluginClass : pluginClasses) { for (Class<? extends Plugin> pluginClass : pluginClasses) {
int pluginType = pluginClass.getAnnotation(PluginType.class).value(); int pluginType = pluginClass.getAnnotation(PluginType.class).value();
if (pluginType == PluginType.COOJA_STANDARD_PLUGIN) { 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 // Backup temporary plugins
Vector<Class<? extends Plugin>> oldTempPlugins = (Vector<Class<? extends Plugin>>) pluginClassesTemporary Vector<Class<? extends Plugin>> oldTempPlugins =
.clone(); (Vector<Class<? extends Plugin>>) pluginClassesTemporary.clone();
// Reset current configuration // Reset current configuration
unregisterMoteTypes(); unregisterMoteTypes();
@ -1541,83 +1531,109 @@ public class GUI extends Observable {
} }
/** /**
* Starts a plugin of given plugin class with given arguments. * 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.
* *
* @param pluginClass * @see #startPlugin(Class, GUI, Simulation, Mote)
* Plugin class * @param pluginClass Plugin class
* @param gui * @param argGUI Plugin GUI argument
* GUI passed as argument to all plugins * @param argSimulation Plugin simulation argument
* @param simulation * @param argMote Plugin mote argument
* Simulation passed as argument to mote and simulation plugins * @return Started plugin
* @param mote */
* Mote passed as argument to mote plugins public Plugin tryStartPlugin(final Class<? extends Plugin> pluginClass,
* @return Start plugin if any 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<? extends Plugin> pluginClass, public Plugin startPlugin(final Class<? extends Plugin> 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 // Check that plugin class is registered
if (!pluginClasses.contains(pluginClass)) { if (!pluginClasses.contains(pluginClass)) {
logger.fatal("Plugin class not registered: " + pluginClass); throw new PluginConstructionException("Plugin class not registered: " + pluginClass);
return null;
} }
// Construct plugin depending on plugin type // Construct plugin depending on plugin type
int pluginType = pluginClass.getAnnotation(PluginType.class).value(); int pluginType = pluginClass.getAnnotation(PluginType.class).value();
Plugin plugin = null; Plugin plugin;
try { try {
if (pluginType == PluginType.MOTE_PLUGIN) { if (pluginType == PluginType.MOTE_PLUGIN) {
if (mote == null) { if (argGUI == null) {
logger.fatal("Can't start mote plugin (no mote selected)"); throw new PluginConstructionException("No GUI argument for mote plugin");
return null; }
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( plugin =
new Class[] { Mote.class, Simulation.class, GUI.class }) pluginClass.getConstructor(new Class[] { Mote.class, Simulation.class, GUI.class })
.newInstance(mote, simulation, gui); .newInstance(argMote, argSimulation, argGUI);
/* Tag plugin with mote */
plugin.tagWithObject(argMote);
// Tag plugin with mote
plugin.tagWithObject(mote);
} else if (pluginType == PluginType.SIM_PLUGIN } else if (pluginType == PluginType.SIM_PLUGIN
|| pluginType == PluginType.SIM_STANDARD_PLUGIN) { || pluginType == PluginType.SIM_STANDARD_PLUGIN) {
if (simulation == null) { if (argGUI == null) {
logger.fatal("Can't start simulation plugin (no simulation)"); throw new PluginConstructionException("No GUI argument for simulation plugin");
return null; }
if (argSimulation == null) {
throw new PluginConstructionException("No simulation argument for simulation plugin");
} }
plugin = pluginClass.getConstructor( plugin =
new Class[] { Simulation.class, GUI.class }).newInstance( pluginClass.getConstructor(new Class[] { Simulation.class, GUI.class })
simulation, gui); .newInstance(argSimulation, argGUI);
} else if (pluginType == PluginType.COOJA_PLUGIN } else if (pluginType == PluginType.COOJA_PLUGIN
|| pluginType == PluginType.COOJA_STANDARD_PLUGIN) { || pluginType == PluginType.COOJA_STANDARD_PLUGIN) {
if (gui == null) { if (argGUI == null) {
logger.fatal("Can't start COOJA plugin (no GUI)"); throw new PluginConstructionException("No GUI argument for GUI plugin");
return null;
} }
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) { } catch (PluginRequiresVisualizationException e) {
logger.info("Plugin not started (requires visualization): " + pluginClass.getName()); PluginConstructionException ex = new PluginConstructionException("Plugin class requires visualization: " + pluginClass.getName());
return null; ex.initCause(e);
} catch (InvocationTargetException e) { throw ex;
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;
} catch (Exception e) { } catch (Exception e) {
logger.fatal("Exception thrown when starting plugin: " + e); PluginConstructionException ex = new PluginConstructionException("Construction error for plugin of class: " + pluginClass.getName());
e.printStackTrace(); ex.initCause(e);
return null; throw ex;
}
if (plugin == null) {
return null;
} }
// Add to active plugins list // Add to active plugins list
@ -1759,7 +1775,7 @@ public class GUI extends Observable {
menuItem = new JMenuItem(description); menuItem = new JMenuItem(description);
menuItem.addActionListener(new ActionListener() { menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { 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) { } else if (pluginType == PluginType.SIM_PLUGIN || pluginType == PluginType.SIM_STANDARD_PLUGIN) {
@ -1875,7 +1891,7 @@ public class GUI extends Observable {
for (Class<? extends Plugin> pluginClass : pluginClasses) { for (Class<? extends Plugin> pluginClass : pluginClasses) {
int pluginType = pluginClass.getAnnotation(PluginType.class).value(); int pluginType = pluginClass.getAnnotation(PluginType.class).value();
if (pluginType == PluginType.SIM_STANDARD_PLUGIN) { 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"); File scriptFile = new File(config.substring(0, config.length()-4) + ".js");
if (scriptFile.exists()) { if (scriptFile.exists()) {
logger.info("Detected old simulation test, starting test editor manually from: " + scriptFile); 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.updateScript(scriptFile);
plugin.setScriptActive(true); plugin.setScriptActive(true);
sim.setDelayTime(0); sim.setDelayTime(0);
@ -3352,9 +3371,8 @@ public class GUI extends Observable {
} }
/* Start plugin */ /* Start plugin */
final Plugin startedPlugin = startPlugin(pluginClass, this, simulation, mote); final Plugin startedPlugin = tryStartPlugin(pluginClass, this, simulation, mote);
if (startedPlugin == null) { if (startedPlugin == null) {
logger.warn("Could not start plugin of class: " + pluginClass);
continue; 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. * A simple error dialog with compilation output and stack trace.
* *
@ -3909,7 +3933,7 @@ public class GUI extends Observable {
Class<Plugin> pluginClass = Class<Plugin> pluginClass =
(Class<Plugin>) ((JMenuItem) e.getSource()).getClientProperty("class"); (Class<Plugin>) ((JMenuItem) e.getSource()).getClientProperty("class");
Mote mote = (Mote) ((JMenuItem) e.getSource()).getClientProperty("mote"); Mote mote = (Mote) ((JMenuItem) e.getSource()).getClientProperty("mote");
startPlugin(pluginClass, myGUI, mySimulation, mote); tryStartPlugin(pluginClass, myGUI, mySimulation, mote);
} }
public boolean shouldBeEnabled() { public boolean shouldBeEnabled() {
return getSimulation() != null; return getSimulation() != null;

View file

@ -24,7 +24,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * 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; package se.sics.cooja.plugins;
@ -37,6 +37,7 @@ import org.apache.log4j.Logger;
import org.jdom.Element; import org.jdom.Element;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.GUI.PluginConstructionException;
import se.sics.cooja.contikimote.ContikiMoteType; import se.sics.cooja.contikimote.ContikiMoteType;
import se.sics.cooja.interfaces.*; import se.sics.cooja.interfaces.*;
@ -115,11 +116,10 @@ public class EventListener extends VisPlugin {
+ myParent.mySimulation.getSimulationTime(), new AbstractAction( + myParent.mySimulation.getSimulationTime(), new AbstractAction(
"View interface visualizer") { "View interface visualizer") {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
MoteInterfaceViewer plugin = (MoteInterfaceViewer) mySimulation MoteInterfaceViewer plugin =
.getGUI().startPlugin(MoteInterfaceViewer.class, (MoteInterfaceViewer) mySimulation.getGUI().tryStartPlugin(
mySimulation.getGUI(), mySimulation, myMote); MoteInterfaceViewer.class, mySimulation.getGUI(), mySimulation, myMote);
plugin.setSelectedInterface(GUI.getDescriptionOf(moteInterface plugin.setSelectedInterface(GUI.getDescriptionOf(moteInterface.getClass()));
.getClass()));
} }
}); });
} }

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * 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; package se.sics.cooja.plugins;
@ -1008,11 +1008,14 @@ public class Visualizer extends VisPlugin {
String desc = GUI.getDescriptionOf(mote.getInterfaces().getLED()); String desc = GUI.getDescriptionOf(mote.getInterfaces().getLED());
MoteInterfaceViewer viewer = MoteInterfaceViewer viewer =
(MoteInterfaceViewer) simulation.getGUI().startPlugin( (MoteInterfaceViewer) simulation.getGUI().tryStartPlugin(
MoteInterfaceViewer.class, MoteInterfaceViewer.class,
simulation.getGUI(), simulation.getGUI(),
simulation, simulation,
mote); mote);
if (viewer == null) {
return;
}
viewer.setSelectedInterface(desc); viewer.setSelectedInterface(desc);
viewer.pack(); viewer.pack();
} }
@ -1056,11 +1059,14 @@ public class Visualizer extends VisPlugin {
String desc = GUI.getDescriptionOf(serialPort); String desc = GUI.getDescriptionOf(serialPort);
MoteInterfaceViewer viewer = MoteInterfaceViewer viewer =
(MoteInterfaceViewer) simulation.getGUI().startPlugin( (MoteInterfaceViewer) simulation.getGUI().tryStartPlugin(
MoteInterfaceViewer.class, MoteInterfaceViewer.class,
simulation.getGUI(), simulation.getGUI(),
simulation, simulation,
mote); mote);
if (viewer == null) {
return;
}
viewer.setSelectedInterface(desc); viewer.setSelectedInterface(desc);
viewer.pack(); viewer.pack();
} }