From 2a216122b3210f2ad162727bced66bc595a4b784 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Tue, 3 Apr 2007 16:18:04 +0000 Subject: [PATCH] throwing exceptions when trying to access the library initially --- tools/cooja/java/se/sics/cooja/CoreComm.java | 27 ++++++++++--------- .../cooja/contikimote/ContikiMoteType.java | 12 ++++++--- .../sics/cooja/mantismote/MantisMoteType.java | 11 +++++--- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/tools/cooja/java/se/sics/cooja/CoreComm.java b/tools/cooja/java/se/sics/cooja/CoreComm.java index da7e8c948..919161fb5 100644 --- a/tools/cooja/java/se/sics/cooja/CoreComm.java +++ b/tools/cooja/java/se/sics/cooja/CoreComm.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: CoreComm.java,v 1.5 2007/03/24 00:41:10 fros4943 Exp $ + * $Id: CoreComm.java,v 1.6 2007/04/03 16:18:44 fros4943 Exp $ */ package se.sics.cooja; @@ -34,6 +34,8 @@ import java.lang.reflect.*; import java.net.*; import java.util.Vector; +import se.sics.cooja.MoteType.MoteTypeCreationException; + /** * The purpose of corecomm's is communicating with a compiled Contiki system * using Java Native Interface (JNI). Each implemented class (named Lib[number]), @@ -207,15 +209,15 @@ public abstract class CoreComm { p = Runtime.getRuntime().exec(cmd, null, null); p.waitFor(); + if (classFile.exists()) + return true; + } catch (IOException e) { return false; } catch (InterruptedException e) { return false; } - if (classFile.exists()) - return true; - return false; } @@ -250,16 +252,17 @@ public abstract class CoreComm { * Native library file * @return Core Communicator */ - public static CoreComm createCoreComm(String className, File libFile) { + public static CoreComm createCoreComm(String className, File libFile) + throws MoteTypeCreationException { if (!generateLibSourceFile(className)) - return null; + throw new MoteTypeCreationException("Could not generate library source file: " + className); if (!compileSourceFile(className)) - return null; + throw new MoteTypeCreationException("Could not compile library: " + className); Class newCoreCommClass = loadClassFile(className); if (newCoreCommClass == null) - return null; + throw new MoteTypeCreationException("Could not load library class file: " + className); try { Constructor constr = newCoreCommClass.getConstructor(new Class[] { File.class }); @@ -271,13 +274,13 @@ public abstract class CoreComm { return newCoreComm; } catch (NoSuchMethodException e) { - return null; + throw new MoteTypeCreationException("Error when creating library instance: " + className); } catch (InstantiationException e) { - return null; + throw new MoteTypeCreationException("Error when creating library instance: " + className); } catch (InvocationTargetException e) { - return null; + throw new MoteTypeCreationException("Error when creating library instance: " + className); } catch (IllegalAccessException e) { - return null; + throw new MoteTypeCreationException("Error when creating library instance: " + className); } } diff --git a/tools/cooja/java/se/sics/cooja/contikimote/ContikiMoteType.java b/tools/cooja/java/se/sics/cooja/contikimote/ContikiMoteType.java index 24a279fee..f0baf8739 100644 --- a/tools/cooja/java/se/sics/cooja/contikimote/ContikiMoteType.java +++ b/tools/cooja/java/se/sics/cooja/contikimote/ContikiMoteType.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: ContikiMoteType.java,v 1.10 2007/04/02 12:45:19 fros4943 Exp $ + * $Id: ContikiMoteType.java,v 1.11 2007/04/03 16:18:05 fros4943 Exp $ */ package se.sics.cooja.contikimote; @@ -280,9 +280,13 @@ public class ContikiMoteType implements MoteType { throw new MoteTypeCreationException("Variable name to addresses mappings could not be created"); } - // Get offset between relative and absolute addresses - offsetRelToAbs = getReferenceAbsAddr() - - getRelVarAddr(mapFileData, "referenceVar"); + try { + // Get offset between relative and absolute addresses + offsetRelToAbs = getReferenceAbsAddr() + - getRelVarAddr(mapFileData, "referenceVar"); + } catch (Exception e) { + throw new MoteTypeCreationException("JNI call error: " + e.getMessage()); + } // Parse addresses of data and BSS memory sections int relDataSectionAddr = loadRelDataSectionAddr(mapFileData); diff --git a/tools/cooja/java/se/sics/cooja/mantismote/MantisMoteType.java b/tools/cooja/java/se/sics/cooja/mantismote/MantisMoteType.java index 193b89206..b56e518e8 100644 --- a/tools/cooja/java/se/sics/cooja/mantismote/MantisMoteType.java +++ b/tools/cooja/java/se/sics/cooja/mantismote/MantisMoteType.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: MantisMoteType.java,v 1.3 2007/03/23 23:34:33 fros4943 Exp $ + * $Id: MantisMoteType.java,v 1.4 2007/04/03 16:18:04 fros4943 Exp $ */ package se.sics.cooja.mantismote; @@ -134,8 +134,13 @@ public class MantisMoteType implements MoteType { // Allocate core communicator class libraryClassName = CoreComm.getAvailableClassName(); - myCoreComm = CoreComm.createCoreComm(libraryClassName, libFile); - + try { + myCoreComm = CoreComm.createCoreComm(libraryClassName, libFile); + } catch (MoteTypeCreationException e) { + logger.fatal("Library creation failed: " + e.getMessage()); + return false; + } + // Parse variable name to addresses mappings using nm varAddresses.clear(); Vector nmData = ContikiMoteType.loadNmData(libFile);