diff --git a/src/main/java/org/xerial/snappy/Snappy.java b/src/main/java/org/xerial/snappy/Snappy.java index cca16b5..3c6f409 100755 --- a/src/main/java/org/xerial/snappy/Snappy.java +++ b/src/main/java/org/xerial/snappy/Snappy.java @@ -56,6 +56,19 @@ public class Snappy */ private static SnappyNative impl; + + /** + * Clean up a temporary file (native lib) generated by snappy-java. + * General users do not need to call this method, since the native library extracted in snappy-java + * is deleted upon JVM termination (vie deleteOnExit()). + * This method is useful when using a J2EE container, which will restart servlet containers multiple times without + * restarting JVM. + */ + static void cleanUp() { + SnappyLoader.cleanUpExtractedNativeLib(); + } + + /** * Copy bytes from source to destination * diff --git a/src/main/java/org/xerial/snappy/SnappyLoader.java b/src/main/java/org/xerial/snappy/SnappyLoader.java index 1e234e0..d60f82d 100755 --- a/src/main/java/org/xerial/snappy/SnappyLoader.java +++ b/src/main/java/org/xerial/snappy/SnappyLoader.java @@ -82,7 +82,14 @@ public class SnappyLoader private static volatile boolean isLoaded = false; private static volatile SnappyNative api = null; - + + private static File nativeLibFile = null; + + static void cleanUpExtractedNativeLib() { + if(nativeLibFile != null && nativeLibFile.exists()) + nativeLibFile.delete(); + } + /** * Set the api instance. * @@ -153,10 +160,10 @@ public class SnappyLoader */ private static void loadNativeLibrary() { - File nativeLib = findNativeLibrary(); - if (nativeLib != null) { + nativeLibFile = findNativeLibrary(); + if (nativeLibFile != null) { // Load extracted or specified snappyjava native library. - System.load(nativeLib.getAbsolutePath()); + System.load(nativeLibFile.getAbsolutePath()); } else { // Load preinstalled snappyjava (in the path -Djava.library.path)