?? configuration.java
字號:
//$Id: Configuration.java 10841 2006-11-17 18:29:10Z max.andersen@jboss.com $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.lang.reflect.Array;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.ListIterator;import java.util.Map;import java.util.Properties;import java.util.Set;import java.util.TreeMap;import java.util.jar.JarFile;import java.util.zip.ZipEntry;import org.dom4j.Attribute;import org.dom4j.DocumentException;import org.dom4j.Element;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.w3c.dom.Document;import org.xml.sax.EntityResolver;import org.xml.sax.InputSource;import org.hibernate.EmptyInterceptor;import org.hibernate.HibernateException;import org.hibernate.Interceptor;import org.hibernate.InvalidMappingException;import org.hibernate.MappingException;import org.hibernate.MappingNotFoundException;import org.hibernate.SessionFactory;import org.hibernate.SessionFactoryObserver;import org.hibernate.dialect.Dialect;import org.hibernate.dialect.MySQLDialect;import org.hibernate.dialect.function.SQLFunction;import org.hibernate.engine.FilterDefinition;import org.hibernate.engine.Mapping;import org.hibernate.event.AutoFlushEventListener;import org.hibernate.event.DeleteEventListener;import org.hibernate.event.DirtyCheckEventListener;import org.hibernate.event.EventListeners;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.PersistEventListener;import org.hibernate.event.PostCollectionRecreateEventListener;import org.hibernate.event.PostCollectionRemoveEventListener;import org.hibernate.event.PostCollectionUpdateEventListener;import org.hibernate.event.PostDeleteEventListener;import org.hibernate.event.PostInsertEventListener;import org.hibernate.event.PostLoadEventListener;import org.hibernate.event.PostUpdateEventListener;import org.hibernate.event.PreCollectionRecreateEventListener;import org.hibernate.event.PreCollectionRemoveEventListener;import org.hibernate.event.PreCollectionUpdateEventListener;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.id.IdentifierGenerator;import org.hibernate.id.PersistentIdentifierGenerator;import org.hibernate.impl.SessionFactoryImpl;import org.hibernate.mapping.AuxiliaryDatabaseObject;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.proxy.EntityNotFoundDelegate;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.ConfigHelper;import org.hibernate.util.PropertiesHelper;import org.hibernate.util.ReflectHelper;import org.hibernate.util.SerializationHelper;import org.hibernate.util.StringHelper;import org.hibernate.util.XMLHelper;/** * 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 Logger log = LoggerFactory.getLogger( Configuration.class ); protected Map classes; protected Map imports; protected Map collections; protected Map tables; protected List auxiliaryDatabaseObjects; protected Map sqlFunctions; protected Map namedQueries; protected Map namedSqlQueries; /** * Map<String, SqlResultSetMapping> result set name, result set description */ protected Map sqlResultSetMappings; protected Map filterDefinitions; protected List secondPasses; protected List propertyReferences;// protected List extendsQueue; protected Map extendsQueue; protected Map tableNameBinding; protected Map columnNameBindingPerTable; private Interceptor interceptor; private Properties properties; private EntityResolver entityResolver; private EntityNotFoundDelegate entityNotFoundDelegate; protected transient XMLHelper xmlHelper; protected transient Map typeDefs; protected NamingStrategy namingStrategy; private EventListeners eventListeners; protected final SettingsFactory settingsFactory; private SessionFactoryObserver sessionFactoryObserver; protected void reset() { classes = new HashMap(); imports = new HashMap(); collections = new HashMap(); tables = new TreeMap(); namedQueries = new HashMap(); namedSqlQueries = new HashMap(); sqlResultSetMappings = new HashMap(); xmlHelper = new XMLHelper(); typeDefs = new HashMap(); propertyReferences = new ArrayList(); secondPasses = new ArrayList(); interceptor = EmptyInterceptor.INSTANCE; properties = Environment.getProperties(); entityResolver = XMLHelper.DEFAULT_DTD_RESOLVER; eventListeners = new EventListeners(); filterDefinitions = new HashMap();// extendsQueue = new ArrayList(); extendsQueue = new HashMap(); auxiliaryDatabaseObjects = new ArrayList(); tableNameBinding = new HashMap(); columnNameBindingPerTable = new HashMap(); namingStrategy = DefaultNamingStrategy.INSTANCE; sqlFunctions = new HashMap(); } private transient Mapping mapping = buildMapping(); protected Configuration(SettingsFactory settingsFactory) { this.settingsFactory = settingsFactory; reset(); } public Configuration() { this( new SettingsFactory() ); } /** * Iterate the entity mappings * * @return Iterator of the entity mappings currently contained in the configuration. */ public Iterator getClassMappings() { return classes.values().iterator(); } /** * Iterate the collection mappings * * @return Iterator of the collection mappings currently contained in the configuration. */ public Iterator getCollectionMappings() { return collections.values().iterator(); } /** * 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 entity * * @param entityName An entity name. * @return the entity mapping information */ public PersistentClass getClassMapping(String entityName) { return (PersistentClass) classes.get( entityName ); } /** * Get the mapping for a particular collection role * * @param role a collection role * @return The collection mapping information */ 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; } public EntityResolver getEntityResolver() { return entityResolver; } /** * Retrieve the user-supplied delegate to handle non-existent entity * scenarios. May be null. * * @return The user-supplied delegate */ public EntityNotFoundDelegate getEntityNotFoundDelegate() { return entityNotFoundDelegate; } /** * Specify a user-supplied delegate to be used to handle scenarios where an entity could not be * located by specified id. This is mainly intended for EJB3 implementations to be able to * control how proxy initialization errors should be handled... * * @param entityNotFoundDelegate The delegate to use */ public void setEntityNotFoundDelegate(EntityNotFoundDelegate entityNotFoundDelegate) { this.entityNotFoundDelegate = entityNotFoundDelegate; } /** * 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 { 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 { 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 ( InvalidMappingException e ) { throw e; } catch ( MappingNotFoundException e ) { throw e; } catch ( Exception e ) { throw new InvalidMappingException( "file", xmlFile.toString(), e ); } } /** * Add a cached 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 <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. * * @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 cachedFile = new File( xmlFile.getAbsolutePath() + ".bin" ); org.dom4j.Document doc = null; final boolean useCachedFile = xmlFile.exists() && cachedFile.exists() && xmlFile.lastModified() < cachedFile.lastModified(); if ( useCachedFile ) { try { 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: " + cachedFile.getPath(), e ); } catch ( FileNotFoundException e ) { log.warn( "I/O reported cached file could not be found : " + cachedFile.getPath(), e ); } } // 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 ); List errors = new ArrayList(); try { doc = xmlHelper.createSAXReader( xmlFile.getAbsolutePath(), errors, entityResolver ).read( xmlFile ); if ( errors.size() != 0 ) { throw new MappingException( "invalid mapping", ( Throwable ) errors.get( 0 ) ); } } catch( DocumentException e){ throw new MappingException( "invalid mapping", e ); } try { 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: " + cachedFile, e ); } catch ( FileNotFoundException e ) { log.warn( "I/O reported error writing cached file : " + cachedFile.getPath(), e ); } } add( doc ); return this; } catch ( InvalidMappingException e ) { throw e; } catch ( MappingNotFoundException 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.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -