From 2903870ec43755ed6897ee83f3db27bb016bbcec Mon Sep 17 00:00:00 2001 From: Daniele Romagnoli Date: Thu, 14 Mar 2013 12:18:52 +0100 Subject: [PATCH] Fixing Issue #49: Closing dataset used for metadata initialization --- .../GDALCommonIIOImageMetadata.java | 98 +++++++++++-------- .../imageio/plugins/jp2ecw/JP2KReadTest.java | 43 ++++---- 2 files changed, 75 insertions(+), 66 deletions(-) diff --git a/library/gdalframework/src/main/java/it/geosolutions/imageio/gdalframework/GDALCommonIIOImageMetadata.java b/library/gdalframework/src/main/java/it/geosolutions/imageio/gdalframework/GDALCommonIIOImageMetadata.java index 03845dba7..6e532d8c9 100644 --- a/library/gdalframework/src/main/java/it/geosolutions/imageio/gdalframework/GDALCommonIIOImageMetadata.java +++ b/library/gdalframework/src/main/java/it/geosolutions/imageio/gdalframework/GDALCommonIIOImageMetadata.java @@ -131,49 +131,63 @@ public GDALCommonIIOImageMetadata(Dataset dataset, String name, return; setDatasetDescription(dataset.GetDescription()); Driver driver = null; - try { - driver = dataset.GetDriver(); - if (driver != null) { - setDriverDescription(driver.GetDescription()); - setDriverName(driver.getShortName()); - } - gdalDomainMetadataMap = new HashMap>(); - - // // - // - // Getting Metadata from Default domain and Image_structure domain - // - // // - Map defMap = dataset.GetMetadata_Dict(GDALUtilities.GDALMetadataDomain.DEFAULT); - if (defMap != null && defMap.size() > 0) - gdalDomainMetadataMap.put(GDALUtilities.GDALMetadataDomain.DEFAULT_KEY_MAP, defMap); - - Map imageStMap = dataset.GetMetadata_Dict(GDALUtilities.GDALMetadataDomain.IMAGESTRUCTURE); - if (imageStMap != null && imageStMap.size() > 0) - gdalDomainMetadataMap.put(GDALUtilities.GDALMetadataDomain.IMAGESTRUCTURE,imageStMap); - - // // - // - // Initializing member if needed - // - // // - if (initializationRequired) - setMembers(dataset); - setGeoreferencingInfo(dataset); - // clean up data set in order to avoid keeping them around for a lot - // of time. - } finally { - if (driver != null){ - try{ + try { + driver = dataset.GetDriver(); + if (driver != null) { + setDriverDescription(driver.GetDescription()); + setDriverName(driver.getShortName()); + } + gdalDomainMetadataMap = new HashMap>(); + + // // + // + // Getting Metadata from Default domain and Image_structure domain + // + // // + Map defMap = dataset + .GetMetadata_Dict(GDALUtilities.GDALMetadataDomain.DEFAULT); + if (defMap != null && defMap.size() > 0) + gdalDomainMetadataMap.put(GDALUtilities.GDALMetadataDomain.DEFAULT_KEY_MAP, defMap); + + Map imageStMap = dataset + .GetMetadata_Dict(GDALUtilities.GDALMetadataDomain.IMAGESTRUCTURE); + if (imageStMap != null && imageStMap.size() > 0) + gdalDomainMetadataMap.put(GDALUtilities.GDALMetadataDomain.IMAGESTRUCTURE, + imageStMap); + + // // + // + // Initializing member if needed + // + // // + if (initializationRequired) + setMembers(dataset); + setGeoreferencingInfo(dataset); + // clean up data set in order to avoid keeping them around for a lot + // of time. + } finally { + if (driver != null) { + try { // Closing the driver - driver.delete(); - }catch (Throwable e) { - if(LOGGER.isLoggable(Level.FINEST)) - LOGGER.log(Level.FINEST,e.getLocalizedMessage(),e); - } - } - } - + driver.delete(); + } catch (Throwable e) { + if (LOGGER.isLoggable(Level.FINEST)) + LOGGER.log(Level.FINEST, e.getLocalizedMessage(), e); + } + } + if (initializationRequired) { + if (dataset != null) + try { + // Closing the dataset + GDALUtilities.closeDataSet(dataset); + } catch (Throwable e) { + if (LOGGER.isLoggable(Level.FINEST)) + LOGGER.log(Level.FINEST, e.getLocalizedMessage(), e); + } + } + + } + } /** diff --git a/plugin/gdal/gdalecwjp2/src/test/java/it/geosolutions/imageio/plugins/jp2ecw/JP2KReadTest.java b/plugin/gdal/gdalecwjp2/src/test/java/it/geosolutions/imageio/plugins/jp2ecw/JP2KReadTest.java index 38c827648..a901fc915 100644 --- a/plugin/gdal/gdalecwjp2/src/test/java/it/geosolutions/imageio/plugins/jp2ecw/JP2KReadTest.java +++ b/plugin/gdal/gdalecwjp2/src/test/java/it/geosolutions/imageio/plugins/jp2ecw/JP2KReadTest.java @@ -52,26 +52,21 @@ public class JP2KReadTest extends AbstractGDALTest { private final static boolean isJP2ECWAvailable; static { - if (isGDALAvailable){ - gdal.AllRegister(); - final String versionInfo = gdal.VersionInfo("RELEASE_NAME"); - if (versionInfo.contains("1.9.") && System.getProperty("os.name").toLowerCase().startsWith("linux")) { - // JP2 ECW crashes on Linux with > 1.8: http://trac.osgeo.org/gdal/ticket/4201 - isJP2ECWAvailable = false; - } else { - final Driver driverkak = gdal.GetDriverByName("JP2KAK"); - final Driver drivermrsid = gdal.GetDriverByName("JP2MrSID"); - if (driverkak != null || drivermrsid != null) { - final StringBuilder skipDriver = new StringBuilder(""); - if (driverkak != null) - skipDriver.append("JP2KAK "); - if (drivermrsid != null) - skipDriver.append("JP2MrSID"); - gdal.SetConfigOption("GDAL_SKIP", skipDriver.toString()); - gdal.AllRegister(); - } - isJP2ECWAvailable = GDALUtilities.isDriverAvailable("JP2ECW"); - } + if (isGDALAvailable) { + gdal.AllRegister(); + final Driver driverkak = gdal.GetDriverByName("JP2KAK"); + final Driver drivermrsid = gdal.GetDriverByName("JP2MrSID"); + if (driverkak != null || drivermrsid != null) { + final StringBuilder skipDriver = new StringBuilder(""); + if (driverkak != null) + skipDriver.append("JP2KAK "); + if (drivermrsid != null) + skipDriver.append("JP2MrSID"); + gdal.SetConfigOption("GDAL_SKIP", skipDriver.toString()); + gdal.AllRegister(); + } + isJP2ECWAvailable = GDALUtilities.isDriverAvailable("JP2ECW"); + // } } else { isJP2ECWAvailable = false; } @@ -225,9 +220,9 @@ public void testJaiOperations() throws IOException { @Before public void setUp() throws Exception { - super.setUp(); - JAI.getDefaultInstance().getTileCache().setMemoryCapacity(64 * 1024 * 1024); - JAI.getDefaultInstance().getTileCache().setMemoryThreshold(1.0f); - } + super.setUp(); + JAI.getDefaultInstance().getTileCache().setMemoryCapacity(64 * 1024 * 1024); + JAI.getDefaultInstance().getTileCache().setMemoryThreshold(1.0f); + } }