[hibernate-commits] Hibernate SVN: r10709 - branches/Branch_3_2/Hibernate3/src/org/hibernate/cfg

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Fri Nov 3 10:33:10 EST 2006


Author: steve.ebersole at jboss.com
Date: 2006-11-03 10:33:09 -0500 (Fri, 03 Nov 2006)
New Revision: 10709

Modified:
   branches/Branch_3_2/Hibernate3/src/org/hibernate/cfg/Configuration.java
Log:
HHH-2108 : fixed cacheable files

Modified: branches/Branch_3_2/Hibernate3/src/org/hibernate/cfg/Configuration.java
===================================================================
--- branches/Branch_3_2/Hibernate3/src/org/hibernate/cfg/Configuration.java	2006-11-03 15:32:55 UTC (rev 10708)
+++ branches/Branch_3_2/Hibernate3/src/org/hibernate/cfg/Configuration.java	2006-11-03 15:33:09 UTC (rev 10709)
@@ -309,57 +309,83 @@
 	}
 
 	/**
-	 * 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 cacheable 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 file exists and is newer than the incoming xml file, the cached
+	 * file will be read directly. Otherwise xmlFile is read (and cached in
+	 * serializable form for use next time) and used.
+	 *
+	 * @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 );
 				}
 			}
 
-			// 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();
+				doc = xmlHelper.createSAXReader( xmlFile.getAbsolutePath(), errors, entityResolver ).read( xmlFile );
+				if ( errors.size() != 0 ) {
+					throw new MappingException( "invalid mapping", (Throwable) errors.get( 0 ) );
+				}
+
 				try {
-					log.debug( "Writing cache file for: " + xmlFile + " to: " + lazyfile );
-					SerializationHelper.serialize( (Serializable) doc, new FileOutputStream( lazyfile ) );
+					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: " + lazyfile, e );
+				catch ( SerializationException e ) {
+					log.warn( "Could not write cached file: " + cachedFile, e );
 				}
-			} else {
-				throw new MappingNotFoundException("file", xmlFile.toString());
 			}
 
-			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 ( MappingException 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 ) );
 	}




More information about the hibernate-commits mailing list