Author: steve.ebersole(a)jboss.com
Date: 2006-11-03 10:37:47 -0500 (Fri, 03 Nov 2006)
New Revision: 10711
Modified:
trunk/Hibernate3/src/org/hibernate/cfg/Configuration.java
Log:
HHH-2108 : fixed cacheable files
Modified: trunk/Hibernate3/src/org/hibernate/cfg/Configuration.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/cfg/Configuration.java 2006-11-03 15:35:48 UTC (rev
10710)
+++ trunk/Hibernate3/src/org/hibernate/cfg/Configuration.java 2006-11-03 15:37:47 UTC (rev
10711)
@@ -309,57 +309,98 @@
}
/**
- * If a cached <tt>xmlFile + ".bin"</tt> exists and is newer than
<tt>xmlFile</tt> the
- * <tt>".bin"</tt> file will be read directly. Otherwise xmlFile
is read and then
- * serialized to <tt>xmlFile + ".bin"</tt> for use the next time.
+ * Add a cached mapping file. A cached file is a serialized representation
+ * of the DOM structure of a particular mapping. It is saved from a previous
+ * call as a file with the name <tt>xmlFile + ".bin"</tt> where
xmlFile is
+ * the name of the original mapping file.
+ * </p>
+ * If a cached <tt>xmlFile + ".bin"</tt> exists and is newer than
+ * <tt>xmlFile</tt> the <tt>".bin"</tt> file will be
read directly. Otherwise
+ * xmlFile is read and then serialized to <tt>xmlFile +
".bin"</tt> for use
+ * the next time.
+ *
+ * @param xmlFile The cacheable mapping file to be added.
+ * @return this (for method chaining purposes)
+ * @throws MappingException Indicates problems reading the cached file or processing
+ * the non-cached file.
*/
public Configuration addCacheableFile(File xmlFile) throws MappingException {
try {
- File lazyfile = new File( xmlFile.getAbsolutePath() + ".bin" );
+ File cachedFile = new File( xmlFile.getAbsolutePath() + ".bin" );
org.dom4j.Document doc = null;
- List errors = new ArrayList();
final boolean useCachedFile = xmlFile.exists() &&
- lazyfile.exists() &&
- xmlFile.lastModified() < lazyfile.lastModified();
+ cachedFile.exists() &&
+ xmlFile.lastModified() < cachedFile.lastModified();
if ( useCachedFile ) {
try {
- log.info( "Reading mappings from cache file: " + lazyfile );
- doc = (org.dom4j.Document) SerializationHelper.deserialize( new FileInputStream(
lazyfile ) );
+ log.info( "Reading mappings from cache file: " + cachedFile );
+ doc = ( org.dom4j.Document ) SerializationHelper.deserialize( new FileInputStream(
cachedFile ) );
}
- catch (SerializationException e) {
- log.warn( "Could not deserialize cache file: " + lazyfile.getPath(), e );
+ catch ( SerializationException e ) {
+ log.warn( "Could not deserialize cache file: " + cachedFile.getPath(), e
);
}
+ catch ( FileNotFoundException e ) {
+ log.warn( "I/O reported cached file could not be found : " +
cachedFile.getPath(), e );
+ }
}
- // If deserialization failed or cached file does not exist
- if ( doc == null && xmlFile.exists()) {
+ // if doc is null, then for whatever reason, the cached file cannot be used...
+ if ( doc == null ) {
+ if ( !xmlFile.exists() ) {
+ throw new MappingNotFoundException( "file", xmlFile.toString() );
+ }
+
log.info( "Reading mappings from file: " + xmlFile );
- doc = xmlHelper.createSAXReader( xmlFile.getAbsolutePath(), errors, entityResolver )
- .read( xmlFile );
+ List errors = new ArrayList();
try {
- log.debug( "Writing cache file for: " + xmlFile + " to: " +
lazyfile );
- SerializationHelper.serialize( (Serializable) doc, new FileOutputStream( lazyfile )
);
+ doc = xmlHelper.createSAXReader( xmlFile.getAbsolutePath(), errors, entityResolver
).read( xmlFile );
+ if ( errors.size() != 0 ) {
+ throw new MappingException( "invalid mapping", ( Throwable ) errors.get(
0 ) );
+ }
}
- catch (SerializationException e) {
- log.warn( "Could not write cached file: " + lazyfile, e );
+ catch( DocumentException e){
+ throw new MappingException( "invalid mapping", e );
}
- } else {
- throw new MappingNotFoundException("file", xmlFile.toString());
+
+ try {
+ log.debug( "Writing cache file for: " + xmlFile + " to: " +
cachedFile );
+ SerializationHelper.serialize( ( Serializable ) doc, new FileOutputStream(
cachedFile ) );
+ }
+ catch ( SerializationException e ) {
+ log.warn( "Could not write cached file: " + cachedFile, e );
+ }
+ catch ( FileNotFoundException e ) {
+ log.warn( "I/O reported error writing cached file : " +
cachedFile.getPath(), e );
+ }
}
- if ( errors.size() != 0 ) {
- throw new MappingException( "invalid mapping", (Throwable) errors.get( 0 )
);
- }
add( doc );
return this;
+
}
- catch (Exception e) {
- throw new InvalidMappingException("file", xmlFile.toString(), e);
+ catch ( InvalidMappingException e ) {
+ throw e;
}
+ catch ( MappingNotFoundException e ) {
+ throw e;
+ }
+ catch ( Exception e ) {
+ throw new InvalidMappingException( "file", xmlFile.toString(), e );
+ }
}
+ /**
+ * Add a cacheable mapping file.
+ *
+ * @param xmlFile The name of the file to be added. This must be in a form
+ * useable to simply construct a {@link java.io.File} instance.
+ * @return this (for method chaining purposes)
+ * @throws MappingException Indicates problems reading the cached file or processing
+ * the non-cached file.
+ * @see #addCacheableFile(java.io.File)
+ */
public Configuration addCacheableFile(String xmlFile) throws MappingException {
return addCacheableFile( new File( xmlFile ) );
}
Show replies by date