From f7ac0a79d720e61794a70dd9142143570f9aee65 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 9 Apr 2016 23:20:13 +0000 Subject: [PATCH] Lazy-load script bundle --- source/net/filebot/Resource.java | 10 ++++------ source/net/filebot/cli/ScriptBundle.java | 10 ++++++---- source/net/filebot/cli/ScriptSource.java | 7 ++++--- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/source/net/filebot/Resource.java b/source/net/filebot/Resource.java index c3d81ffd..49690a8e 100644 --- a/source/net/filebot/Resource.java +++ b/source/net/filebot/Resource.java @@ -31,13 +31,11 @@ class MemoizedResource implements Resource { } @Override - public R get() throws Exception { - synchronized (resource) { - if (value == null) { - value = resource.get(); - } - return value; + public synchronized R get() throws Exception { + if (value == null) { + value = resource.get(); } + return value; } } diff --git a/source/net/filebot/cli/ScriptBundle.java b/source/net/filebot/cli/ScriptBundle.java index 8157aed2..e203f456 100644 --- a/source/net/filebot/cli/ScriptBundle.java +++ b/source/net/filebot/cli/ScriptBundle.java @@ -15,19 +15,21 @@ import java.util.jar.JarInputStream; import com.google.common.io.ByteStreams; +import net.filebot.Resource; + public class ScriptBundle implements ScriptProvider { - private byte[] bytes; + private Resource bundle; private Certificate certificate; - public ScriptBundle(byte[] bytes, InputStream certificate) throws CertificateException { - this.bytes = bytes; + public ScriptBundle(Resource bundle, InputStream certificate) throws CertificateException { + this.bundle = bundle.memoize(); this.certificate = CertificateFactory.getInstance("X.509").generateCertificate(certificate); } @Override public String getScript(String name) throws Exception { - try (JarInputStream jar = new JarInputStream(new ByteArrayInputStream(bytes), true)) { + try (JarInputStream jar = new JarInputStream(new ByteArrayInputStream(bundle.get()), true)) { for (JarEntry f = jar.getNextJarEntry(); f != null; f = jar.getNextJarEntry()) { if (f.isDirectory() || !f.getName().startsWith(name) || !f.getName().substring(name.length()).equals(".groovy")) continue; diff --git a/source/net/filebot/cli/ScriptSource.java b/source/net/filebot/cli/ScriptSource.java index 50a0e010..9d0c9f5d 100644 --- a/source/net/filebot/cli/ScriptSource.java +++ b/source/net/filebot/cli/ScriptSource.java @@ -10,6 +10,7 @@ import java.time.Duration; import net.filebot.Cache; import net.filebot.CacheType; +import net.filebot.Resource; public enum ScriptSource { @@ -22,10 +23,10 @@ public enum ScriptSource { @Override public ScriptProvider getScriptProvider(String input) throws Exception { - URI bundle = new URI(getApplicationProperty("github.stable")); - byte[] bytes = getCache().bytes(bundle, URI::toURL).expire(Cache.ONE_WEEK).get(); + URI resource = new URI(getApplicationProperty("github.stable")); + Resource bundle = getCache().bytes(resource, URI::toURL).expire(Cache.ONE_WEEK); - return new ScriptBundle(bytes, getClass().getResourceAsStream("repository.cer")); + return new ScriptBundle(bundle, getClass().getResourceAsStream("repository.cer")); } },