Skip to content

Commit

Permalink
Make lazy loading more flexible.
Browse files Browse the repository at this point in the history
  • Loading branch information
olonho committed Nov 23, 2020
1 parent 593ec4d commit 6652e62
Showing 1 changed file with 46 additions and 23 deletions.
69 changes: 46 additions & 23 deletions shared/src/main/java/org/jetbrains/skija/impl/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,35 @@
import org.jetbrains.annotations.*;

public class Library {
@ApiStatus.Internal
public static volatile boolean _loaded = false;
private static boolean _loaded = false;
private static boolean _loading = false;

public static synchronized void staticLoad() {
if (_loaded || _loading)
return;

public static void staticLoad() {
if (!_loaded && !"false".equals(System.getProperty("skija.staticLoad")))
_loading = true;
try {
Class clazz = Class.forName("org.jetbrains.skija.CustomLoader");
Runnable customLoader = (Runnable)clazz.newInstance();
customLoader.run();
_loaded = true;
return;
} catch (ClassNotFoundException cnfe) {
} catch (Throwable t) {
t.printStackTrace();
} finally {
_loading = false;
}

if (!_loaded)
load();
}

public static synchronized void load() {
if (_loaded) return;

_loading = true;
File tempDir = new File(System.getProperty("java.io.tmpdir"), "skija_" + System.nanoTime());
String os = System.getProperty("os.name").toLowerCase();

Expand All @@ -35,32 +53,37 @@ public static synchronized void load() {
throw new RuntimeException("Unknown operation system");

_loaded = true;
_loading = false;
_nAfterLoad();
}

// https://github.com/adamheinrich/native-utils/blob/e6a39489662846a77504634b6fafa4995ede3b1d/src/main/java/cz/adamh/utils/NativeUtils.java
@ApiStatus.Internal
@SneakyThrows
public static File _extract(String resourcePath, String fileName, File tempDir) {
File file;
URL url = Library.class.getResource(resourcePath + fileName);
if (url == null)
throw new IllegalArgumentException("Library file " + fileName + " not found in " + resourcePath);
else if (url.getProtocol() == "file") {
// System.out.println("Loading " + url);
file = new File(url.toURI());
} else
try (InputStream is = url.openStream()) {
if (!tempDir.exists()) {
tempDir.mkdirs();
tempDir.deleteOnExit();
// @SneakyThrows leak Lombok to the runtime.
try {
File file;
URL url = Library.class.getResource(resourcePath + fileName);
if (url == null)
throw new IllegalArgumentException("Library file " + fileName + " not found in " + resourcePath);
else if (url.getProtocol() == "file") {
// System.out.println("Loading " + url);
file = new File(url.toURI());
} else
try (InputStream is = url.openStream()) {
if (!tempDir.exists()) {
tempDir.mkdirs();
tempDir.deleteOnExit();
}
file = new File(tempDir, fileName);
file.deleteOnExit();
Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
// System.out.println("Loading " + url + " from " + file);
}
file = new File(tempDir, fileName);
file.deleteOnExit();
Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
// System.out.println("Loading " + url + " from " + file);
}
return file;
return file;
} catch (Throwable t) {
throw new RuntimeException(t);
}
}

@ApiStatus.Internal public static native void _nAfterLoad();
Expand Down

0 comments on commit 6652e62

Please sign in to comment.