View Javadoc
1   /*
2    * Copyright 2002-2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.scripting.support;
18  
19  import java.io.IOException;
20  import java.io.Reader;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  import org.springframework.core.io.Resource;
26  import org.springframework.core.io.support.EncodedResource;
27  import org.springframework.scripting.ScriptSource;
28  import org.springframework.util.Assert;
29  import org.springframework.util.FileCopyUtils;
30  import org.springframework.util.StringUtils;
31  
32  /**
33   * {@link org.springframework.scripting.ScriptSource} implementation
34   * based on Spring's {@link org.springframework.core.io.Resource}
35   * abstraction. Loads the script text from the underlying Resource's
36   * {@link org.springframework.core.io.Resource#getFile() File} or
37   * {@link org.springframework.core.io.Resource#getInputStream() InputStream},
38   * and tracks the last-modified timestamp of the file (if possible).
39   *
40   * @author Rob Harrop
41   * @author Juergen Hoeller
42   * @since 2.0
43   * @see org.springframework.core.io.Resource#getInputStream()
44   * @see org.springframework.core.io.Resource#getFile()
45   * @see org.springframework.core.io.ResourceLoader
46   */
47  public class ResourceScriptSource implements ScriptSource {
48  
49  	/** Logger available to subclasses */
50  	protected final Log logger = LogFactory.getLog(getClass());
51  
52  	private EncodedResource resource;
53  
54  	private long lastModified = -1;
55  
56  	private final Object lastModifiedMonitor = new Object();
57  
58  
59  	/**
60  	 * Create a new ResourceScriptSource for the given resource.
61  	 * @param resource the EncodedResource to load the script from
62  	 */
63  	public ResourceScriptSource(EncodedResource resource) {
64  		Assert.notNull(resource, "Resource must not be null");
65  		this.resource = resource;
66  	}
67  
68  	/**
69  	 * Create a new ResourceScriptSource for the given resource.
70  	 * @param resource the Resource to load the script from (using UTF-8 encoding)
71  	 */
72  	public ResourceScriptSource(Resource resource) {
73  		Assert.notNull(resource, "Resource must not be null");
74  		this.resource = new EncodedResource(resource, "UTF-8");
75  	}
76  
77  
78  	/**
79  	 * Return the {@link org.springframework.core.io.Resource} to load the
80  	 * script from.
81  	 */
82  	public final Resource getResource() {
83  		return this.resource.getResource();
84  	}
85  
86  	/**
87  	 * Set the encoding used for reading the script resource.
88  	 * <p>The default value for regular Resources is "UTF-8".
89  	 * A {@code null} value implies the platform default.
90  	 */
91  	public void setEncoding(String encoding) {
92  		this.resource = new EncodedResource(this.resource.getResource(), encoding);
93  	}
94  
95  
96  	@Override
97  	public String getScriptAsString() throws IOException {
98  		synchronized (this.lastModifiedMonitor) {
99  			this.lastModified = retrieveLastModifiedTime();
100 		}
101 		Reader reader = this.resource.getReader();
102 		return FileCopyUtils.copyToString(reader);
103 	}
104 
105 	@Override
106 	public boolean isModified() {
107 		synchronized (this.lastModifiedMonitor) {
108 			return (this.lastModified < 0 || retrieveLastModifiedTime() > this.lastModified);
109 		}
110 	}
111 
112 	/**
113 	 * Retrieve the current last-modified timestamp of the underlying resource.
114 	 * @return the current timestamp, or 0 if not determinable
115 	 */
116 	protected long retrieveLastModifiedTime() {
117 		try {
118 			return getResource().lastModified();
119 		}
120 		catch (IOException ex) {
121 			if (logger.isDebugEnabled()) {
122 				logger.debug(getResource() + " could not be resolved in the file system - " +
123 						"current timestamp not available for script modification check", ex);
124 			}
125 			return 0;
126 		}
127 	}
128 
129 	@Override
130 	public String suggestedClassName() {
131 		return StringUtils.stripFilenameExtension(getResource().getFilename());
132 	}
133 
134 	@Override
135 	public String toString() {
136 		return this.resource.toString();
137 	}
138 
139 }