/** * @param scriptLocation The location of the script */ public void setLocation(Resource scriptLocation) { this.scriptSource = new ResourceScriptSource(scriptLocation); }
/** * Retrieve the current last-modified timestamp of the underlying resource. * @return the current timestamp, or 0 if not determinable */ protected long retrieveLastModifiedTime() { try { return getResource().lastModified(); } catch (IOException ex) { if (logger.isDebugEnabled()) { logger.debug(getResource() + " could not be resolved in the file system - " + "current timestamp not available for script modification check", ex); } return 0; } }
@Test public void doesNotPropagateFatalExceptionOnResourceThatCannotBeResolvedToAFile() throws Exception { Resource resource = mock(Resource.class); given(resource.lastModified()).willThrow(new IOException()); ResourceScriptSource scriptSource = new ResourceScriptSource(resource); long lastModified = scriptSource.retrieveLastModifiedTime(); assertEquals(0, lastModified); }
@Test public void lastModifiedWorksWithResourceThatDoesNotSupportFileBasedAccessAtAll() throws Exception { Resource resource = new ByteArrayResource(new byte[0]); ResourceScriptSource scriptSource = new ResourceScriptSource(resource); assertTrue("ResourceScriptSource must start off in the 'isModified' state (it obviously isn't).", scriptSource.isModified()); scriptSource.getScriptAsString(); assertFalse("ResourceScriptSource must not report back as being modified if the underlying File resource is not reporting a changed lastModified time.", scriptSource.isModified()); // Must now continue to report back as not having been modified 'cos the Resource does not support access as a File (and so the lastModified date cannot be determined). assertFalse("ResourceScriptSource must not report back as being modified if the underlying File resource is not reporting a changed lastModified time.", scriptSource.isModified()); }
@Test public void beginsInModifiedState() throws Exception { Resource resource = mock(Resource.class); ResourceScriptSource scriptSource = new ResourceScriptSource(resource); assertTrue(scriptSource.isModified()); }
public RefreshableResourceScriptSource(Resource resource, long refreshDelay) { this.refreshDelay = refreshDelay; this.source = new ResourceScriptSource(resource); try { this.script = this.source.getScriptAsString(); } catch (IOException e) { this.lastModifiedChecked.set(0); } }
@Override public boolean isModified() { synchronized (this.lastModifiedMonitor) { return (this.lastModified < 0 || retrieveLastModifiedTime() > this.lastModified); } }
public boolean isModified() { return this.refreshDelay >= 0 && (System.currentTimeMillis() - this.lastModifiedChecked.get()) > this.refreshDelay && this.source.isModified(); }
public String getScriptAsString() throws IOException { if (this.script == null || this.isModified()) { this.lastModifiedChecked.set(System.currentTimeMillis()); this.script = this.source.getScriptAsString(); } return this.script; }
@Test public void lastModifiedWorksWithResourceThatDoesNotSupportFileBasedReading() throws Exception { Resource resource = mock(Resource.class); // underlying File is asked for so that the last modified time can be checked... // And then mock the file changing; i.e. the File says it has been modified given(resource.lastModified()).willReturn(100L, 100L, 200L); // does not support File-based reading; delegates to InputStream-style reading... //resource.getFile(); //mock.setThrowable(new FileNotFoundException()); given(resource.getInputStream()).willReturn(StreamUtils.emptyInput()); ResourceScriptSource scriptSource = new ResourceScriptSource(resource); assertTrue("ResourceScriptSource must start off in the 'isModified' state (it obviously isn't).", scriptSource.isModified()); scriptSource.getScriptAsString(); assertFalse("ResourceScriptSource must not report back as being modified if the underlying File resource is not reporting a changed lastModified time.", scriptSource.isModified()); // Must now report back as having been modified assertTrue("ResourceScriptSource must report back as being modified if the underlying File resource is reporting a changed lastModified time.", scriptSource.isModified()); }
public RefreshableResourceScriptSource(Resource resource, long refreshDelay) { this.refreshDelay = refreshDelay; this.source = new ResourceScriptSource(resource); try { this.script = this.source.getScriptAsString(); } catch (IOException e) { this.lastModifiedChecked.set(0); } }
@Override public boolean isModified() { synchronized (this.lastModifiedMonitor) { return (this.lastModified < 0 || retrieveLastModifiedTime() > this.lastModified); } }
public boolean isModified() { return this.refreshDelay >= 0 && (System.currentTimeMillis() - this.lastModifiedChecked.get()) > this.refreshDelay && this.source.isModified(); }
public String getScriptAsString() throws IOException { if (this.script == null || this.isModified()) { this.lastModifiedChecked.set(System.currentTimeMillis()); this.script = this.source.getScriptAsString(); } return this.script; }
/** * <p> * Sets the {@link org.springframework.core.io.Resource} location of the script to use. * The script language will be deduced from the filename extension. * </p> * * @param resource the {@link org.springframework.core.io.Resource} location of the script to use. */ public void setScript(Resource resource) { Assert.notNull(resource, "The script resource cannot be null"); this.script = new ResourceScriptSource(resource); }
@Override @Nullable public String suggestedClassName() { String filename = getResource().getFilename(); return (filename != null ? StringUtils.stripFilenameExtension(filename) : null); }
@Override public String getScriptAsString() throws IOException { synchronized (this.lastModifiedMonitor) { this.lastModified = retrieveLastModifiedTime(); } Reader reader = this.resource.getReader(); return FileCopyUtils.copyToString(reader); }
/** * Convert the given script source locator to a ScriptSource instance. * <p>By default, supported locators are Spring resource locations * (such as "file:C:/myScript.bsh" or "classpath:myPackage/myScript.bsh") * and inline scripts ("inline:myScriptText..."). * @param beanName the name of the scripted bean * @param scriptSourceLocator the script source locator * @param resourceLoader the ResourceLoader to use (if necessary) * @return the ScriptSource instance */ protected ScriptSource convertToScriptSource(String beanName, String scriptSourceLocator, ResourceLoader resourceLoader) { if (scriptSourceLocator.startsWith(INLINE_SCRIPT_PREFIX)) { return new StaticScriptSource(scriptSourceLocator.substring(INLINE_SCRIPT_PREFIX.length()), beanName); } else { return new ResourceScriptSource(resourceLoader.getResource(scriptSourceLocator)); } }
@Override @Nullable public String suggestedClassName() { String filename = getResource().getFilename(); return (filename != null ? StringUtils.stripFilenameExtension(filename) : null); }
@Override public String getScriptAsString() throws IOException { synchronized (this.lastModifiedMonitor) { this.lastModified = retrieveLastModifiedTime(); } Reader reader = this.resource.getReader(); return FileCopyUtils.copyToString(reader); }