From: Michael D. <mik...@us...> - 2005-02-21 14:27:45
|
Update of /cvsroot/nhibernate/nhibernate/src/NHibernate/Cfg In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21222/src/NHibernate/Cfg Modified Files: Configuration.cs Log Message: NH-207: readers clean up after themselves. Index: Configuration.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Cfg/Configuration.cs,v retrieving revision 1.29 retrieving revision 1.30 diff -C2 -d -r1.29 -r1.30 *** Configuration.cs 14 Feb 2005 03:29:05 -0000 1.29 --- Configuration.cs 21 Feb 2005 14:27:36 -0000 1.30 *************** *** 61,65 **** private static XmlNamespaceManager CfgNamespaceMgr; ! /// <summary></summary> protected void Reset() { --- 61,67 ---- private static XmlNamespaceManager CfgNamespaceMgr; ! /// <summary> ! /// Clear the internal state of the <see cref="Configuration"/> object. ! /// </summary> protected void Reset() { *************** *** 134,179 **** /// <summary> ! /// Adds the Mappings contained in the file specified. /// </summary> ! /// <param name="xmlFile">The name of the file (url or file system) that contains the Xml.</param> /// <returns>This Configuration object.</returns> ! public Configuration AddXmlFile( string xmlFile ) { ! log.Debug( "Mapping file: " + xmlFile ); try { ! AddXmlReader( new XmlTextReader( xmlFile ) ); } catch( Exception e ) { ! log.Error( "Could not configure datastore from file: " + xmlFile, e ); ! throw new MappingException( e ); } return this; } /// <summary> ! /// Adds the Mappings from a <c>String</c> /// </summary> ! /// <param name="xml">A string that contains the Mappings for the Xml</param> /// <returns>This Configuration object.</returns> ! public Configuration AddXmlString( string xml ) { ! if( log.IsDebugEnabled ) { ! log.Debug( "Mapping XML:\n" + xml ); } ! try { ! // make a StringReader for the string passed in - the StringReader ! // inherits from TextReader. We can use the XmlTextReader.ctor that ! // takes the TextReader to build from a string... ! AddXmlReader( new XmlTextReader( new StringReader( xml ) ) ); } ! catch( Exception e ) { ! log.Error( "Could not configure datastore from XML", e ); } - return this; } --- 136,264 ---- /// <summary> ! /// Create a new <c>Mappings</c> to add classes and collection mappings to /// </summary> ! /// <returns></returns> ! public Mappings CreateMappings() ! { ! return new Mappings( classes, collections, tables, namedQueries, imports, caches, secondPasses ); ! } ! ! /// <summary> ! /// Takes the validated XmlDocument and has the Binder do its work of ! /// creating Mapping objects from the Mapping Xml. ! /// </summary> ! /// <param name="doc">The validated XmlDocument that contains the Mappings.</param> ! private void Add( XmlDocument doc ) ! { ! try ! { ! Binder.dialect = Dialect.Dialect.GetDialect( properties ); ! Binder.BindRoot( doc, CreateMappings() ); ! } ! catch( MappingException me ) ! { ! log.Error( "Could not compile the mapping document", me ); ! throw; ! } ! } ! ! /// <summary> ! /// Adds all of the Assembly's Resource files that end with "<c>hbm.xml</c>" ! /// </summary> ! /// <param name="assemblyName">The name of the Assembly to load.</param> /// <returns>This Configuration object.</returns> ! /// <remarks> ! /// The Assembly must be in the local bin, probing path, or GAC so that the ! /// Assembly can be loaded by name. If these conditions are not satisfied ! /// then your code should load the Assembly and call the override ! /// <see cref="AddAssembly(Assembly)"/> instead. ! /// </remarks> ! public Configuration AddAssembly( string assemblyName ) { ! log.Info( "searching for mapped documents in assembly: " + assemblyName ); ! ! Assembly assembly = null; try { ! assembly = Assembly.Load( assemblyName ); } catch( Exception e ) { ! log.Error( "Could not configure datastore from assembly", e ); ! throw new MappingException( "Could not add assembly named: " + assemblyName, e ); } + + return this.AddAssembly( assembly ); + } + + /// <summary> + /// Adds all of the Assembly's Resource files that end with "hbm.xml" + /// </summary> + /// <param name="assembly">The loaded Assembly.</param> + /// <returns>This Configuration object.</returns> + public Configuration AddAssembly( Assembly assembly ) + { + foreach( string fileName in assembly.GetManifestResourceNames() ) + { + if( fileName.EndsWith( ".hbm.xml" ) ) + { + log.Info( "Found mapping documents in assembly: " + fileName ); + Stream resourceStream = null; + try + { + resourceStream = assembly.GetManifestResourceStream( fileName ); + AddInputStream( resourceStream ); + } + catch( MappingException ) + { + throw; + } + catch( Exception e ) + { + log.Error( "Could not configure datastore from assembly", e ); + throw new MappingException( e ); + } + finally + { + if( resourceStream!=null ) + { + resourceStream.Close(); + } + + } + } + } + return this; } /// <summary> ! /// Adds the Class' Mapping by appending an ".hbm.xml" to the end of the Full Class Name ! /// and looking in the Class' Assembly. /// </summary> ! /// <param name="persistentClass">The Type to Map.</param> /// <returns>This Configuration object.</returns> ! /// <remarks> ! /// If the Mappings and Classes are defined in different Assemblies or don't follow ! /// the same naming convention then this can not be used. ! /// </remarks> ! public Configuration AddClass( System.Type persistentClass ) { ! string fileName = persistentClass.FullName + ".hbm.xml"; ! log.Info( "Mapping resource: " + fileName ); ! Stream rsrc = persistentClass.Assembly.GetManifestResourceStream( fileName ); ! if( rsrc==null ) { ! throw new MappingException( "Resource: " + fileName + " not found" ); } ! ! try { ! return AddInputStream( rsrc ); } ! finally { ! rsrc.Close(); } } *************** *** 185,188 **** --- 270,275 ---- public Configuration AddDocument( XmlDocument doc ) { + XmlNodeReader nodeReader = null; + if( log.IsDebugEnabled ) { *************** *** 191,195 **** try { ! AddXmlReader( new XmlNodeReader( doc ) ); } catch( Exception e ) --- 278,284 ---- try { ! nodeReader = new XmlNodeReader( doc ); ! AddXmlReader( nodeReader ); ! return this; } catch( Exception e ) *************** *** 198,242 **** throw new MappingException( e ); } ! return this; ! } ! ! /// <summary> ! /// Takes the validated XmlDocument and has the Binder do its work of ! /// creating Mapping objects from the Mapping Xml. ! /// </summary> ! /// <param name="doc">The validated XmlDocument that contains the Mappings.</param> ! private void Add( XmlDocument doc ) ! { ! try ! { ! Binder.dialect = Dialect.Dialect.GetDialect( properties ); ! Binder.BindRoot( doc, CreateMappings() ); ! } ! catch( MappingException me ) { ! log.Error( "Could not compile the mapping document", me ); ! throw; } } /// <summary> - /// Create a new <c>Mappings</c> to add classes and collection mappings to - /// </summary> - /// <returns></returns> - public Mappings CreateMappings() - { - return new Mappings( classes, collections, tables, namedQueries, imports, caches, secondPasses ); - } - - /// <summary> /// Adds the Xml Mappings from the Stream. /// </summary> /// <param name="xmlInputStream">The Stream to read Xml from.</param> /// <returns>This Configuration object.</returns> public Configuration AddInputStream( Stream xmlInputStream ) { try { ! AddXmlReader( new XmlTextReader( xmlInputStream ) ); return this; } --- 287,314 ---- throw new MappingException( e ); } ! finally { ! nodeReader.Close(); } } /// <summary> /// Adds the Xml Mappings from the Stream. /// </summary> /// <param name="xmlInputStream">The Stream to read Xml from.</param> /// <returns>This Configuration object.</returns> + /// <remarks> + /// The <see cref="Stream"/> passed in through the parameter <c>xmlInputStream</c> + /// is not <b>guaranteed</b> to be cleaned up by this method. It is the callers responsiblity to + /// ensure that the <c>xmlInputStream</c> is properly handled when this method + /// completes. + /// </remarks> public Configuration AddInputStream( Stream xmlInputStream ) { + XmlTextReader textReader = null; try { ! textReader = new XmlTextReader( xmlInputStream ); ! AddXmlReader( textReader ); return this; } *************** *** 250,272 **** throw new MappingException( e ); } ! } ! ! /// <summary> ! /// Adds the Mappings in the XmlReader after validating it against the ! /// nhibernate-mapping-2.0 schema. ! /// </summary> ! /// <param name="hbmReader">The XmlReader that contains the mapping.</param> ! /// <returns>This Configuration object.</returns> ! public Configuration AddXmlReader( XmlReader hbmReader ) ! { ! XmlValidatingReader validatingReader = new XmlValidatingReader( hbmReader ); ! validatingReader.ValidationType = ValidationType.Schema; ! validatingReader.Schemas.Add( mappingSchema ); ! ! XmlDocument hbmDocument = new XmlDocument(); ! hbmDocument.Load( validatingReader ); ! Add( hbmDocument ); ! ! return this; } --- 322,332 ---- throw new MappingException( e ); } ! finally ! { ! if( textReader!=null ) ! { ! textReader.Close(); ! } ! } } *************** *** 281,374 **** log.Info( "mapping resource: " + path ); Stream rsrc = assembly.GetManifestResourceStream( path ); ! if( rsrc == null ) { throw new MappingException( "Resource: " + path + " not found" ); } ! return AddInputStream( rsrc ); } /// <summary> ! /// Adds the Class' Mapping by appending an ".hbm.xml" to the end of the Full Class Name ! /// and looking in the Class' Assembly. /// </summary> ! /// <param name="persistentClass">The Type to Map.</param> /// <returns>This Configuration object.</returns> ! /// <remarks> ! /// If the Mappings and Classes are defined in different Assemblies or don't follow ! /// the same naming convention then this can not be used. ! /// </remarks> ! public Configuration AddClass( System.Type persistentClass ) { ! string fileName = persistentClass.FullName + ".hbm.xml"; ! log.Info( "Mapping resource: " + fileName ); ! Stream rsrc = persistentClass.Assembly.GetManifestResourceStream( fileName ); ! if( rsrc == null ) { ! throw new MappingException( "Resource: " + fileName + " not found" ); } ! return AddInputStream( rsrc ); } /// <summary> ! /// Adds all of the Assembly's Resource files that end with "hbm.xml" /// </summary> ! /// <param name="assemblyName">The name of the Assembly to load.</param> /// <returns>This Configuration object.</returns> ! /// <remarks> ! /// The Assembly must be in the local bin, probing path, or GAC so that the ! /// Assembly can be loaded by name. If these conditions are not satisfied ! /// then your code should load the Assembly and call the override <see cref="AddAssembly(Assembly)"/> ! /// instead. ! /// </remarks> ! public Configuration AddAssembly( string assemblyName ) { - log.Info( "searching for mapped documents in assembly: " + assemblyName ); ! Assembly assembly = null; ! try { ! assembly = Assembly.Load( assemblyName ); } ! catch( Exception e ) { ! log.Error( "Could not configure datastore from assembly", e ); ! throw new MappingException( "Could not add assembly named: " + assemblyName, e ); } - - return this.AddAssembly( assembly ); } /// <summary> ! /// Adds all of the Assembly's Resource files that end with "hbm.xml" /// </summary> ! /// <param name="assembly">The loaded Assembly.</param> /// <returns>This Configuration object.</returns> ! public Configuration AddAssembly( Assembly assembly ) { ! foreach( string fileName in assembly.GetManifestResourceNames() ) { ! if( fileName.EndsWith( ".hbm.xml" ) ) ! { ! log.Info( "Found mapping documents in assembly: " + fileName ); ! try ! { ! AddInputStream( assembly.GetManifestResourceStream( fileName ) ); ! } ! catch( MappingException ) ! { ! throw; ! } ! catch( Exception e ) ! { ! log.Error( "Could not configure datastore from assembly", e ); ! throw new MappingException( e ); ! } ! } } - return this; } - private ICollection CollectionGenerators( Dialect.Dialect dialect ) { --- 341,447 ---- log.Info( "mapping resource: " + path ); Stream rsrc = assembly.GetManifestResourceStream( path ); ! if( rsrc==null ) { throw new MappingException( "Resource: " + path + " not found" ); } ! ! try ! { ! return AddInputStream( rsrc ); ! } ! finally ! { ! if( rsrc!=null ) ! { ! rsrc.Close(); ! } ! } } /// <summary> ! /// Adds the Mappings contained in the file specified. /// </summary> ! /// <param name="xmlFile">The name of the file (url or file system) that contains the Xml.</param> /// <returns>This Configuration object.</returns> ! public Configuration AddXmlFile( string xmlFile ) { ! log.Debug( "Mapping file: " + xmlFile ); ! XmlTextReader textReader = null; ! try { ! textReader = new XmlTextReader( xmlFile ); ! AddXmlReader( textReader ); } ! catch( Exception e ) ! { ! log.Error( "Could not configure datastore from file: " + xmlFile, e ); ! throw new MappingException( e ); ! } ! finally ! { ! if( textReader!=null ) ! { ! textReader.Close(); ! } ! } ! return this; } /// <summary> ! /// Adds the Mappings in the XmlReader after validating it against the ! /// nhibernate-mapping-2.0 schema. /// </summary> ! /// <param name="hbmReader">The XmlReader that contains the mapping.</param> /// <returns>This Configuration object.</returns> ! public Configuration AddXmlReader( XmlReader hbmReader ) { ! XmlValidatingReader validatingReader = null; ! try { ! validatingReader = new XmlValidatingReader( hbmReader ); ! validatingReader.ValidationType = ValidationType.Schema; ! validatingReader.Schemas.Add( mappingSchema ); ! ! XmlDocument hbmDocument = new XmlDocument(); ! hbmDocument.Load( validatingReader ); ! Add( hbmDocument ); ! ! return this; } ! finally { ! if( validatingReader!=null ) ! { ! validatingReader.Close(); ! } } } /// <summary> ! /// Adds the Mappings from a <c>String</c> /// </summary> ! /// <param name="xml">A string that contains the Mappings for the Xml</param> /// <returns>This Configuration object.</returns> ! public Configuration AddXmlString( string xml ) { ! if( log.IsDebugEnabled ) { ! log.Debug( "Mapping XML:\n" + xml ); ! } ! try ! { ! // make a StringReader for the string passed in - the StringReader ! // inherits from TextReader. We can use the XmlTextReader.ctor that ! // takes the TextReader to build from a string... ! AddXmlReader( new XmlTextReader( new StringReader( xml ) ) ); ! } ! catch( Exception e ) ! { ! log.Error( "Could not configure datastore from XML", e ); } return this; } private ICollection CollectionGenerators( Dialect.Dialect dialect ) { *************** *** 602,606 **** } ! /// <summary></summary> public IInterceptor Interceptor { --- 675,682 ---- } ! /// <summary> ! /// Gets or sets the <see cref="IInterceptor"/> to use. ! /// </summary> ! /// <value>The <see cref="IInterceptor"/> to use.</value> public IInterceptor Interceptor { *************** *** 609,613 **** } ! /// <summary></summary> public IDictionary Properties { --- 685,696 ---- } ! /// <summary> ! /// Gets or sets the <see cref="IDictionary"/> that contains the configuration ! /// Properties and their values. ! /// </summary> ! /// <value> ! /// The <see cref="IDictionary"/> that contains the configuration ! /// Properties and their values. ! /// </value> public IDictionary Properties { *************** *** 617,624 **** /// <summary> ! /// /// </summary> ! /// <param name="properties"></param> ! /// <returns></returns> public Configuration AddProperties( IDictionary properties ) { --- 700,711 ---- /// <summary> ! /// Adds an <see cref="IDictionary"/> of configuration properties. The ! /// Key is the name of the Property and the Value is the <see cref="String"/> ! /// value of the Property. /// </summary> ! /// <param name="properties">An <see cref="IDictionary"/> of configuration properties.</param> ! /// <returns> ! /// This <see cref="Configuration"/> object. ! /// </returns> public Configuration AddProperties( IDictionary properties ) { *************** *** 631,639 **** /// <summary> ! /// /// </summary> ! /// <param name="name"></param> ! /// <param name="value"></param> ! /// <returns></returns> public Configuration SetProperty( string name, string value ) { --- 718,728 ---- /// <summary> ! /// Sets the value of the configuration property. /// </summary> ! /// <param name="name">The name of the property.</param> ! /// <param name="value">The value of the property.</param> ! /// <returns> ! /// This <see cref="Configuration"/> object. ! /// </returns> public Configuration SetProperty( string name, string value ) { *************** *** 643,650 **** /// <summary> ! /// /// </summary> ! /// <param name="name"></param> ! /// <returns></returns> public string GetProperty( string name ) { --- 732,739 ---- /// <summary> ! /// Gets the value of the configuration property. /// </summary> ! /// <param name="name">The name of the property.</param> ! /// <returns>The configured value of the property, or <c>null</c> if the property was not specified.</returns> public string GetProperty( string name ) { *************** *** 689,694 **** public Configuration Configure( string resource ) { ! XmlTextReader reader = new XmlTextReader( resource ); ! return Configure( reader ); } --- 778,795 ---- public Configuration Configure( string resource ) { ! XmlTextReader reader = null; ! try ! { ! reader = new XmlTextReader( resource ); ! return Configure( reader ); ! } ! finally ! { ! if( reader!=null ) ! { ! reader.Close(); ! } ! } ! } *************** *** 709,720 **** } ! Stream stream = assembly.GetManifestResourceStream( resourceName ); ! if( stream == null ) { ! // resource does not exist - throw appropriate exception ! throw new HibernateException( "A ManifestResourceStream could not be created for the resource " + resourceName + " in Assembly " + assembly.FullName ); ! } ! return Configure( new XmlTextReader( stream ) ); } --- 810,832 ---- } ! Stream stream = null; ! try { ! stream = assembly.GetManifestResourceStream( resourceName ); ! if( stream==null ) ! { ! // resource does not exist - throw appropriate exception ! throw new HibernateException( "A ManifestResourceStream could not be created for the resource " + resourceName + " in Assembly " + assembly.FullName ); ! } ! return Configure( new XmlTextReader( stream ) ); ! } ! finally ! { ! if( stream!=null ) ! { ! stream.Close(); ! } ! } } *************** *** 757,760 **** --- 869,879 ---- throw new HibernateException( "problem parsing configuration : " + e ); } + finally + { + if( validatingReader!=null ) + { + validatingReader.Close(); + } + } XmlNode sfNode = doc.DocumentElement.SelectSingleNode( CfgNamespacePrefix + ":session-factory", CfgNamespaceMgr ); *************** *** 799,803 **** log.Debug( "properties: " + properties ); - validatingReader.Close(); return this; } --- 918,921 ---- |