From 6ace838d61f649cb51ef44430890b426b22496a7 Mon Sep 17 00:00:00 2001 From: Holger Date: Thu, 29 Oct 2015 22:59:40 +0100 Subject: [PATCH] HHH-10181: CacheableFileXmlSource.doBind uses obsolete .bin file --- .../jaxb/internal/CacheableFileXmlSource.java | 24 ++++++++++++------- .../hibernate/internal/CoreMessageLogger.java | 4 ++++ .../hibernate/test/cfg/CacheableFileTest.java | 7 ++++++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/jaxb/internal/CacheableFileXmlSource.java b/hibernate-core/src/main/java/org/hibernate/boot/jaxb/internal/CacheableFileXmlSource.java index f66097f845..2d5591510f 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/jaxb/internal/CacheableFileXmlSource.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/jaxb/internal/CacheableFileXmlSource.java @@ -32,6 +32,7 @@ public class CacheableFileXmlSource extends XmlSource { private final File xmlFile; private final File serFile; private final boolean strict; + private final boolean serFileObsolete; public CacheableFileXmlSource(Origin origin, File xmlFile, boolean strict) { super( origin ); @@ -40,6 +41,8 @@ public class CacheableFileXmlSource extends XmlSource { this.serFile = determineCachedFile( xmlFile ); + this.serFileObsolete = xmlFile.exists() && serFile.exists() && xmlFile.lastModified() > serFile.lastModified(); + if ( strict ) { if ( !serFile.exists() ) { throw new MappingException( @@ -47,7 +50,7 @@ public class CacheableFileXmlSource extends XmlSource { origin ); } - if ( xmlFile.exists() && xmlFile.lastModified() > serFile.lastModified() ) { + if ( serFileObsolete ) { throw new MappingException( String.format( "Cached file [%s] could not be used as the mapping file is newer", origin.getName() ), origin @@ -83,14 +86,19 @@ public class CacheableFileXmlSource extends XmlSource { } } else { - try { - return readSerFile(); + if ( !serFileObsolete ) { + try { + return readSerFile(); + } + catch ( SerializationException e ) { + log.unableToDeserializeCache( serFile.getName(), e ); + } + catch ( FileNotFoundException e ) { + log.cachedFileNotFound( serFile.getName(), e ); + } } - catch ( SerializationException e ) { - log.unableToDeserializeCache( serFile.getName(), e ); - } - catch ( FileNotFoundException e ) { - log.cachedFileNotFound( serFile.getName(), e ); + else { + log.cachedFileObsolete( serFile ); } log.readingMappingsFromFile( xmlFile.getPath() ); diff --git a/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java b/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java index 782c3bcda0..01aab257df 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java @@ -1747,4 +1747,8 @@ public interface CoreMessageLogger extends BasicLogger { @Message(value = "Hikari properties were encountered, but the Hikari ConnectionProvider was not found on the classpath; these properties are going to be ignored.", id = 472) void hikariProviderClassNotFound(); + + @LogMessage(level = INFO) + @Message(value = "Omitting cached file [%s] as the mapping file is newer", id = 473) + void cachedFileObsolete(File cachedFile); } diff --git a/hibernate-core/src/test/java/org/hibernate/test/cfg/CacheableFileTest.java b/hibernate-core/src/test/java/org/hibernate/test/cfg/CacheableFileTest.java index 84c614c7f0..9d6ec0c831 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/cfg/CacheableFileTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/cfg/CacheableFileTest.java @@ -59,5 +59,12 @@ public class CacheableFileTest extends BaseUnitTestCase { assertTrue( mappingBinFile.exists() ); new Configuration().addCacheableFileStrictly( mappingFile ); + + // make mappingBinFile obsolete by declaring it a minute older than mappingFile + mappingBinFile.setLastModified( mappingFile.lastModified() - 60000L ); + + new Configuration().addCacheableFile( mappingFile ); + assertTrue( mappingBinFile.exists() ); + assertTrue( "mappingFile should have been recreated.", mappingBinFile.lastModified() >= mappingFile.lastModified()); } }