Skip to content

Instantly share code, notes, and snippets.

@nschlimm
Created September 9, 2011 13:54
Show Gist options
  • Save nschlimm/1206265 to your computer and use it in GitHub Desktop.
Save nschlimm/1206265 to your computer and use it in GitHub Desktop.
ConcurrentTransformer
protected class ConcurrentTransformer {
private final Lock lock = new ReentrantLock();
private URL url = null;
private volatile Templates template = null;
private long lastCompileTime = Long.MAX_VALUE;
protected Templates generateTemplate(long ladezeitProzessApp) throws TransformerFactoryConfigurationError, TransformerConfigurationException, InterruptedException {
if (!mustCompile(loadTimeApplication)) {
return template;
}
lock.lockInterruptibly();
try {
if (!mustCompile(loadTimeApplication)) {
return template;
}
bouncer.acquire();
try {
TransformerFactory transFactory = TransformerFactory.newInstance();
URIResolver resolver = transFactory.getURIResolver();
transFactory.setURIResolver(new XMLTransformResolver(resolver));
StreamSource source = new StreamSource(url.toExternalForm());
template = transFactory.newTemplates(source);
lastCompileTime = System.currentTimeMillis();
return template;
} finally {
bouncer.release();
}
} finally {
lock.unlock();
}
}
private boolean mustCompile(long loadTimeApplication) {
return template == null || !templateCacheActive || requiresRecompile(loadTimeApplication);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment