1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2013 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
23
23
24
24
import org .apache .commons .logging .Log ;
25
25
import org .apache .commons .logging .LogFactory ;
26
+
26
27
import org .springframework .core .io .Resource ;
27
28
import org .springframework .scripting .ScriptSource ;
28
29
import org .springframework .util .Assert ;
@@ -51,11 +52,12 @@ public class ResourceScriptSource implements ScriptSource {
51
52
52
53
private final Resource resource ;
53
54
55
+ private String encoding = "UTF-8" ;
56
+
54
57
private long lastModified = -1 ;
55
58
56
59
private final Object lastModifiedMonitor = new Object ();
57
60
58
- private String encoding = "UTF-8" ;
59
61
60
62
/**
61
63
* Create a new ResourceScriptSource for the given resource.
@@ -74,15 +76,23 @@ public final Resource getResource() {
74
76
return this .resource ;
75
77
}
76
78
79
+ /**
80
+ * Set the encoding used for reading the script resource.
81
+ * <p>The default value for regular Resources is "UTF-8".
82
+ * A {@code null} value implies the platform default.
83
+ */
84
+ public void setEncoding (String encoding ) {
85
+ this .encoding = encoding ;
86
+ }
87
+
88
+
77
89
public String getScriptAsString () throws IOException {
78
90
synchronized (this .lastModifiedMonitor ) {
79
91
this .lastModified = retrieveLastModifiedTime ();
80
92
}
81
-
82
93
InputStream stream = this .resource .getInputStream ();
83
- Reader reader = (StringUtils .hasText (encoding ) ? new InputStreamReader (stream , encoding )
84
- : new InputStreamReader (stream ));
85
-
94
+ Reader reader = (StringUtils .hasText (this .encoding ) ? new InputStreamReader (stream , this .encoding ) :
95
+ new InputStreamReader (stream ));
86
96
return FileCopyUtils .copyToString (reader );
87
97
}
88
98
@@ -99,10 +109,11 @@ public boolean isModified() {
99
109
protected long retrieveLastModifiedTime () {
100
110
try {
101
111
return getResource ().lastModified ();
102
- } catch (IOException ex ) {
112
+ }
113
+ catch (IOException ex ) {
103
114
if (logger .isDebugEnabled ()) {
104
- logger .debug (getResource () + " could not be resolved in the file system - "
105
- + "current timestamp not available for script modification check" , ex );
115
+ logger .debug (getResource () + " could not be resolved in the file system - " +
116
+ "current timestamp not available for script modification check" , ex );
106
117
}
107
118
return 0 ;
108
119
}
@@ -112,18 +123,9 @@ public String suggestedClassName() {
112
123
return StringUtils .stripFilenameExtension (getResource ().getFilename ());
113
124
}
114
125
115
- /**
116
- * Sets the encoding used for reading the script resource. The default value is "UTF-8".
117
- * A null value, implies the platform default.
118
- *
119
- * @param encoding charset encoding used for reading the script.
120
- */
121
- public void setEncoding (String encoding ) {
122
- this .encoding = encoding ;
123
- }
124
-
125
126
@ Override
126
127
public String toString () {
127
128
return this .resource .toString ();
128
129
}
130
+
129
131
}
0 commit comments