Skip to content

Commit 57851de

Browse files
committed
clarified Resource's "getFilename" method to consistently return null if resource type does not have a filename (SPR-9043)
1 parent 1e1f8c9 commit 57851de

File tree

4 files changed

+41
-41
lines changed

4 files changed

+41
-41
lines changed

org.springframework.core/src/main/java/org/springframework/core/io/AbstractResource.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -150,11 +150,11 @@ public Resource createRelative(String relativePath) throws IOException {
150150
}
151151

152152
/**
153-
* This implementation always throws IllegalStateException,
154-
* assuming that the resource does not have a filename.
153+
* This implementation always returns <code>null</code>,
154+
* assuming that this resource type does not have a filename.
155155
*/
156156
public String getFilename() throws IllegalStateException {
157-
throw new IllegalStateException(getDescription() + " does not have a filename");
157+
return null;
158158
}
159159

160160

org.springframework.core/src/main/java/org/springframework/core/io/Resource.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -116,8 +116,10 @@ public interface Resource extends InputStreamSource {
116116
Resource createRelative(String relativePath) throws IOException;
117117

118118
/**
119-
* Return a filename for this resource, i.e. typically the last
119+
* Determine a filename for this resource, i.e. typically the last
120120
* part of the path: for example, "myfile.txt".
121+
* <p>Returns <code>null</code> if this type of resource does not
122+
* have a filename.
121123
*/
122124
String getFilename();
123125

org.springframework.core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,7 @@
2323

2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
26-
import org.springframework.core.io.AbstractFileResolvingResource;
26+
2727
import org.springframework.core.io.Resource;
2828
import org.springframework.util.CollectionUtils;
2929
import org.springframework.util.DefaultPropertiesPersister;
@@ -179,9 +179,7 @@ protected void loadProperties(Properties props) throws IOException {
179179
InputStream is = null;
180180
try {
181181
is = location.getInputStream();
182-
183-
String filename = (location instanceof AbstractFileResolvingResource) ?
184-
location.getFilename() : null;
182+
String filename = location.getFilename();
185183
if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
186184
this.propertiesPersister.loadFromXml(props, is);
187185
}

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/jasperreports/AbstractJasperReportsView.java

+30-30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -485,38 +485,38 @@ protected JasperReport loadReport() {
485485
*/
486486
protected final JasperReport loadReport(Resource resource) {
487487
try {
488-
String fileName = resource.getFilename();
489-
if (fileName.endsWith(".jasper")) {
490-
// Load pre-compiled report.
491-
if (logger.isInfoEnabled()) {
492-
logger.info("Loading pre-compiled Jasper Report from " + resource);
493-
}
494-
InputStream is = resource.getInputStream();
495-
try {
496-
return (JasperReport) JRLoader.loadObject(is);
497-
}
498-
finally {
499-
is.close();
500-
}
501-
}
502-
else if (fileName.endsWith(".jrxml")) {
503-
// Compile report on-the-fly.
504-
if (logger.isInfoEnabled()) {
505-
logger.info("Compiling Jasper Report loaded from " + resource);
506-
}
507-
InputStream is = resource.getInputStream();
508-
try {
509-
JasperDesign design = JRXmlLoader.load(is);
510-
return JasperCompileManager.compileReport(design);
488+
String filename = resource.getFilename();
489+
if (filename != null) {
490+
if (filename.endsWith(".jasper")) {
491+
// Load pre-compiled report.
492+
if (logger.isInfoEnabled()) {
493+
logger.info("Loading pre-compiled Jasper Report from " + resource);
494+
}
495+
InputStream is = resource.getInputStream();
496+
try {
497+
return (JasperReport) JRLoader.loadObject(is);
498+
}
499+
finally {
500+
is.close();
501+
}
511502
}
512-
finally {
513-
is.close();
503+
else if (filename.endsWith(".jrxml")) {
504+
// Compile report on-the-fly.
505+
if (logger.isInfoEnabled()) {
506+
logger.info("Compiling Jasper Report loaded from " + resource);
507+
}
508+
InputStream is = resource.getInputStream();
509+
try {
510+
JasperDesign design = JRXmlLoader.load(is);
511+
return JasperCompileManager.compileReport(design);
512+
}
513+
finally {
514+
is.close();
515+
}
514516
}
515517
}
516-
else {
517-
throw new IllegalArgumentException(
518-
"Report filename [" + fileName + "] must end in either .jasper or .jrxml");
519-
}
518+
throw new IllegalArgumentException(
519+
"Report filename [" + filename + "] must end in either .jasper or .jrxml");
520520
}
521521
catch (IOException ex) {
522522
throw new ApplicationContextException(

0 commit comments

Comments
 (0)