Skip to content

Commit

Permalink
Replace try/finally with try-with-resources, add a PMD rule to enforc…
Browse files Browse the repository at this point in the history
…e it
  • Loading branch information
aaime committed Dec 29, 2020
1 parent c02517b commit 3637dc5
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,15 @@ private File findOrCreateConfFile() throws IOException {
+ getClass().getResource(templateLocation).toExternalForm());
// grab template from classpath
try {
InputStream templateStream = getClass().getResourceAsStream(templateLocation);
try {
try (InputStream templateStream =
getClass().getResourceAsStream(templateLocation)) {
OutputStream output = new FileOutputStream(xmlFile);
try {
IOUtils.copy(templateStream, output);
} finally {
output.flush();
output.close();
}
} finally {
templateStream.close();
}
} catch (IOException e) {
throw new IOException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,14 @@ public BlankTileException(RequestFilter reqFilter) {

private Resource getBlankTile() {
// Use the built-in one:
InputStream is = null;
try {
is = GeoWebCacheDispatcher.class.getResourceAsStream("blank.png");
try (InputStream is = GeoWebCacheDispatcher.class.getResourceAsStream("blank.png")) {
byte[] blankTile = new byte[425];
int ret = is.read(blankTile);
log.info("Read " + ret + " from blank PNG file (expected 425).");

return new ByteArrayResource(blankTile);
} catch (IOException ioe) {
log.error(ioe);
} finally {
try {
if (is != null) is.close();
} catch (IOException e) {
// close quietly
}
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public NIOLockProvider(String root) throws ConfigurationException {
this.maxLockAttempts = 120 * 1000 / waitBeforeRetry;
}

@SuppressWarnings("PMD.CloseResource") // complex but seemingly correct resource handling
@SuppressWarnings({"PMD.CloseResource", "PMD.UseTryWithResources"})
// complex but seemingly correct resource handling
public LockProvider.Lock getLock(final String lockKey) throws GeoWebCacheException {
File file = null;
// first off, synchronize among threads in the same jvm (the nio locks won't lock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ public class MetastoreRemover {
public MetastoreRemover(DefaultStorageFinder finder) throws Exception {
this.storageFinder = finder;
File root = new File(storageFinder.getDefaultPath());
Connection conn = null;
try {
conn = getMetaStoreConnection(root);
try (Connection conn = getMetaStoreConnection(root)) {
if (conn != null) {
log.info("Migrating the old metastore to filesystem storage");
SingleConnectionDataSource ds = new SingleConnectionDataSource(conn, false);
Expand All @@ -74,10 +72,6 @@ public MetastoreRemover(DefaultStorageFinder finder) throws Exception {
removeTiles(template);
}
}
} finally {
if (conn != null) {
conn.close();
}
}

// wipe out the entire database if the db location is the default one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,8 @@ private void mockSeed(TileLayer layer, int numFiles, int fileSize)

File tileDir = tilePath.getParentFile();
tileDir.mkdirs();
FileOutputStream fout = new FileOutputStream(tilePath);
try {
try (FileOutputStream fout = new FileOutputStream(tilePath)) {
fout.write(mockTileContents);
} finally {
fout.close();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,10 @@ public class FixtureUtilities {
/** Load {@link Properties} from a {@link File}. */
public static Properties loadProperties(File file) {
try {
InputStream input = null;
try {
input = new BufferedInputStream(new FileInputStream(file));
try (InputStream input = new BufferedInputStream(new FileInputStream(file))) {
Properties properties = new Properties();
properties.load(input);
return properties;
} finally {
if (input != null) {
input.close();
}
}
} catch (Exception e) {
throw new RuntimeException(e);
Expand Down
1 change: 1 addition & 0 deletions geowebcache/pmd-ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ under the License.
<rule ref="category/java/bestpractices.xml/UseAssertEqualsInsteadOfAssertTrue" />
<rule ref="category/java/bestpractices.xml/UseAssertNullInsteadOfAssertTrue" />
<rule ref="category/java/bestpractices.xml/UseAssertSameInsteadOfAssertTrue" />
<rule ref="category/java/bestpractices.xml/UseTryWithResources" />

<rule ref="category/java/codestyle.xml/DontImportJavaLang" />
<rule ref="category/java/codestyle.xml/DuplicateImports" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,8 @@ public boolean get(final TileObject tile) throws StorageException {
true,
connection -> {
// instantiating geotools mbtiles reader
MBTilesFile mbtiles =
GeoToolsMbtilesUtils.getMBTilesFile(connection, file);
try {
try (MBTilesFile mbtiles =
GeoToolsMbtilesUtils.getMBTilesFile(connection, file)) {

final boolean gzipped = tileIsGzipped(tile);

Expand Down Expand Up @@ -279,8 +278,6 @@ public boolean get(final TileObject tile) throws StorageException {
"Error loading tile '%s' from MBTiles file '%s'.",
tile,
file);
} finally {
mbtiles.close();
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(
Expand Down

0 comments on commit 3637dc5

Please sign in to comment.