private long getLastModified(String scriptName) throws ResourceException { URLConnection conn = rc.getResourceConnection(scriptName); long lastMod = 0; try { lastMod = conn.getLastModified(); } finally { // getResourceConnection() opening the inputstream, let's ensure all streams are closed forceClose(conn); } return lastMod; }
/** * Get the class of the scriptName in question, so that you can instantiate * Groovy objects with caching and reloading. * * @param scriptName resource name pointing to the script * @return the loaded scriptName as a compiled class * @throws ResourceException if there is a problem accessing the script * @throws ScriptException if there is a problem parsing the script */ public Class loadScriptByName(String scriptName) throws ResourceException, ScriptException { URLConnection conn = rc.getResourceConnection(scriptName); String path = conn.getURL().toExternalForm(); ScriptCacheEntry entry = scriptCache.get(path); Class clazz = null; if (entry != null) clazz = entry.scriptClass; try { if (isSourceNewer(entry)) { try { String encoding = conn.getContentEncoding() != null ? conn.getContentEncoding() : config.getSourceEncoding(); String content = IOGroovyMethods.getText(conn.getInputStream(), encoding); clazz = groovyLoader.parseClass(content, path); } catch (IOException e) { throw new ResourceException(e); } } } finally { forceClose(conn); } return clazz; }
private long getLastModified(String scriptName) throws ResourceException { URLConnection conn = rc.getResourceConnection(scriptName); long lastMod = 0; try { // getLastModified() truncates up to 999 ms from the true modification time, let's fix that lastMod = ((conn.getLastModified() / 1000) + 1) * 1000 - 1; } finally { // getResourceConnection() opening the inputstream, let's ensure all streams are closed forceClose(conn); } return lastMod; }
private boolean dependencyOutOfDate(ScriptCacheEntry cacheEntry) { if (cacheEntry != null) { for (URL url : cacheEntry.dependencies.keySet()) { URLConnection urlc = null; try { urlc = url.openConnection(); urlc.setDoInput(false); urlc.setDoOutput(false); long dependentLastModified = urlc.getLastModified(); if (dependentLastModified > cacheEntry.dependencies.get(url)) { return true; } } catch (IOException ioe) { return true; } finally { forceClose(urlc); } } } return false; }
private Class parseScript(String scriptName, URLConnection scriptConn) throws ScriptException { try { InputStream in = scriptConn.getInputStream(); return groovyLoader.parseClass(in, scriptName); } catch (Exception e) { throw new ScriptException("Could not parse script: " + scriptName, e); } finally { currentCacheEntryHolder.set(null); forceClose(scriptConn); } }
cacheEntry = null; } else { forceClose(scriptConn);
/** * Get the class of the scriptName in question, so that you can instantiate * Groovy objects with caching and reloading. * * @param scriptName resource name pointing to the script * @return the loaded scriptName as a compiled class * @throws ResourceException if there is a problem accessing the script * @throws ScriptException if there is a problem parsing the script */ public Class loadScriptByName(String scriptName) throws ResourceException, ScriptException { URLConnection conn = rc.getResourceConnection(scriptName); String path = conn.getURL().toExternalForm(); ScriptCacheEntry entry = scriptCache.get(path); Class clazz = null; if (entry != null) clazz = entry.scriptClass; try { if (isSourceNewer(entry)) { try { String encoding = conn.getContentEncoding() != null ? conn.getContentEncoding() : "UTF-8"; String content = DefaultGroovyMethods.getText(conn.getInputStream(), encoding); clazz = groovyLoader.parseClass(content, path); } catch (IOException e) { throw new ResourceException(e); } } } finally { forceClose(conn); } return clazz; }