diff --git a/deegree-core/deegree-core-gdal/src/main/java/org/deegree/commons/gdal/GdalDatasetPool.java b/deegree-core/deegree-core-gdal/src/main/java/org/deegree/commons/gdal/GdalDatasetPool.java index f57e3ae6fa..3a4295110d 100644 --- a/deegree-core/deegree-core-gdal/src/main/java/org/deegree/commons/gdal/GdalDatasetPool.java +++ b/deegree-core/deegree-core-gdal/src/main/java/org/deegree/commons/gdal/GdalDatasetPool.java @@ -105,4 +105,8 @@ void shutdown() { } } + public void removeAndClose(File file) throws IOException { + pool.removeAndClose(file.getCanonicalFile().toString()); + } + } diff --git a/deegree-core/deegree-core-gdal/src/main/java/org/deegree/commons/gdal/pool/LimitedKeyedResourcePool.java b/deegree-core/deegree-core-gdal/src/main/java/org/deegree/commons/gdal/pool/LimitedKeyedResourcePool.java index 0f6daf7e1a..8cb6fefdab 100644 --- a/deegree-core/deegree-core-gdal/src/main/java/org/deegree/commons/gdal/pool/LimitedKeyedResourcePool.java +++ b/deegree-core/deegree-core-gdal/src/main/java/org/deegree/commons/gdal/pool/LimitedKeyedResourcePool.java @@ -27,6 +27,7 @@ ----------------------------------------------------------------------------*/ package org.deegree.commons.gdal.pool; +import static org.apache.commons.io.IOUtils.closeQuietly; import static org.slf4j.LoggerFactory.getLogger; import java.io.Closeable; @@ -153,4 +154,12 @@ private synchronized BlockingQueue getQueue(final String key) { return queue; } + public void removeAndClose(final String key) { + LOG.debug("Destroying resource, key: " + key); + BlockingQueue queue = getQueue(key); + T resource = queue.poll(); + closeQuietly(resource); + keyTracker.remove(key); + } + } diff --git a/deegree-datastores/deegree-tilestores/deegree-tilestore-gdal/src/main/java/org/deegree/tile/persistence/gdal/GdalTileDataLevel.java b/deegree-datastores/deegree-tilestores/deegree-tilestore-gdal/src/main/java/org/deegree/tile/persistence/gdal/GdalTileDataLevel.java index b28cde5fda..60821afdc5 100644 --- a/deegree-datastores/deegree-tilestores/deegree-tilestore-gdal/src/main/java/org/deegree/tile/persistence/gdal/GdalTileDataLevel.java +++ b/deegree-datastores/deegree-tilestores/deegree-tilestore-gdal/src/main/java/org/deegree/tile/persistence/gdal/GdalTileDataLevel.java @@ -28,6 +28,7 @@ package org.deegree.tile.persistence.gdal; import java.io.File; +import java.io.IOException; import java.util.List; import org.deegree.commons.gdal.GdalDataset; @@ -47,7 +48,7 @@ * @author Markus Schneider * @since 3.4 */ -class GdalTileDataLevel implements TileDataLevel { +public class GdalTileDataLevel implements TileDataLevel { private final TileMatrix metadata; @@ -120,4 +121,8 @@ public List getStyles() { return null; } + public void clearPool() throws IOException { + gdalSettings.getDatasetPool().removeAndClose(file); + } + } diff --git a/deegree-datastores/deegree-tilestores/deegree-tilestore-geotiff/src/main/java/org/deegree/tile/persistence/geotiff/GeoTIFFTileDataLevel.java b/deegree-datastores/deegree-tilestores/deegree-tilestore-geotiff/src/main/java/org/deegree/tile/persistence/geotiff/GeoTIFFTileDataLevel.java index 3d3a31c7d1..862d70f0b7 100644 --- a/deegree-datastores/deegree-tilestores/deegree-tilestore-geotiff/src/main/java/org/deegree/tile/persistence/geotiff/GeoTIFFTileDataLevel.java +++ b/deegree-datastores/deegree-tilestores/deegree-tilestore-geotiff/src/main/java/org/deegree/tile/persistence/geotiff/GeoTIFFTileDataLevel.java @@ -119,4 +119,8 @@ public List getStyles() { return null; } + public void clearPool() { + this.readerPool.clear(); + } + } diff --git a/deegree-datastores/deegree-tilestores/deegree-tilestore-geotiff/src/main/java/org/deegree/tile/persistence/geotiff/GeoTiffTileDataSetMapBuilder.java b/deegree-datastores/deegree-tilestores/deegree-tilestore-geotiff/src/main/java/org/deegree/tile/persistence/geotiff/GeoTiffTileDataSetMapBuilder.java index a362b89127..4ed995f89a 100644 --- a/deegree-datastores/deegree-tilestores/deegree-tilestore-geotiff/src/main/java/org/deegree/tile/persistence/geotiff/GeoTiffTileDataSetMapBuilder.java +++ b/deegree-datastores/deegree-tilestores/deegree-tilestore-geotiff/src/main/java/org/deegree/tile/persistence/geotiff/GeoTiffTileDataSetMapBuilder.java @@ -111,19 +111,20 @@ Map buildTileDataSetMap() throws IOException { continue; } - ImageInputStream iis = createImageInputStream(file); - reader.setInput(iis, false, true); - IIOMetadata md = reader.getImageMetadata(0); - Envelope envelope = getEnvelope(md, reader.getWidth(0), reader.getHeight(0), null); - - if (envelope == null) { - throw new ResourceInitException( - "No envelope information could be read from GeoTIFF. " + "Please add one to the GeoTIFF."); + try (ImageInputStream iis = createImageInputStream(file)) { + reader.setInput(iis, false, true); + IIOMetadata md = reader.getImageMetadata(0); + Envelope envelope = getEnvelope(md, reader.getWidth(0), reader.getHeight(0), null); + reader.dispose(); + + if (envelope == null) { + throw new ResourceInitException( + "No envelope information could be read from GeoTIFF. " + "Please add one to the GeoTIFF."); + } + LOG.debug("Envelope from GeoTIFF was {}.", envelope); + + map.put(id, builder.buildTileDataSet(tds, location, envelope)); } - - LOG.debug("Envelope from GeoTIFF was {}.", envelope); - - map.put(id, builder.buildTileDataSet(tds, location, envelope)); } return map; } diff --git a/deegree-datastores/deegree-tilestores/deegree-tilestore-geotiff/src/main/java/org/deegree/tile/persistence/geotiff/ImageReaderFactory.java b/deegree-datastores/deegree-tilestores/deegree-tilestore-geotiff/src/main/java/org/deegree/tile/persistence/geotiff/ImageReaderFactory.java index c9f329b5ed..7050285b64 100644 --- a/deegree-datastores/deegree-tilestores/deegree-tilestore-geotiff/src/main/java/org/deegree/tile/persistence/geotiff/ImageReaderFactory.java +++ b/deegree-datastores/deegree-tilestores/deegree-tilestore-geotiff/src/main/java/org/deegree/tile/persistence/geotiff/ImageReaderFactory.java @@ -71,8 +71,10 @@ public ImageReaderFactory(File file) { @Override public void destroyObject(PooledObject pooledObject) throws Exception { - ImageReader reader = (ImageReader) pooledObject; - reader.dispose(); + ImageReader reader = pooledObject.getObject(); + try (ImageInputStream input = (ImageInputStream) reader.getInput()) { + reader.dispose(); + } } @Override @@ -95,16 +97,15 @@ public void passivateObject(PooledObject pooledObject) throws Excep @Override public PooledObject makeObject() throws Exception { - ImageInputStream iis = null; ImageReader reader = null; Iterator readers = getImageReadersBySuffix("tiff"); while (readers.hasNext() && !(reader instanceof TIFFImageReader)) { reader = readers.next(); } - iis = createImageInputStream(file); + ImageInputStream iis = createImageInputStream(file); // already checked in provider reader.setInput(iis); - return new DefaultPooledObject(reader); + return new DefaultPooledObject<>(reader); } } diff --git a/deegree-datastores/deegree-tilestores/deegree-tilestore-geotiff/src/main/java/org/deegree/tile/tilematrixset/geotiff/GeoTiffTileMatrixSetBuilder.java b/deegree-datastores/deegree-tilestores/deegree-tilestore-geotiff/src/main/java/org/deegree/tile/tilematrixset/geotiff/GeoTiffTileMatrixSetBuilder.java index 00f8c287c0..6fa43bef24 100644 --- a/deegree-datastores/deegree-tilestores/deegree-tilestore-geotiff/src/main/java/org/deegree/tile/tilematrixset/geotiff/GeoTiffTileMatrixSetBuilder.java +++ b/deegree-datastores/deegree-tilestores/deegree-tilestore-geotiff/src/main/java/org/deegree/tile/tilematrixset/geotiff/GeoTiffTileMatrixSetBuilder.java @@ -81,8 +81,6 @@ public GeoTiffTileMatrixSetBuilder(GeoTIFFTileMatrixSetConfig cfg, ResourceMetad @Override public TileMatrixSet build() { - ImageReader reader = null; - ImageInputStream iis = null; try { ICRS crs = null; @@ -99,6 +97,7 @@ public TileMatrixSet build() { ImageIO.scanForPlugins(); + ImageReader reader = null; Iterator readers = getImageReadersBySuffix("tiff"); while (readers.hasNext() && !(reader instanceof TIFFImageReader)) { reader = readers.next(); @@ -110,42 +109,43 @@ public TileMatrixSet build() { throw new ResourceInitException("The file " + file + " does not exist."); } - iis = createImageInputStream(file); - // this is already checked in provider - reader.setInput(iis, false, true); - int num = reader.getNumImages(true); - IIOMetadata md = reader.getImageMetadata(0); - Envelope envelope = getEnvelopeAndCrs(md, reader.getWidth(0), reader.getHeight(0), crs); + try (ImageInputStream iis = createImageInputStream(file);) { + // this is already checked in provider + reader.setInput(iis, false, true); + int num = reader.getNumImages(true); + IIOMetadata md = reader.getImageMetadata(0); + Envelope envelope = getEnvelopeAndCrs(md, reader.getWidth(0), reader.getHeight(0), crs); - if (envelope == null) { - throw new ResourceInitException( - "No envelope information could be read from GeoTIFF. " + "Please add one to the GeoTIFF."); - } + if (envelope == null) { + throw new ResourceInitException( + "No envelope information could be read from GeoTIFF. " + "Please add one to the GeoTIFF."); + } - LOG.debug("Envelope from GeoTIFF was {}.", envelope); - - SpatialMetadata smd = new SpatialMetadata(envelope, singletonList(envelope.getCoordinateSystem())); - - List matrices = new ArrayList(); - - for (int i = 0; i < num; ++i) { - int tw = reader.getTileWidth(i); - int th = reader.getTileHeight(i); - int width = reader.getWidth(i); - int height = reader.getHeight(i); - int numx = (int) Math.ceil((double) width / (double) tw); - int numy = (int) Math.ceil((double) height / (double) th); - double res = Math.max(envelope.getSpan0() / width, envelope.getSpan1() / height); - String id = Double.toString(res / DEFAULT_PIXEL_SIZE); - TileMatrix tmd = new TileMatrix(id, smd, BigInteger.valueOf(tw), BigInteger.valueOf(th), res, - BigInteger.valueOf(numx), BigInteger.valueOf(numy)); - matrices.add(tmd); - LOG.debug("Level {} has {}x{} tiles of {}x{} pixels, resolution is {}", - new Object[] { i, numx, numy, tw, th, res }); + LOG.debug("Envelope from GeoTIFF was {}.", envelope); + + SpatialMetadata smd = new SpatialMetadata(envelope, singletonList(envelope.getCoordinateSystem())); + + List matrices = new ArrayList(); + + for (int i = 0; i < num; ++i) { + int tw = reader.getTileWidth(i); + int th = reader.getTileHeight(i); + int width = reader.getWidth(i); + int height = reader.getHeight(i); + int numx = (int) Math.ceil((double) width / (double) tw); + int numy = (int) Math.ceil((double) height / (double) th); + double res = Math.max(envelope.getSpan0() / width, envelope.getSpan1() / height); + String id = Double.toString(res / DEFAULT_PIXEL_SIZE); + TileMatrix tmd = new TileMatrix(id, smd, BigInteger.valueOf(tw), BigInteger.valueOf(th), res, + BigInteger.valueOf(numx), BigInteger.valueOf(numy)); + matrices.add(tmd); + LOG.debug("Level {} has {}x{} tiles of {}x{} pixels, resolution is {}", + new Object[] { i, numx, numy, tw, th, res }); + } + reader.dispose(); + return new TileMatrixSet(file.getName().substring(0, file.getName().length() - 4), null, matrices, smd, + metadata); } - - return new TileMatrixSet(file.getName().substring(0, file.getName().length() - 4), null, matrices, smd, - metadata); } catch (Exception e) { throw new ResourceInitException("Could not create tile matrix set. Reason: " + e.getLocalizedMessage(), e);