From: <one...@us...> - 2003-04-06 15:02:07
|
Update of /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/cfg In directory sc8-pr-cvs1:/tmp/cvs-serv7415 Modified Files: Configuration.java Log Message: applied Max Andersen's patch to allow configuration by various means: Index: Configuration.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/cfg/Configuration.java,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** Configuration.java 4 Apr 2003 13:51:59 -0000 1.19 --- Configuration.java 6 Apr 2003 15:02:01 -0000 1.20 *************** *** 10,17 **** --- 10,21 ---- import java.util.jar.JarFile; import java.util.zip.ZipEntry; + import java.io.File; + import java.io.FileInputStream; + import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; import java.io.StringReader; + import java.net.URL; import org.apache.commons.logging.Log; *************** *** 172,175 **** --- 176,195 ---- /** + * Read mappings from a <tt>URL</tt> + * @param url + */ + public Configuration addURL(URL url) throws MappingException { + if ( log.isDebugEnabled() ) log.debug("Mapping URL:\n" + url); + try { + addInputStream( url.openStream() ); + } + catch (Exception e) { + log.error("Could not configure datastore from URL", e); + throw new MappingException(e); + } + return this; + } + + /** * Read mappings from a DOM <tt>Document</tt> * @param doc a DOM document *************** *** 187,191 **** } ! private void add(org.dom4j.Document doc) throws Exception { try { Binder.bindRoot( doc, createMappings() ); --- 207,211 ---- } ! protected void add(org.dom4j.Document doc) throws Exception { try { Binder.bindRoot( doc, createMappings() ); *************** *** 670,687 **** * Use the mappings and properties specified in the given application * resource. The format of the resource is defined in ! * <tt>hibernate-configuration.dtd</tt>. */ public Configuration configure(String resource) throws HibernateException { - InputStream stream = getConfigurationInputStream(resource); org.dom4j.Document doc; try { ! doc = XMLHelper.createSAXReader(resource).read( new InputSource(stream) ); } catch (Exception e) { ! log.error("Problem parsing configuration " + resource, e); ! throw new HibernateException( "Problem parsing configuration " + resource + ": " + e ); } Element sfNode = doc.getRootElement().element("session-factory"); --- 690,788 ---- * Use the mappings and properties specified in the given application * resource. The format of the resource is defined in ! * <tt>hibernate-configuration-2.0.dtd</tt>. ! * ! * The resource is found via <tt>getConfigurationInputStream(resource)</tt>. */ public Configuration configure(String resource) throws HibernateException { InputStream stream = getConfigurationInputStream(resource); + return configure(stream, resource); + } + + /** + * Use the mappings and properties specified in the given document. + * The format of the document is defined in + * <tt>hibernate-configuration-2.0.dtd</tt>. + * + * @param url URL from which you wish to load the configuration + * @return A configuration configured via the file + * @throws HibernateException + */ + public Configuration configure(URL url) throws HibernateException { + try { + return configure( url.openStream(), url.toString() ); + } + catch (IOException ioe) { + throw new HibernateException("could not configure from URL: " + url, ioe); + } + } + + /** + * Use the mappings and properties specified in the given application + * file. The format of the file is defined in + * <tt>hibernate-configuration-2.0.dtd</tt>. + * + * @param configFile <tt>File</tt> from which you wish to load the configuration + * @return A configuration configured via the file + * @throws HibernateException + */ + public Configuration configure(File configFile) throws HibernateException { + try { + return configure( new FileInputStream(configFile), configFile.toString() ); + } + catch (FileNotFoundException fnfe) { + throw new HibernateException("could not find file: " + configFile, fnfe); + } + } + + /** + * Use the mappings and properties specified in the given application + * resource. The format of the resource is defined in + * <tt>hibernate-configuration-2.0.dtd</tt>. + * + * @param stream Inputstream to be read from + * @param resourceName The name to use in warning/error messages + * @return A configuration configured via the stream + * @throws HibernateException + */ + protected Configuration configure(InputStream stream, String resourceName) throws HibernateException { + + org.dom4j.Document doc; + try { + doc = XMLHelper.createSAXReader(resourceName).read( new InputSource(stream) ); + } + catch (Exception e) { + log.error("problem parsing configuration" + resourceName, e); + throw new HibernateException("problem parsing configuration" + resourceName, e); + } + + return configure(doc); + } + + /** + * Use the mappings and properties specified in the given XML document. + * The format of the file is defined in + * <tt>hibernate-configuration-2.0.dtd</tt>. + * + * @param document an XML document from which you wish to load the configuration + * @return A configuration configured via the <tt>Document</tt> + * @throws HibernateException + * @throws FileNotFoundException if there is problem in accessing the file. + */ + public Configuration configure(Document document) throws HibernateException { + org.dom4j.Document doc; try { ! doc = XMLHelper.createDOMReader().read(document); } catch (Exception e) { ! log.error("problem parsing document", e); ! throw new HibernateException("problem parsing document", e); } + + return configure(doc); + } + + protected Configuration configure(org.dom4j.Document doc) throws HibernateException { Element sfNode = doc.getRootElement().element("session-factory"); |