clarified Resource's "getFilename" method to consistently return null if resource type does not have a filename (SPR-9043)

This commit is contained in:
Juergen Hoeller 2012-02-09 17:58:35 +01:00
parent 1e1f8c912b
commit 57851de88e
4 changed files with 41 additions and 41 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -150,11 +150,11 @@ public abstract class AbstractResource implements Resource {
}
/**
* This implementation always throws IllegalStateException,
* assuming that the resource does not have a filename.
* This implementation always returns <code>null</code>,
* assuming that this resource type does not have a filename.
*/
public String getFilename() throws IllegalStateException {
throw new IllegalStateException(getDescription() + " does not have a filename");
return null;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -116,8 +116,10 @@ public interface Resource extends InputStreamSource {
Resource createRelative(String relativePath) throws IOException;
/**
* Return a filename for this resource, i.e. typically the last
* Determine a filename for this resource, i.e. typically the last
* part of the path: for example, "myfile.txt".
* <p>Returns <code>null</code> if this type of resource does not
* have a filename.
*/
String getFilename();

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -23,7 +23,7 @@ import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.AbstractFileResolvingResource;
import org.springframework.core.io.Resource;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DefaultPropertiesPersister;
@ -179,9 +179,7 @@ public abstract class PropertiesLoaderSupport {
InputStream is = null;
try {
is = location.getInputStream();
String filename = (location instanceof AbstractFileResolvingResource) ?
location.getFilename() : null;
String filename = location.getFilename();
if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
this.propertiesPersister.loadFromXml(props, is);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -485,38 +485,38 @@ public abstract class AbstractJasperReportsView extends AbstractUrlBasedView {
*/
protected final JasperReport loadReport(Resource resource) {
try {
String fileName = resource.getFilename();
if (fileName.endsWith(".jasper")) {
// Load pre-compiled report.
if (logger.isInfoEnabled()) {
logger.info("Loading pre-compiled Jasper Report from " + resource);
String filename = resource.getFilename();
if (filename != null) {
if (filename.endsWith(".jasper")) {
// Load pre-compiled report.
if (logger.isInfoEnabled()) {
logger.info("Loading pre-compiled Jasper Report from " + resource);
}
InputStream is = resource.getInputStream();
try {
return (JasperReport) JRLoader.loadObject(is);
}
finally {
is.close();
}
}
InputStream is = resource.getInputStream();
try {
return (JasperReport) JRLoader.loadObject(is);
}
finally {
is.close();
else if (filename.endsWith(".jrxml")) {
// Compile report on-the-fly.
if (logger.isInfoEnabled()) {
logger.info("Compiling Jasper Report loaded from " + resource);
}
InputStream is = resource.getInputStream();
try {
JasperDesign design = JRXmlLoader.load(is);
return JasperCompileManager.compileReport(design);
}
finally {
is.close();
}
}
}
else if (fileName.endsWith(".jrxml")) {
// Compile report on-the-fly.
if (logger.isInfoEnabled()) {
logger.info("Compiling Jasper Report loaded from " + resource);
}
InputStream is = resource.getInputStream();
try {
JasperDesign design = JRXmlLoader.load(is);
return JasperCompileManager.compileReport(design);
}
finally {
is.close();
}
}
else {
throw new IllegalArgumentException(
"Report filename [" + fileName + "] must end in either .jasper or .jrxml");
}
throw new IllegalArgumentException(
"Report filename [" + filename + "] must end in either .jasper or .jrxml");
}
catch (IOException ex) {
throw new ApplicationContextException(