Fixed source location of setEncoding method

This commit is contained in:
Juergen Hoeller
2013-10-27 00:57:37 +02:00
parent ff9c695599
commit 3eaec64ee8

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.scripting.ScriptSource; import org.springframework.scripting.ScriptSource;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@@ -51,11 +52,12 @@ public class ResourceScriptSource implements ScriptSource {
private final Resource resource; private final Resource resource;
private String encoding = "UTF-8";
private long lastModified = -1; private long lastModified = -1;
private final Object lastModifiedMonitor = new Object(); private final Object lastModifiedMonitor = new Object();
private String encoding = "UTF-8";
/** /**
* Create a new ResourceScriptSource for the given resource. * Create a new ResourceScriptSource for the given resource.
@@ -74,15 +76,23 @@ public class ResourceScriptSource implements ScriptSource {
return this.resource; return this.resource;
} }
/**
* Set the encoding used for reading the script resource.
* <p>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 { public String getScriptAsString() throws IOException {
synchronized (this.lastModifiedMonitor) { synchronized (this.lastModifiedMonitor) {
this.lastModified = retrieveLastModifiedTime(); this.lastModified = retrieveLastModifiedTime();
} }
InputStream stream = this.resource.getInputStream(); InputStream stream = this.resource.getInputStream();
Reader reader = (StringUtils.hasText(encoding) ? new InputStreamReader(stream, encoding) Reader reader = (StringUtils.hasText(this.encoding) ? new InputStreamReader(stream, this.encoding) :
: new InputStreamReader(stream)); new InputStreamReader(stream));
return FileCopyUtils.copyToString(reader); return FileCopyUtils.copyToString(reader);
} }
@@ -99,10 +109,11 @@ public class ResourceScriptSource implements ScriptSource {
protected long retrieveLastModifiedTime() { protected long retrieveLastModifiedTime() {
try { try {
return getResource().lastModified(); return getResource().lastModified();
} catch (IOException ex) { }
catch (IOException ex) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(getResource() + " could not be resolved in the file system - " logger.debug(getResource() + " could not be resolved in the file system - " +
+ "current timestamp not available for script modification check", ex); "current timestamp not available for script modification check", ex);
} }
return 0; return 0;
} }
@@ -112,18 +123,9 @@ public class ResourceScriptSource implements ScriptSource {
return StringUtils.stripFilenameExtension(getResource().getFilename()); 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 @Override
public String toString() { public String toString() {
return this.resource.toString(); return this.resource.toString();
} }
} }