restructured corecomm to throw exceptions instead of returning success of operation
This commit is contained in:
parent
80ec85abad
commit
ca6b1d4bf8
1 changed files with 73 additions and 51 deletions
|
@ -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.9 2007/05/11 10:02:13 fros4943 Exp $
|
||||
* $Id: CoreComm.java,v 1.10 2007/05/11 10:15:42 fros4943 Exp $
|
||||
*/
|
||||
|
||||
package se.sics.cooja;
|
||||
|
@ -39,18 +39,19 @@ import se.sics.cooja.dialogs.MessageList;
|
|||
|
||||
/**
|
||||
* The purpose of corecomm's is communicating with a compiled Contiki system
|
||||
* using Java Native Interface (JNI). Each implemented class (named Lib[number]),
|
||||
* loads a shared library which belongs to one mote type. The reason for this
|
||||
* somewhat strange design is that once loaded, a native library cannot be
|
||||
* unloaded in Java (in the current versions available). Therefore if we wish to
|
||||
* load several libraries, the names and associated native functions must have
|
||||
* unique names. And those names are defined via the calling class in JNI. For
|
||||
* example, the corresponding function for a native tick method in class Lib1
|
||||
* will be named Java_se_sics_cooja_corecomm_Lib1_tick. When creating a new mote
|
||||
* type, the main Contiki source file is generated with function names
|
||||
* compatible with the next available corecomm class. This also implies that
|
||||
* even if a mote type is deleted, a new one cannot be created using the same
|
||||
* corecomm class without restarting the JVM and thus the entire simulation.
|
||||
* using Java Native Interface (JNI). Each implemented class (named
|
||||
* Lib[number]), loads a shared library which belongs to one mote type. The
|
||||
* reason for this somewhat strange design is that once loaded, a native library
|
||||
* cannot be unloaded in Java (in the current versions available). Therefore if
|
||||
* we wish to load several libraries, the names and associated native functions
|
||||
* must have unique names. And those names are defined via the calling class in
|
||||
* JNI. For example, the corresponding function for a native tick method in
|
||||
* class Lib1 will be named Java_se_sics_cooja_corecomm_Lib1_tick. When creating
|
||||
* a new mote type, the main Contiki source file is generated with function
|
||||
* names compatible with the next available corecomm class. This also implies
|
||||
* that even if a mote type is deleted, a new one cannot be created using the
|
||||
* same corecomm class without restarting the JVM and thus the entire
|
||||
* simulation.
|
||||
*
|
||||
* Each implemented CoreComm class needs read access to the following core
|
||||
* variables:
|
||||
|
@ -71,7 +72,9 @@ public abstract class CoreComm {
|
|||
|
||||
// Static pointers to current libraries
|
||||
private final static Vector<CoreComm> coreComms = new Vector<CoreComm>();
|
||||
|
||||
private final static Vector<File> coreCommFiles = new Vector<File>();
|
||||
|
||||
private static int fileCounter = 1;
|
||||
|
||||
/**
|
||||
|
@ -97,7 +100,8 @@ public abstract class CoreComm {
|
|||
*/
|
||||
public static boolean hasLibraryFileBeenLoaded(File libraryFile) {
|
||||
for (File loadedFile : coreCommFiles)
|
||||
if (loadedFile != null && loadedFile.getName().equals(libraryFile.getName()))
|
||||
if (loadedFile != null
|
||||
&& loadedFile.getName().equals(libraryFile.getName()))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
@ -117,10 +121,12 @@ public abstract class CoreComm {
|
|||
* the class name field.
|
||||
*
|
||||
* @param className
|
||||
* Wanted class name
|
||||
* @return True if success, false otherwise
|
||||
* Java class name (without extension)
|
||||
* @throws MoteTypeCreationException
|
||||
* If error occurs
|
||||
*/
|
||||
public static boolean generateLibSourceFile(String className) {
|
||||
public static void generateLibSourceFile(String className)
|
||||
throws MoteTypeCreationException {
|
||||
BufferedWriter sourceFileWriter = null;
|
||||
BufferedReader templateFileReader = null;
|
||||
String destFilename = null;
|
||||
|
@ -167,24 +173,28 @@ public abstract class CoreComm {
|
|||
if (templateFileReader != null)
|
||||
templateFileReader.close();
|
||||
} catch (Exception e2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
throw (MoteTypeCreationException) new MoteTypeCreationException(
|
||||
"Could not generate corecomm source file: " + className + ".java")
|
||||
.initCause(e);
|
||||
}
|
||||
|
||||
File genFile = new File("se/sics/cooja/corecomm/" + destFilename);
|
||||
if (genFile.exists())
|
||||
return true;
|
||||
return;
|
||||
|
||||
return false;
|
||||
throw (MoteTypeCreationException) new MoteTypeCreationException(
|
||||
"Could not generate corecomm source file: " + className + ".java");
|
||||
}
|
||||
|
||||
/**
|
||||
* Compiles Java class.
|
||||
*
|
||||
* @param className Java class name (without extension)
|
||||
* @throws MoteTypeCreationException If Java class compilation error occurrs
|
||||
* @param className
|
||||
* Java class name (without extension)
|
||||
* @throws MoteTypeCreationException
|
||||
* If Java class compilation error occurs
|
||||
*/
|
||||
private static void compileSourceFile(String className)
|
||||
throws MoteTypeCreationException {
|
||||
|
@ -260,23 +270,36 @@ public abstract class CoreComm {
|
|||
}
|
||||
|
||||
/**
|
||||
* Load given Java class file, making it ready to be used.
|
||||
* Loads given Java class file from disk.
|
||||
*
|
||||
* @param classFile Class file
|
||||
* @return Loaded class, or null
|
||||
* @param classFile
|
||||
* Java class (without extension)
|
||||
* @return Loaded class
|
||||
* @throws MoteTypeCreationException
|
||||
* If error occurs
|
||||
*/
|
||||
private static Class loadClassFile(String className) {
|
||||
private static Class loadClassFile(String className)
|
||||
throws MoteTypeCreationException {
|
||||
Class loadedClass = null;
|
||||
try {
|
||||
ClassLoader urlClassLoader = new URLClassLoader(
|
||||
new URL[] { new File(".").toURL() },
|
||||
CoreComm.class.getClassLoader());
|
||||
loadedClass = urlClassLoader.loadClass("se.sics.cooja.corecomm." + className);
|
||||
ClassLoader urlClassLoader = new URLClassLoader(new URL[] { new File(".")
|
||||
.toURL() }, CoreComm.class.getClassLoader());
|
||||
loadedClass = urlClassLoader.loadClass("se.sics.cooja.corecomm."
|
||||
+ className);
|
||||
|
||||
} catch (MalformedURLException e) {
|
||||
return null;
|
||||
throw (MoteTypeCreationException) new MoteTypeCreationException(
|
||||
"Could not load corecomm class file: " + className + ".class")
|
||||
.initCause(e);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
throw (MoteTypeCreationException) new MoteTypeCreationException(
|
||||
"Could not load corecomm class file: " + className + ".class")
|
||||
.initCause(e);
|
||||
}
|
||||
if (loadedClass == null)
|
||||
throw (MoteTypeCreationException) new MoteTypeCreationException(
|
||||
"Could not load corecomm class file: " + className + ".class");
|
||||
|
||||
return loadedClass;
|
||||
}
|
||||
|
||||
|
@ -292,18 +315,17 @@ public abstract class CoreComm {
|
|||
*/
|
||||
public static CoreComm createCoreComm(String className, File libFile)
|
||||
throws MoteTypeCreationException {
|
||||
if (!generateLibSourceFile(className))
|
||||
throw new MoteTypeCreationException("Could not generate corecomm source file: " + className + ".java");
|
||||
generateLibSourceFile(className);
|
||||
|
||||
compileSourceFile(className);
|
||||
|
||||
Class newCoreCommClass = loadClassFile(className);
|
||||
if (newCoreCommClass == null)
|
||||
throw new MoteTypeCreationException("Could not load corecomm class file: " + className + ".class");
|
||||
|
||||
try {
|
||||
Constructor constr = newCoreCommClass.getConstructor(new Class[] { File.class });
|
||||
CoreComm newCoreComm = (CoreComm) constr.newInstance(new Object[] { libFile });
|
||||
Constructor constr = newCoreCommClass
|
||||
.getConstructor(new Class[] { File.class });
|
||||
CoreComm newCoreComm = (CoreComm) constr
|
||||
.newInstance(new Object[] { libFile });
|
||||
|
||||
coreComms.add(newCoreComm);
|
||||
coreCommFiles.add(libFile);
|
||||
|
@ -312,7 +334,7 @@ public abstract class CoreComm {
|
|||
return newCoreComm;
|
||||
} catch (Exception e) {
|
||||
throw (MoteTypeCreationException) new MoteTypeCreationException(
|
||||
"Error when creating library instance: " + className).initCause(e);
|
||||
"Error when creating corecomm instance: " + className).initCause(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue