Skip to content

Commit

Permalink
Merge pull request #30 from Electrostat-Lab/1.0.0-stable-branch
Browse files Browse the repository at this point in the history
1.0.0 stable branch
  • Loading branch information
pavly-gerges authored Aug 8, 2024
2 parents bb00290 + 9da8878 commit f7cb347
Show file tree
Hide file tree
Showing 18 changed files with 448 additions and 253 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@

package electrostatic4j.snaploader.examples;

import java.io.IOException;

import electrostatic4j.snaploader.LibraryInfo;
import electrostatic4j.snaploader.NativeBinaryLoader;
import electrostatic4j.snaploader.filesystem.DirectoryPath;
import electrostatic4j.snaploader.platform.util.DefaultDynamicLibraries;
import electrostatic4j.snaploader.platform.NativeDynamicLibrary;
import electrostatic4j.snaploader.platform.util.NativeVariant;
Expand All @@ -51,7 +50,7 @@
public final class TestBasicFeatures {

protected static final LibraryInfo libraryInfo = new LibraryInfo(getJarFilePath(),
"lib/placeholder",
new DirectoryPath(DefaultDynamicLibraries.LINUX_X86.getPlatformDirectory()),
getLibraryBaseName(),
getLibrariesAbsolutePath());

Expand Down Expand Up @@ -92,18 +91,16 @@ protected static void printDetails(NativeBinaryLoader loader) {
System.out.println("--------------------------------------------------------------");
}

protected static String getLibrariesAbsolutePath() {
return PropertiesProvider.USER_DIR.getSystemProperty() +
PropertiesProvider.FILE_SEPARATOR.getSystemProperty() + "libs";
protected static DirectoryPath getLibrariesAbsolutePath() {
return new DirectoryPath(PropertiesProvider.USER_DIR.getSystemProperty(), "libs");
}

protected static String getJarFilePath() {
return getLibrariesAbsolutePath() +
PropertiesProvider.FILE_SEPARATOR.getSystemProperty() + getJarFile();
protected static DirectoryPath getJarFilePath() {
return new DirectoryPath(getLibrariesAbsolutePath().getPath(), getJarFile());
}

protected static String getNativeDynamicLibraryPath() {
return getLibrariesAbsolutePath() +
return getLibrariesAbsolutePath().getPath() +
PropertiesProvider.FILE_SEPARATOR.getSystemProperty() +
"lib" + getLibraryBaseName() + ".so";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@

package electrostatic4j.snaploader.examples;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import electrostatic4j.snaploader.LibraryInfo;
import electrostatic4j.snaploader.NativeBinaryLoader;
import electrostatic4j.snaploader.filesystem.DirectoryPath;
import electrostatic4j.snaploader.platform.util.DefaultDynamicLibraries;
import electrostatic4j.snaploader.platform.NativeDynamicLibrary;
import electrostatic4j.snaploader.platform.util.NativeVariant;
Expand All @@ -59,8 +59,8 @@ public static void main(String[] args) throws Exception {
final Path extractionPath = Files.createDirectories(Paths.get(PropertiesProvider.USER_DIR.getSystemProperty(), "libs",
NativeVariant.OS_NAME.getProperty(), NativeVariant.OS_ARCH.getProperty()));

final LibraryInfo libraryInfo = new LibraryInfo(compressionPath.toString(), "lib/placeholder",
"jmealloc", extractionPath.toString());
final LibraryInfo libraryInfo = new LibraryInfo(new DirectoryPath(compressionPath.toString()), new DirectoryPath("lib/placeholder"),
"jmealloc", new DirectoryPath(extractionPath.toString()));

final NativeDynamicLibrary[] libraries = new NativeDynamicLibrary[] {
DefaultDynamicLibraries.LINUX_X86,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import electrostatic4j.snaploader.LibraryInfo;
import electrostatic4j.snaploader.LoadingCriterion;
import electrostatic4j.snaploader.NativeBinaryLoader;
import electrostatic4j.snaploader.filesystem.DirectoryPath;
import electrostatic4j.snaploader.platform.NativeDynamicLibrary;
import electrostatic4j.snaploader.platform.util.DefaultDynamicLibraries;
import electrostatic4j.snaploader.platform.util.NativeVariant;
Expand All @@ -20,8 +21,8 @@ public static void main(String[] args) throws Exception {
final Path extractionPath = Files.createDirectories(Paths.get(PropertiesProvider.USER_DIR.getSystemProperty(), "libs",
NativeVariant.OS_NAME.getProperty(), NativeVariant.OS_ARCH.getProperty()));

final LibraryInfo libraryInfo = new LibraryInfo(compressionPath.toString(), "lib/placeholder",
"jme3alloc", extractionPath.toString());
final LibraryInfo libraryInfo = new LibraryInfo(new DirectoryPath(compressionPath.toString()), new DirectoryPath("lib/placeholder"),
"jme3alloc", new DirectoryPath(extractionPath.toString()));

final NativeDynamicLibrary[] libraries = new NativeDynamicLibrary[] {
DefaultDynamicLibraries.LINUX_X86,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@
import electrostatic4j.snaploader.filesystem.FileExtractionListener;
import electrostatic4j.snaploader.filesystem.FileExtractor;
import electrostatic4j.snaploader.filesystem.FileLocator;
import electrostatic4j.snaploader.filesystem.ZipCompressionType;
import electrostatic4j.snaploader.platform.util.PropertiesProvider;
import electrostatic4j.snaploader.util.SnapLoaderLogger;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipFile;

/**
* Testing impacts of memory leaks, test this using jconsole.
Expand All @@ -52,7 +50,7 @@ public class TestFilesystemMemoryLeak {
public static void main(String[] args) throws Exception {
/* Locates the image inside the Zip Compression */
SnapLoaderLogger.setLoggingEnabled(true);
final FileLocator fileLocator = new FileLocator(getZipAbsolutePath(), getFilePath(), ZipCompressionType.ZIP);
final FileLocator fileLocator = new FileLocator(new ZipFile(getZipAbsolutePath()), getFilePath());
/* Extracts the image filesystem from the Zip Compression */
final FileExtractor fileExtractor = new FileExtractor(fileLocator, getExtractionPath());
fileLocator.initialize(0);
Expand All @@ -79,12 +77,12 @@ public void onExtractionFinalization(FileExtractor fileExtractor, FileLocator fi
}

protected static String getZipAbsolutePath() {
return TestBasicFeatures.getLibrariesAbsolutePath() +
return TestBasicFeatures.getLibrariesAbsolutePath().getPath() +
PropertiesProvider.FILE_SEPARATOR.getSystemProperty() + "jmelogo700.zip";
}

protected static String getExtractionPath() {
return TestBasicFeatures.getLibrariesAbsolutePath() +
return TestBasicFeatures.getLibrariesAbsolutePath().getPath() +
PropertiesProvider.FILE_SEPARATOR.getSystemProperty() + getFilePath();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@

package electrostatic4j.snaploader.examples;

import java.io.IOException;

import electrostatic4j.snaploader.filesystem.FileExtractionListener;
import electrostatic4j.snaploader.filesystem.FileExtractor;
import electrostatic4j.snaploader.filesystem.FileLocator;
import electrostatic4j.snaploader.filesystem.ZipCompressionType;
import electrostatic4j.snaploader.platform.util.PropertiesProvider;
import electrostatic4j.snaploader.throwable.FilesystemResourceScavengingException;

import java.util.zip.ZipFile;

/**
* Tests extracting an image compression from a Zip compression type filesystem using {@link FileExtractor} API.
*
Expand All @@ -50,7 +49,7 @@ public class TestZipExtractor {

public static void main(String[] args) throws Exception {
/* Locates the image inside the Zip Compression */
final FileLocator fileLocator = new FileLocator(getZipAbsolutePath(), getFilePath(), ZipCompressionType.ZIP);
final FileLocator fileLocator = new FileLocator(new ZipFile(getZipAbsolutePath()), getFilePath());
/* Extracts the image filesystem from the Zip Compression */
final FileExtractor fileExtractor = new FileExtractor(fileLocator, getExtractionPath());
fileLocator.initialize(0);
Expand Down Expand Up @@ -80,12 +79,12 @@ public void onExtractionFinalization(FileExtractor fileExtractor, FileLocator fi
}

protected static String getZipAbsolutePath() {
return TestBasicFeatures.getLibrariesAbsolutePath() +
return TestBasicFeatures.getLibrariesAbsolutePath().getPath() +
PropertiesProvider.FILE_SEPARATOR.getSystemProperty() + "jmelogo700.zip";
}

protected static String getExtractionPath() {
return TestBasicFeatures.getLibrariesAbsolutePath() +
return TestBasicFeatures.getLibrariesAbsolutePath().getPath() +
PropertiesProvider.FILE_SEPARATOR.getSystemProperty() + getFilePath();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

package electrostatic4j.snaploader;

import electrostatic4j.snaploader.filesystem.DirectoryPath;
import electrostatic4j.snaploader.platform.NativeDynamicLibrary;

/**
Expand Down Expand Up @@ -59,41 +60,44 @@
*/
public final class LibraryInfo {

private String jarPath;
private String directory;
private DirectoryPath jarPath;
private DirectoryPath directory;
private String baseName;
private String extractionDir;
private DirectoryPath directoryPath;

/**
* Instantiates a library info data structure pointing to a library in the classpath.
*
* @param directory the platform-independent directory inside the compression used for locating the native dynamic library,
* this is used as a backup directory path
* in case the {@link NativeDynamicLibrary#getPlatformDirectory()} is not valid.
* @param baseName the library basename, for example, 'lib-basename.so'.
* @param extractionDir the extraction destination in absolute string format, "null" if the current [user.dir] is
* specified as the extraction directory
* @param baseName the library basename, for example, 'lib-basename.so' (not null).
* @param directoryPath the extraction destination path, {@link DirectoryPath#USER_DIR} for
* a user working directory extraction path,
* and {@link DirectoryPath#USER_HOME} for the user home (not null).
*/
public LibraryInfo(String directory, String baseName, String extractionDir) {
this(null, directory, baseName, extractionDir);
public LibraryInfo(DirectoryPath directory, String baseName, DirectoryPath directoryPath) {
this(DirectoryPath.CLASS_PATH, directory, baseName, directoryPath);
}

/**
* Instantiates a library info data structure pointing to a library in an external jar with jarPath.
*
* @param jarPath a path to an external jar to locate the library inside, "null" to use the project jar (classpath).
* @param jarPath a path to an external jar to locate the library inside, {@link DirectoryPath#CLASS_PATH} to assign the classpath routine
* to the {@link electrostatic4j.snaploader.filesystem.FileLocator} API (not null).
* @param directory the platform-independent directory inside the compression used for locating the native dynamic library,
* this is used as a backup directory path
* in case the {@link NativeDynamicLibrary#getPlatformDirectory()} is not valid.
* @param baseName the library basename, for example, 'lib-basename.so'.
* @param extractionDir the extraction destination in absolute string format, "null" if the current [user.dir] is
* specified as the extraction directory
* @param baseName the library basename, for example, 'lib-basename.so' (not null).
* @param directoryPath the extraction destination path, {@link DirectoryPath#USER_DIR} for
* a user working directory extraction path,
* and {@link DirectoryPath#USER_HOME} for the user home (not null).
*/
public LibraryInfo(String jarPath, String directory, String baseName, String extractionDir) {
public LibraryInfo(DirectoryPath jarPath, DirectoryPath directory, String baseName, DirectoryPath directoryPath) {
this.jarPath = jarPath;
this.directory = directory;
this.baseName = baseName;
this.extractionDir = extractionDir;
this.directoryPath = directoryPath;
}

/**
Expand All @@ -109,52 +113,48 @@ public String getBaseName() {
* Retrieves the jar filesystem path, the jar is the compression used to locate the native dynamic library to
* be extracted and loaded by {@link NativeBinaryLoader}.
*
* @return the jar absolute filesystem path in a string format, "null" if the classpath is specified instead of
* an external jar compression
* @return the jar absolute filesystem path object.
*/
public String getJarPath() {
public DirectoryPath getJarPath() {
return jarPath;
}

/**
* Retrieves the directory inside the compression used for locating the native dynamic library.
*
* @return the path to the dynamic library filesystem inside the compression, "null" if the
* default variant-based directories are set to be used
* @return the path to the dynamic library filesystem inside the compression.
*/
public String getDirectory() {
public DirectoryPath getDirectory() {
return directory;
}

/**
* Retrieves the extraction absolute directory.
*
* @return the extraction destination in absolute string format, "null" if the current [user.dir] is
* specified as the extraction directory
* @return the extraction destination path object.
*/
public String getExtractionDir() {
return extractionDir;
public DirectoryPath getExtractionDirectory() {
return directoryPath;
}

/**
* Sets the absolute path to the jar filesystem to locate the native dynamic library to be
* extracted and loaded, "null" to use the "classpath (the stock jar)"" to load the library filesystem.
*
* @param jarPath the absolute path to the jar filesystem to locate the library to be extracted, "null" to
* use the "classpath" (aka. the stock jar)
* @param jarPath the external jar path object to localize the compression, use {@link DirectoryPath#CLASS_PATH}
* to switch the file locator to the classpath routine.
*/
public void setJarPath(String jarPath) {
public void setJarPath(DirectoryPath jarPath) {
this.jarPath = jarPath;
}

/**
* Sets the directory to the native dynamic library inside the jar compression, "null" to use
* the default directories specified for each variant by {@link NativeBinaryLoader}.
*
* @param directory the location to the native dynamic library inside the jar compression, "null"
* to use the default variant-based directories
* @param directory the location to the native dynamic library inside the jar compression.
*/
public void setDirectory(String directory) {
public void setDirectory(DirectoryPath directory) {
this.directory = directory;
}

Expand All @@ -168,13 +168,12 @@ public void setBaseName(final String baseName) {
}

/**
* Sets the extraction directory used for extracting the native dynamic library in the
* form of an absolute directory, "null" to use the current [user.dir].
* Sets the extraction directory used for extracting the native dynamic library.
*
* @param extractionDir the absolute extraction directory to which the located library
* will be extracted to, "null" to set the extraction to the current [user.dir]
* @param directoryPath the extraction directory path to which the native-located library
* will be extracted to.
*/
public void setExtractionDir(String extractionDir) {
this.extractionDir = extractionDir;
public void setExtractionDirectory(DirectoryPath directoryPath) {
this.directoryPath = directoryPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.jar.JarFile;
import java.util.logging.Level;
import java.lang.UnsatisfiedLinkError;
import electrostatic4j.snaploader.filesystem.FileExtractionListener;
Expand Down Expand Up @@ -349,8 +350,10 @@ public void onExtractionFinalization(FileExtractor fileExtractor, FileLocator fi
protected FileExtractor initializeLibraryExtractor(NativeDynamicLibrary library) throws Exception {
FileExtractor extractor;
if (library.getJarPath() != null) {
extractor = new LibraryExtractor(library.getJarPath(), library.getCompressedLibrary(), library.getExtractedLibrary());
// use an extractor with the external jar routine
extractor = new LibraryExtractor(new JarFile(library.getJarPath()), library.getCompressedLibrary(), library.getExtractedLibrary());
} else {
// use an extractor with the classpath routine
extractor = new LibraryExtractor(library.getCompressedLibrary(), library.getExtractedLibrary());
}
extractor.initialize(0);
Expand All @@ -363,7 +366,7 @@ protected LibraryLocator preInitLibraryLocator(FileExtractor extractor) {
extractor.getFileLocator().setFileLocalizingListener(new FileLocalizingListener() {
@Override
public void onFileLocalizationSuccess(FileLocator locator) {
SnapLoaderLogger.log(Level.INFO, getClass().getName(), "initializeLibraryExtractor",
SnapLoaderLogger.log(Level.INFO, getClass().getName(), "preInitLibraryLocator",
"Locating native libraries has succeeded!");

// bind the library locator lifecycle to the user application
Expand All @@ -374,7 +377,7 @@ public void onFileLocalizationSuccess(FileLocator locator) {

@Override
public void onFileLocalizationFailure(FileLocator locator, Throwable throwable) {
SnapLoaderLogger.log(Level.SEVERE, getClass().getName(), "initializeLibraryExtractor",
SnapLoaderLogger.log(Level.SEVERE, getClass().getName(), "preInitLibraryLocator",
"Locating native libraries has failed!", throwable);
try {
extractor.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

package electrostatic4j.snaploader.filesystem;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.locks.ReentrantLock;

Expand All @@ -53,9 +52,8 @@ public class ConcurrentFileExtractor extends FileExtractor {
*
* @param fileLocator locates a filesystem inside a zip compression
* @param destination an absolute filesystem path representing the extraction destination filesystem
* @throws FileNotFoundException if the destination filesystem path is not found
*/
public ConcurrentFileExtractor(FileLocator fileLocator, String destination) throws FileNotFoundException {
public ConcurrentFileExtractor(FileLocator fileLocator, String destination) {
super(fileLocator, destination);
}

Expand Down
Loading

0 comments on commit f7cb347

Please sign in to comment.