Created
September 9, 2011 13:54
-
-
Save nschlimm/1206265 to your computer and use it in GitHub Desktop.
ConcurrentTransformer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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