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

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Nov 6 12:50:38 EST 2006


Author: steve.ebersole at jboss.com
Date: 2006-11-06 12:50:37 -0500 (Mon, 06 Nov 2006)
New Revision: 10730

Modified:
   branches/Branch_3_2/Hibernate3/src/org/hibernate/cfg/Configuration.java
Log:
Configuration : javadocs and general code cleanup

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-06 17:43:53 UTC (rev 10729)
+++ branches/Branch_3_2/Hibernate3/src/org/hibernate/cfg/Configuration.java	2006-11-06 17:50:37 UTC (rev 10730)
@@ -192,7 +192,9 @@
 	}
 
 	/**
-	 * Iterate the class mappings
+	 * Iterate the entity mappings
+	 *
+	 * @return Iterator of the entity mappings currently contained in the configuration.
 	 */
 	public Iterator getClassMappings() {
 		return classes.values().iterator();
@@ -200,6 +202,8 @@
 
 	/**
 	 * Iterate the collection mappings
+	 *
+	 * @return Iterator of the collection mappings currently contained in the configuration.
 	 */
 	public Iterator getCollectionMappings() {
 		return collections.values().iterator();
@@ -207,23 +211,28 @@
 
 	/**
 	 * Iterate the table mappings
+	 *
+	 * @return Iterator of the table mappings currently contained in the configuration.
 	 */
 	public Iterator getTableMappings() {
 		return tables.values().iterator();
 	}
 
 	/**
-	 * Get the mapping for a particular class
+	 * Get the mapping for a particular entity
+	 *
+	 * @param entityName An entity name.
+	 * @return the entity mapping information
 	 */
-	public PersistentClass getClassMapping(String persistentClass) {
-		return (PersistentClass) classes.get( persistentClass );
+	public PersistentClass getClassMapping(String entityName) {
+		return (PersistentClass) classes.get( entityName );
 	}
 
 	/**
 	 * Get the mapping for a particular collection role
 	 *
 	 * @param role a collection role
-	 * @return Collection
+	 * @return The collection mapping information
 	 */
 	public Collection getCollectionMapping(String role) {
 		return (Collection) collections.get( role );
@@ -269,43 +278,46 @@
 	 * Read mappings from a particular XML file
 	 *
 	 * @param xmlFile a path to a file
+	 * @return this (for method chaining purposes)
+	 * @throws org.hibernate.MappingException Indicates inability to locate or parse
+	 * the specified mapping file.
+	 * @see #addFile(java.io.File)
 	 */
 	public Configuration addFile(String xmlFile) throws MappingException {
-		log.info( "Reading mappings from file: " + xmlFile );
-		try {
-			File file = new File( xmlFile );
-			if(file.exists()) {
-				List errors = new ArrayList();
-				org.dom4j.Document doc = xmlHelper.createSAXReader( xmlFile, errors, entityResolver )
-				.read( file );
-				if ( errors.size() != 0 ) {
-					throw new MappingException( "invalid mapping", (Throwable) errors.get( 0 ) );
-				}
-				add( doc );
-				return this;
-			} else {
-				throw new MappingNotFoundException("file", file.toString());
-			}
-		}
-		catch (Exception e) {
-			throw new InvalidMappingException("file", xmlFile, e);
-		}
+		return addFile( new File( xmlFile ) );
 	}
 
 	/**
 	 * Read mappings from a particular XML file
 	 *
 	 * @param xmlFile a path to a file
+	 * @return this (for method chaining purposes)
+	 * @throws org.hibernate.MappingException Indicates inability to locate or parse
+	 * the specified mapping file.
 	 */
 	public Configuration addFile(File xmlFile) throws MappingException {
 		log.info( "Reading mappings from file: " + xmlFile.getPath() );
+		if ( !xmlFile.exists() ) {
+			throw new MappingNotFoundException( "file", xmlFile.toString() );
+		}
 		try {
-			addInputStream( new FileInputStream( xmlFile ) );
+			List errors = new ArrayList();
+			org.dom4j.Document doc = xmlHelper.createSAXReader( xmlFile.toString(), errors, entityResolver ).read( xmlFile );
+			if ( errors.size() != 0 ) {
+				throw new InvalidMappingException( "file", xmlFile.toString(), ( Throwable ) errors.get( 0 ) );
+			}
+			add( doc );
+			return this;
 		}
-		catch (Exception e) {
-			throw new InvalidMappingException( "file", xmlFile.getPath(),e );
+		catch ( InvalidMappingException e ) {
+			throw e;
 		}
-		return this;
+		catch  ( MappingNotFoundException e ) {
+			throw e;
+		}
+		catch ( Exception e ) {
+			throw new InvalidMappingException( "file", xmlFile.toString(), e );
+		}
 	}
 
 	/**
@@ -410,6 +422,9 @@
 	 * Read mappings from a <tt>String</tt>
 	 *
 	 * @param xml an XML string
+	 * @return this (for method chaining purposes)
+	 * @throws org.hibernate.MappingException Indicates problems parsing the
+	 * given XML string
 	 */
 	public Configuration addXML(String xml) throws MappingException {
 		if ( log.isDebugEnabled() ) {
@@ -433,17 +448,23 @@
 	/**
 	 * Read mappings from a <tt>URL</tt>
 	 *
-	 * @param url
+	 * @param url The url for the mapping document to be read.
+	 * @return this (for method chaining purposes)
+	 * @throws MappingException Indicates problems reading the URL or processing
+	 * the mapping document.
 	 */
 	public Configuration addURL(URL url) throws MappingException {
 		if ( log.isDebugEnabled() ) {
-			log.debug( "Reading mapping document from URL:" + url );
+			log.debug( "Reading mapping document from URL:" + url.toExternalForm() );
 		}
 		try {
 			addInputStream( url.openStream() );
 		}
+		catch ( InvalidMappingException e ) {
+			throw new InvalidMappingException( "URL", url.toExternalForm(), e.getCause() );
+		}
 		catch (Exception e) {
-			throw new InvalidMappingException( "URL", ""+url, e );
+			throw new InvalidMappingException( "URL", url.toExternalForm(), e );
 		}
 		return this;
 	}
@@ -451,7 +472,10 @@
 	/**
 	 * Read mappings from a DOM <tt>Document</tt>
 	 *
-	 * @param doc a DOM document
+	 * @param doc The DOM document
+	 * @return this (for method chaining purposes)
+	 * @throws MappingException Indicates problems reading the DOM or processing
+	 * the mapping document.
 	 */
 	public Configuration addDocument(Document doc) throws MappingException {
 		if ( log.isDebugEnabled() ) {
@@ -461,39 +485,13 @@
 		return this;
 	}
 
-	protected void add(org.dom4j.Document doc) throws MappingException {
-		HbmBinder.bindRoot( doc, createMappings(), CollectionHelper.EMPTY_MAP );
-	}
-
 	/**
-	 * Create a new <tt>Mappings</tt> to add class and collection
-	 * mappings to.
-	 */
-	public Mappings createMappings() {
-		return new Mappings(
-				classes,
-				collections,
-				tables,
-				namedQueries,
-				namedSqlQueries,
-				sqlResultSetMappings,
-				imports,
-				secondPasses,
-				propertyReferences,
-				namingStrategy,
-				typeDefs,
-				filterDefinitions,
-				extendsQueue,
-				auxiliaryDatabaseObjects,
-				tableNameBinding,
-				columnNameBindingPerTable
-			);
-	}
-
-	/**
-	 * Read mappings from an <tt>InputStream</tt>
+	 * Read mappings from an {@link java.io.InputStream}.
 	 *
-	 * @param xmlInputStream an <tt>InputStream</tt> containing XML
+	 * @param xmlInputStream The input stream containing a DOM.
+	 * @return this (for method chaining purposes)
+	 * @throws MappingException Indicates problems reading the stream, or
+	 * processing the contained mapping document.
 	 */
 	public Configuration addInputStream(InputStream xmlInputStream) throws MappingException {
 		try {
@@ -501,7 +499,7 @@
 			org.dom4j.Document doc = xmlHelper.createSAXReader( "XML InputStream", errors, entityResolver )
 					.read( new InputSource( xmlInputStream ) );
 			if ( errors.size() != 0 ) {
-				throw new MappingException( "invalid mapping", (Throwable) errors.get( 0 ) );
+				throw new InvalidMappingException( "invalid mapping", null, (Throwable) errors.get( 0 ) );
 			}
 			add( doc );
 			return this;
@@ -520,85 +518,88 @@
 	}
 
 	/**
-	 * Read mappings from an application resource
+	 * Read mappings as a application resource (i.e. classpath lookup).
 	 *
-	 * @param path		a resource
-	 * @param classLoader a <tt>ClassLoader</tt> to use
+	 * @param resourceName The resource name
+	 * @param classLoader The class loader to use.
+	 * @return this (for method chaining purposes)
+	 * @throws MappingException Indicates problems locating the resource or
+	 * processing the contained mapping document.
 	 */
-	public Configuration addResource(String path, ClassLoader classLoader) throws MappingException {
-		log.info( "Reading mappings from resource: " + path );
-		InputStream rsrc = classLoader.getResourceAsStream( path );
+	public Configuration addResource(String resourceName, ClassLoader classLoader) throws MappingException {
+		log.info( "Reading mappings from resource: " + resourceName );
+		InputStream rsrc = classLoader.getResourceAsStream( resourceName );
 		if ( rsrc == null ) {
-			throw new MappingNotFoundException( "resource", path );
+			throw new MappingNotFoundException( "resource", resourceName );
 		}
 		try {
 			return addInputStream( rsrc );
 		}
 		catch (MappingException me) {
-			throw new InvalidMappingException( "resource", path, me );
+			throw new InvalidMappingException( "resource", resourceName, me );
 		}
 	}
 
 	/**
-	 * Read mappings from an application resource trying different classloaders.
-	 * This method will try to load the resource first from the thread context
-	 * classloader and then from the classloader that loaded Hibernate.
+	 * Read mappings as a application resourceName (i.e. classpath lookup)
+	 * trying different classloaders.
+	 *
+	 * @param resourceName The resource name
+	 * @return this (for method chaining purposes)
+	 * @throws MappingException Indicates problems locating the resource or
+	 * processing the contained mapping document.
 	 */
-	public Configuration addResource(String path) throws MappingException {
-		log.info( "Reading mappings from resource: " + path );
+	public Configuration addResource(String resourceName) throws MappingException {
+		log.info( "Reading mappings from resource : " + resourceName );
 		ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
 		InputStream rsrc = null;
 		if (contextClassLoader!=null) {
-			rsrc = contextClassLoader.getResourceAsStream( path );
+			rsrc = contextClassLoader.getResourceAsStream( resourceName );
 		}
 		if ( rsrc == null ) {
-			rsrc = Environment.class.getClassLoader().getResourceAsStream( path );
+			rsrc = Environment.class.getClassLoader().getResourceAsStream( resourceName );
 		}
 		if ( rsrc == null ) {
-			throw new MappingNotFoundException( "resource", path );
+			throw new MappingNotFoundException( "resource", resourceName );
 		}
 		try {
 			return addInputStream( rsrc );
 		}
 		catch (MappingException me) {
-			throw new InvalidMappingException( "resource", path, me );
+			throw new InvalidMappingException( "resource", resourceName, me );
 		}
 	}
 
 	/**
-	 * Read a mapping from an application resource, using a convention.
-	 * The class <tt>foo.bar.Foo</tt> is mapped by the file <tt>foo/bar/Foo.hbm.xml</tt>.
+	 * Read a mapping as an application resouurce using the convention that a class
+	 * named <tt>foo.bar.Foo</tt> is mapped by a file <tt>foo/bar/Foo.hbm.xml</tt>
+	 * which can be resolved as a classpath resource.
 	 *
-	 * @param persistentClass the mapped class
+	 * @param persistentClass The mapped class
+	 * @return this (for method chaining purposes)
+	 * @throws MappingException Indicates problems locating the resource or
+	 * processing the contained mapping document.
 	 */
 	public Configuration addClass(Class persistentClass) throws MappingException {
-		String fileName = persistentClass.getName().replace( '.', '/' ) + ".hbm.xml";
-		log.info( "Reading mappings from resource: " + fileName );
-		InputStream rsrc = persistentClass.getClassLoader().getResourceAsStream( fileName );
-		if ( rsrc == null ) {
-			throw new MappingNotFoundException( "resource", fileName );
-		}
-		try {
-			return addInputStream( rsrc );
-		}
-		catch (MappingException me) {
-			throw new InvalidMappingException(
-					"resource", fileName, me );
-		}
+		String mappingResourceName = persistentClass.getName().replace( '.', '/' ) + ".hbm.xml";
+		log.info( "Reading mappings from resource: " + mappingResourceName );
+		return addResource( mappingResourceName, persistentClass.getClassLoader() );
 	}
 
 	/**
 	 * Read all mappings from a jar file
+	 * <p/>
+	 * Assumes that any file named <tt>*.hbm.xml</tt> is a mapping document.
 	 *
 	 * @param jar a jar file
+	 * @return this (for method chaining purposes)
+	 * @throws MappingException Indicates problems reading the jar file or
+	 * processing the contained mapping documents.
 	 */
 	public Configuration addJar(File jar) throws MappingException {
-
 		log.info( "Searching for mapping documents in jar: " + jar.getName() );
-
 		JarFile jarFile = null;
 		try {
-
 			try {
 				jarFile = new JarFile( jar );
 			}
@@ -606,14 +607,11 @@
 				throw new InvalidMappingException(
 						"Could not read mapping documents from jar: " + jar.getName(), "jar", jar.getName(),
 						ioe
-					);
+				);
 			}
-
 			Enumeration jarEntries = jarFile.entries();
 			while ( jarEntries.hasMoreElements() ) {
-
 				ZipEntry ze = (ZipEntry) jarEntries.nextElement();
-
 				if ( ze.getName().endsWith( ".hbm.xml" ) ) {
 					log.info( "Found mapping document in jar: " + ze.getName() );
 					try {
@@ -621,18 +619,16 @@
 					}
 					catch (Exception e) {
 						throw new InvalidMappingException(
-								"Could not read mapping documents from jar: " + jar.getName(), 
-								"jar", 
-								jar.getName(), 
+								"Could not read mapping documents from jar: " + jar.getName(),
+								"jar",
+								jar.getName(),
 								e
-							);
+						);
 					}
 				}
 			}
-
 		}
 		finally {
-
 			try {
 				if ( jarFile != null ) {
 					jarFile.close();
@@ -641,18 +637,20 @@
 			catch (IOException ioe) {
 				log.error("could not close jar", ioe);
 			}
-
 		}
 
 		return this;
-
 	}
 
 	/**
-	 * Read all mapping documents from a directory tree. Assume that any
-	 * file named <tt>*.hbm.xml</tt> is a mapping document.
+	 * Read all mapping documents from a directory tree.
+	 * <p/>
+	 * Assumes that any file named <tt>*.hbm.xml</tt> is a mapping document.
 	 *
-	 * @param dir a directory
+	 * @param dir The directory
+	 * @return this (for method chaining purposes)
+	 * @throws MappingException Indicates problems reading the jar file or
+	 * processing the contained mapping documents.
 	 */
 	public Configuration addDirectory(File dir) throws MappingException {
 		File[] files = dir.listFiles();
@@ -667,6 +665,36 @@
 		return this;
 	}
 
+	protected void add(org.dom4j.Document doc) throws MappingException {
+		HbmBinder.bindRoot( doc, createMappings(), CollectionHelper.EMPTY_MAP );
+	}
+
+	/**
+	 * Create a new <tt>Mappings</tt> to add class and collection
+	 * mappings to.
+	 */
+	public Mappings createMappings() {
+		return new Mappings(
+				classes,
+				collections,
+				tables,
+				namedQueries,
+				namedSqlQueries,
+				sqlResultSetMappings,
+				imports,
+				secondPasses,
+				propertyReferences,
+				namingStrategy,
+				typeDefs,
+				filterDefinitions,
+				extendsQueue,
+				auxiliaryDatabaseObjects,
+				tableNameBinding,
+				columnNameBindingPerTable
+			);
+	}
+
+
 	private Iterator iterateGenerators(Dialect dialect) throws MappingException {
 
 		TreeMap generators = new TreeMap();




More information about the hibernate-commits mailing list