KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > scripting > support > ResourceScriptSource


1 /*
2  * Copyright 2002-2007 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.File JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.FileReader JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.io.Reader JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.springframework.core.io.Resource;
30 import org.springframework.scripting.ScriptSource;
31 import org.springframework.util.Assert;
32 import org.springframework.util.FileCopyUtils;
33
34 /**
35  * {@link org.springframework.scripting.ScriptSource} implementation
36  * based on Spring's {@link org.springframework.core.io.Resource}
37  * abstraction. Loads the script text from the underlying Resource's
38  * {@link org.springframework.core.io.Resource#getFile() File} or
39  * {@link org.springframework.core.io.Resource#getInputStream() InputStream},
40  * and tracks the last-modified timestamp of the file (if possible).
41  *
42  * @author Rob Harrop
43  * @author Juergen Hoeller
44  * @since 2.0
45  * @see org.springframework.core.io.Resource#getInputStream()
46  * @see org.springframework.core.io.Resource#getFile()
47  * @see org.springframework.core.io.ResourceLoader
48  */

49 public class ResourceScriptSource implements ScriptSource {
50
51     /** Logger available to subclasses */
52     protected final Log logger = LogFactory.getLog(getClass());
53
54     private final Resource resource;
55
56     private long lastModified = -1;
57
58     private final Object JavaDoc lastModifiedMonitor = new Object JavaDoc();
59
60
61     /**
62      * Create a new ResourceScriptSource for the given resource.
63      * @param resource the Resource to load the script from
64      */

65     public ResourceScriptSource(Resource resource) {
66         Assert.notNull(resource, "Resource must not be null");
67         this.resource = resource;
68     }
69
70     /**
71      * Return the {@link org.springframework.core.io.Resource} to load the
72      * script from.
73      */

74     public final Resource getResource() {
75         return this.resource;
76     }
77
78
79     public String JavaDoc getScriptAsString() throws IOException JavaDoc {
80         File JavaDoc file = null;
81         try {
82             file = getResource().getFile();
83         }
84         catch (IOException JavaDoc ex) {
85             if (logger.isDebugEnabled()) {
86                 logger.debug(getResource() + " could not be resolved in the file system - " +
87                         "cannot store last-modified timestamp for obtained script", ex);
88             }
89         }
90         synchronized (this.lastModifiedMonitor) {
91             this.lastModified = (file != null ? file.lastModified() : 0);
92         }
93         Reader JavaDoc reader = null;
94         if (file != null) {
95             try {
96                 // Try to get a FileReader first: generally more reliable.
97
reader = new FileReader JavaDoc(file);
98             }
99             catch (FileNotFoundException JavaDoc ex) {
100                 if (logger.isDebugEnabled()) {
101                     logger.debug("Could not open FileReader for " + this.resource +
102                             " - falling back to InputStreamReader", ex);
103                 }
104             }
105         }
106         if (reader == null) {
107             reader = new InputStreamReader JavaDoc(this.resource.getInputStream());
108         }
109         return FileCopyUtils.copyToString(reader);
110     }
111
112     public boolean isModified() {
113         synchronized (this.lastModifiedMonitor) {
114             return (this.lastModified < 0 || retrieveLastModifiedTime() > this.lastModified);
115         }
116     }
117
118     /**
119      * Retrieve the current last-modified timestamp of the underlying resource.
120      * @return the current timestamp, or 0 if not determinable
121      */

122     protected long retrieveLastModifiedTime() {
123         try {
124             return getResource().getFile().lastModified();
125         }
126         catch (IOException JavaDoc ex) {
127             if (logger.isDebugEnabled()) {
128                 logger.debug(getResource() + " could not be resolved in the file system - " +
129                         "current timestamp not available for script modification check", ex);
130             }
131             return 0;
132         }
133     }
134
135
136     public String JavaDoc toString() {
137         return this.resource.toString();
138     }
139
140 }
141
Popular Tags