diff --git a/spring-context/src/main/java/org/springframework/scripting/support/ResourceScriptSource.java b/spring-context/src/main/java/org/springframework/scripting/support/ResourceScriptSource.java index c0de42493f4..9934edc0ea5 100644 --- a/spring-context/src/main/java/org/springframework/scripting/support/ResourceScriptSource.java +++ b/spring-context/src/main/java/org/springframework/scripting/support/ResourceScriptSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ import java.io.Reader; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.springframework.core.io.Resource; import org.springframework.scripting.ScriptSource; import org.springframework.util.Assert; @@ -51,11 +52,12 @@ public class ResourceScriptSource implements ScriptSource { private final Resource resource; + private String encoding = "UTF-8"; + private long lastModified = -1; private final Object lastModifiedMonitor = new Object(); - private String encoding = "UTF-8"; /** * Create a new ResourceScriptSource for the given resource. @@ -74,15 +76,23 @@ public class ResourceScriptSource implements ScriptSource { return this.resource; } + /** + * Set the encoding used for reading the script resource. + *

The default value for regular Resources is "UTF-8". + * A {@code null} value implies the platform default. + */ + public void setEncoding(String encoding) { + this.encoding = encoding; + } + + public String getScriptAsString() throws IOException { synchronized (this.lastModifiedMonitor) { this.lastModified = retrieveLastModifiedTime(); } - InputStream stream = this.resource.getInputStream(); - Reader reader = (StringUtils.hasText(encoding) ? new InputStreamReader(stream, encoding) - : new InputStreamReader(stream)); - + Reader reader = (StringUtils.hasText(this.encoding) ? new InputStreamReader(stream, this.encoding) : + new InputStreamReader(stream)); return FileCopyUtils.copyToString(reader); } @@ -99,10 +109,11 @@ public class ResourceScriptSource implements ScriptSource { protected long retrieveLastModifiedTime() { try { return getResource().lastModified(); - } catch (IOException ex) { + } + 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); + logger.debug(getResource() + " could not be resolved in the file system - " + + "current timestamp not available for script modification check", ex); } return 0; } @@ -112,18 +123,9 @@ public class ResourceScriptSource implements ScriptSource { return StringUtils.stripFilenameExtension(getResource().getFilename()); } - /** - * Sets the encoding used for reading the script resource. The default value is "UTF-8". - * A null value, implies the platform default. - * - * @param encoding charset encoding used for reading the script. - */ - public void setEncoding(String encoding) { - this.encoding = encoding; - } - @Override public String toString() { return this.resource.toString(); } + }