?? configuration.java
字號:
//$Id: Configuration.java,v 1.67 2005/04/08 16:39:43 steveebersole Exp $package org.hibernate.cfg;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;import java.io.Serializable;import java.io.StringReader;import java.net.URL;import java.util.ArrayList;import java.util.Enumeration;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;import java.util.TreeMap;import java.util.Map.Entry;import java.util.jar.JarFile;import java.util.zip.ZipEntry;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.dom4j.Attribute;import org.dom4j.Element;import org.hibernate.EntityMode;import org.hibernate.HibernateException;import org.hibernate.Interceptor;import org.hibernate.MappingException;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.dialect.Dialect;import org.hibernate.dialect.MySQLDialect;import org.hibernate.engine.FilterDefinition;import org.hibernate.engine.Mapping;import org.hibernate.event.AutoFlushEventListener;import org.hibernate.event.PersistEventListener;import org.hibernate.event.DeleteEventListener;import org.hibernate.event.DirtyCheckEventListener;import org.hibernate.event.EvictEventListener;import org.hibernate.event.FlushEntityEventListener;import org.hibernate.event.FlushEventListener;import org.hibernate.event.InitializeCollectionEventListener;import org.hibernate.event.LoadEventListener;import org.hibernate.event.LockEventListener;import org.hibernate.event.MergeEventListener;import org.hibernate.event.PostDeleteEventListener;import org.hibernate.event.PostInsertEventListener;import org.hibernate.event.PostLoadEventListener;import org.hibernate.event.PostUpdateEventListener;import org.hibernate.event.PreDeleteEventListener;import org.hibernate.event.PreInsertEventListener;import org.hibernate.event.PreLoadEventListener;import org.hibernate.event.PreUpdateEventListener;import org.hibernate.event.RefreshEventListener;import org.hibernate.event.ReplicateEventListener;import org.hibernate.event.SaveOrUpdateEventListener;import org.hibernate.event.SessionEventListenerConfig;import org.hibernate.id.IdentifierGenerator;import org.hibernate.id.PersistentIdentifierGenerator;import org.hibernate.impl.SessionFactoryImpl;import org.hibernate.mapping.Collection;import org.hibernate.mapping.ForeignKey;import org.hibernate.mapping.IdentifierCollection;import org.hibernate.mapping.Index;import org.hibernate.mapping.PersistentClass;import org.hibernate.mapping.Property;import org.hibernate.mapping.RootClass;import org.hibernate.mapping.SimpleValue;import org.hibernate.mapping.Table;import org.hibernate.mapping.UniqueKey;import org.hibernate.secure.JACCConfiguration;import org.hibernate.tool.hbm2ddl.DatabaseMetadata;import org.hibernate.tool.hbm2ddl.TableMetadata;import org.hibernate.type.SerializationException;import org.hibernate.type.Type;import org.hibernate.util.ArrayHelper;import org.hibernate.util.CollectionHelper;import org.hibernate.util.SerializationHelper;import org.hibernate.util.XMLHelper;import org.hibernate.util.ReflectHelper;import org.w3c.dom.Document;import org.xml.sax.EntityResolver;import org.xml.sax.InputSource;/** * An instance of <tt>Configuration</tt> allows the application * to specify properties and mapping documents to be used when * creating a <tt>SessionFactory</tt>. Usually an application will create * a single <tt>Configuration</tt>, build a single instance of * <tt>SessionFactory</tt> and then instantiate <tt>Session</tt>s in * threads servicing client requests. The <tt>Configuration</tt> is meant * only as an initialization-time object. <tt>SessionFactory</tt>s are * immutable and do not retain any association back to the * <tt>Configuration</tt>.<br> * <br> * A new <tt>Configuration</tt> will use the properties specified in * <tt>hibernate.properties</tt> by default. * * @author Gavin King * @see org.hibernate.SessionFactory */public class Configuration implements Serializable { private static Log log = LogFactory.getLog( Configuration.class ); protected Map classes; protected Map imports; protected Map collections; protected Map tables; protected Map namedQueries; protected Map namedSqlQueries; protected Map filterDefinitions; protected List secondPasses; protected List propertyReferences; protected Map extendsQueue; // key: classname -> value: dom4j document private Interceptor interceptor; private Properties properties; private EntityResolver entityResolver; private transient XMLHelper xmlHelper; protected transient Map typeDefs; protected NamingStrategy namingStrategy = DefaultNamingStrategy.INSTANCE; private SessionEventListenerConfig sessionEventListenerConfig; private final SettingsFactory settingsFactory; protected void reset() { classes = new HashMap(); imports = new HashMap(); collections = new HashMap(); tables = new TreeMap(); namedQueries = new HashMap(); namedSqlQueries = new HashMap(); xmlHelper = new XMLHelper(); typeDefs = new HashMap(); propertyReferences = new ArrayList(); secondPasses = new ArrayList(); interceptor = EMPTY_INTERCEPTOR; properties = Environment.getProperties(); entityResolver = XMLHelper.DEFAULT_DTD_RESOLVER; sessionEventListenerConfig = new SessionEventListenerConfig(); filterDefinitions = new HashMap(); extendsQueue = new HashMap(); } private transient Mapping mapping = buildMapping(); protected Configuration(SettingsFactory settingsFactory) { this.settingsFactory = settingsFactory; reset(); } public Configuration() { this( new SettingsFactory() ); } /** * Iterate the class mappings */ public Iterator getClassMappings() { return classes.values().iterator(); } /** * Iterate the collection mappings */ public Iterator getCollectionMappings() { return collections.values().iterator(); } /** * Iterate the table mappings */ public Iterator getTableMappings() { return tables.values().iterator(); } /** * Get the mapping for a particular class */ public PersistentClass getClassMapping(String persistentClass) { return ( PersistentClass ) classes.get( persistentClass ); } /** * Get the mapping for a particular collection role * * @param role a collection role * @return Collection */ public Collection getCollectionMapping(String role) { return ( Collection ) collections.get( role ); } /** * Set a custom entity resolver. This entity resolver must be * set before addXXX(misc) call. * Default value is {@link org.hibernate.util.DTDEntityResolver} * * @param entityResolver entity resolver to use */ public void setEntityResolver(EntityResolver entityResolver) { this.entityResolver = entityResolver; } /** * Read mappings from a particular XML file * * @param xmlFile a path to a file */ public Configuration addFile(String xmlFile) throws MappingException { log.info( "Mapping file: " + xmlFile ); try { List errors = new ArrayList(); org.dom4j.Document doc = xmlHelper.createSAXReader( xmlFile, errors, entityResolver ).read( new File( xmlFile ) ); if ( errors.size() != 0 ) throw new MappingException( "invalid mapping", ( Throwable ) errors.get( 0 ) ); add( doc ); return this; } catch ( Exception e ) { log.error( "Could not configure datastore from file: " + xmlFile, e ); throw new MappingException( "Could not configure datastore from file: " + xmlFile, e ); } } /** * Read mappings from a particular XML file * * @param xmlFile a path to a file */ public Configuration addFile(File xmlFile) throws MappingException { log.info( "Mapping file: " + xmlFile.getPath() ); try { addInputStream( new FileInputStream( xmlFile ) ); } catch ( Exception e ) { log.error( "Could not configure datastore from file: " + xmlFile.getPath(), e ); throw new MappingException( "Could not configure datastore from file: " + xmlFile.getPath(), e ); } return this; } /** * 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. */ public Configuration addCacheableFile(File xmlFile) throws MappingException { try { File lazyfile = 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(); if ( useCachedFile ) { try { log.info( "Mapping cached file: " + lazyfile ); doc = ( org.dom4j.Document ) SerializationHelper.deserialize( new FileInputStream( lazyfile ) ); } catch ( SerializationException e ) { log.warn( "Could not deserialize cached file: " + lazyfile.getPath(), e ); } } // If deserialization failed if ( doc == null ) { doc = xmlHelper.createSAXReader( xmlFile.getAbsolutePath(), errors, entityResolver ).read( xmlFile ); try { log.info( "Writing cached file of " + xmlFile + " to " + lazyfile ); SerializationHelper.serialize( ( Serializable ) doc, new FileOutputStream( lazyfile ) ); } catch ( SerializationException e ) { log.warn( "Could not write cached file: " + lazyfile, e ); } } if ( errors.size() != 0 ) throw new MappingException( "invalid mapping", ( Throwable ) errors.get( 0 ) ); add( doc ); return this; } catch ( Exception e ) { log.error( "Could not configure datastore from file: " + xmlFile, e ); throw new MappingException( e ); } } public Configuration addCacheableFile(String xmlFile) throws MappingException { return addCacheableFile( new File( xmlFile ) ); } /** * Read mappings from a <tt>String</tt> * * @param xml an XML string */ public Configuration addXML(String xml) throws MappingException { if ( log.isDebugEnabled() ) log.debug( "Mapping XML:\n" + xml ); try { List errors = new ArrayList(); org.dom4j.Document doc = xmlHelper.createSAXReader( "XML String", errors, entityResolver ).read( new StringReader( xml ) ); if ( errors.size() != 0 ) throw new MappingException( "invalid mapping", ( Throwable ) errors.get( 0 ) ); add( doc ); } catch ( Exception e ) { log.error( "Could not configure datastore from XML", e ); throw new MappingException( e ); } return this; } /** * 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: " + url , e ); throw new MappingException( "Could not configure datastore from URL: " + url, e ); } return this; } /** * Read mappings from a DOM <tt>Document</tt> * * @param doc a DOM document */ public Configuration addDocument(Document doc) throws MappingException { if ( log.isDebugEnabled() ) log.debug( "Mapping XML:\n" + doc ); try { add( xmlHelper.createDOMReader().read( doc ) ); } catch ( Exception e ) { log.error( "Could not configure datastore from XML document", e ); throw new MappingException( e ); } return this; } protected void add(org.dom4j.Document doc) throws MappingException { try { HbmBinder.bindRoot( doc, createMappings(), CollectionHelper.EMPTY_MAP ); // TODO: possibly get inheritable meta's from cfg.xml } catch ( MappingException me ) { log.error( "Could not compile the mapping document", me ); throw me; } } /** * Create a new <tt>Mappings</tt> to add class and collection * mappings to. */ public Mappings createMappings() { return new Mappings( classes, collections, tables, namedQueries, namedSqlQueries, imports, secondPasses, propertyReferences, namingStrategy, typeDefs, filterDefinitions, extendsQueue ); } /** * Read mappings from an <tt>InputStream</tt> * * @param xmlInputStream an <tt>InputStream</tt> containing XML */ public Configuration addInputStream(InputStream xmlInputStream) throws MappingException { try { List errors = new ArrayList(); 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 ) );
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -