|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2015 the original author or authors. |
| 2 | + * Copyright 2002-2016 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.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.web.servlet.view.script;
|
18 | 18 |
|
19 |
| -import java.io.FileNotFoundException; |
20 | 19 | import java.io.IOException;
|
21 | 20 | import java.io.InputStreamReader;
|
22 | 21 | import java.net.URL;
|
|
57 | 56 | * An {@link AbstractUrlBasedView} subclass designed to run any template library
|
58 | 57 | * based on a JSR-223 script engine.
|
59 | 58 | *
|
60 |
| - * <p>If not set, each property is auto-detected by looking up up a single |
| 59 | + * <p>If not set, each property is auto-detected by looking up a single |
61 | 60 | * {@link ScriptTemplateConfig} bean in the web application context and using
|
62 | 61 | * it to obtain the configured properties.
|
63 | 62 | *
|
@@ -189,19 +188,6 @@ public void setResourceLoaderPath(String resourceLoaderPath) {
|
189 | 188 | this.resourceLoaderPath = resourceLoaderPath;
|
190 | 189 | }
|
191 | 190 |
|
192 |
| - @Override |
193 |
| - public boolean checkResource(Locale locale) throws Exception { |
194 |
| - try { |
195 |
| - getTemplate(getUrl()); |
196 |
| - return true; |
197 |
| - } |
198 |
| - catch (FileNotFoundException exc) { |
199 |
| - if (logger.isDebugEnabled()) { |
200 |
| - logger.debug("No ScriptTemplate view found for URL: " + getUrl()); |
201 |
| - } |
202 |
| - return false; |
203 |
| - } |
204 |
| - } |
205 | 191 |
|
206 | 192 | @Override
|
207 | 193 | protected void initApplicationContext(ApplicationContext context) {
|
@@ -260,7 +246,6 @@ else if (this.engine != null) {
|
260 | 246 | Assert.isTrue(this.renderFunction != null, "The 'renderFunction' property must be defined.");
|
261 | 247 | }
|
262 | 248 |
|
263 |
| - |
264 | 249 | protected ScriptEngine getEngine() {
|
265 | 250 | if (Boolean.FALSE.equals(this.sharedEngine)) {
|
266 | 251 | Map<Object, ScriptEngine> engines = enginesHolder.get();
|
@@ -295,17 +280,17 @@ protected ScriptEngine createEngineFromName() {
|
295 | 280 |
|
296 | 281 | protected void loadScripts(ScriptEngine engine) {
|
297 | 282 | if (!ObjectUtils.isEmpty(this.scripts)) {
|
298 |
| - try { |
299 |
| - for (String script : this.scripts) { |
300 |
| - Resource resource = this.resourceLoader.getResource(script); |
301 |
| - if (!resource.exists()) { |
302 |
| - throw new IllegalStateException("Script resource [" + script + "] not found"); |
303 |
| - } |
| 283 | + for (String script : this.scripts) { |
| 284 | + Resource resource = this.resourceLoader.getResource(script); |
| 285 | + if (!resource.exists()) { |
| 286 | + throw new IllegalStateException("Script resource [" + script + "] not found"); |
| 287 | + } |
| 288 | + try { |
304 | 289 | engine.eval(new InputStreamReader(resource.getInputStream()));
|
305 | 290 | }
|
306 |
| - } |
307 |
| - catch (Exception ex) { |
308 |
| - throw new IllegalStateException("Failed to load script", ex); |
| 291 | + catch (Throwable ex) { |
| 292 | + throw new IllegalStateException("Failed to evaluate script [" + script + "]", ex); |
| 293 | + } |
309 | 294 | }
|
310 | 295 | }
|
311 | 296 | }
|
@@ -345,6 +330,11 @@ protected ScriptTemplateConfig autodetectViewConfig() throws BeansException {
|
345 | 330 | }
|
346 | 331 |
|
347 | 332 |
|
| 333 | + @Override |
| 334 | + public boolean checkResource(Locale locale) throws Exception { |
| 335 | + return this.resourceLoader.getResource(getUrl()).exists(); |
| 336 | + } |
| 337 | + |
348 | 338 | @Override
|
349 | 339 | protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) {
|
350 | 340 | super.prepareResponse(request, response);
|
|
0 commit comments