Lazy-load script bundle

This commit is contained in:
Reinhard Pointner 2016-04-09 23:20:13 +00:00
parent 612a243518
commit f7ac0a79d7
3 changed files with 14 additions and 13 deletions

View File

@ -31,13 +31,11 @@ class MemoizedResource<R> implements Resource<R> {
}
@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;
}
}

View File

@ -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<byte[]> bundle;
private Certificate certificate;
public ScriptBundle(byte[] bytes, InputStream certificate) throws CertificateException {
this.bytes = bytes;
public ScriptBundle(Resource<byte[]> 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;

View File

@ -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<byte[]> 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"));
}
},