⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configuration.java

📁 通过系统把几乎所有与人力资源相关的数据统一管理
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
//$Id: Configuration.java,v 1.27.2.30 2004/02/04 18:56:44 oneovthafew Exp $package net.sf.hibernate.cfg;import java.util.Enumeration;import java.util.Iterator;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Properties;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;import org.apache.commons.logging.LogFactory;import org.dom4j.Attribute;import org.dom4j.Element;import org.w3c.dom.Document;import org.xml.sax.InputSource;import net.sf.hibernate.util.ArrayHelper;import net.sf.hibernate.util.ReflectHelper;import net.sf.hibernate.util.StringHelper;import net.sf.hibernate.util.XMLHelper;import net.sf.hibernate.HibernateException;import net.sf.hibernate.Interceptor;import net.sf.hibernate.MappingException;import net.sf.hibernate.SessionFactory;import net.sf.hibernate.tool.hbm2ddl.DatabaseMetadata;import net.sf.hibernate.tool.hbm2ddl.TableMetadata;import net.sf.hibernate.type.Type;import net.sf.hibernate.id.IdentifierGenerator;import net.sf.hibernate.id.PersistentIdentifierGenerator;import net.sf.hibernate.impl.SessionFactoryImpl;import net.sf.hibernate.mapping.Collection;import net.sf.hibernate.mapping.ForeignKey;import net.sf.hibernate.mapping.Index;import net.sf.hibernate.mapping.PersistentClass;import net.sf.hibernate.mapping.Property;import net.sf.hibernate.mapping.RootClass;import net.sf.hibernate.mapping.Table;import net.sf.hibernate.mapping.SimpleValue;import net.sf.hibernate.cache.Cache;import net.sf.hibernate.cache.CacheConcurrencyStrategy;import net.sf.hibernate.cache.CacheException;import net.sf.hibernate.cache.CacheFactory;import net.sf.hibernate.dialect.Dialect;import net.sf.hibernate.engine.Mapping;/** * 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. * * @see net.sf.hibernate.SessionFactory * @author Gavin King */public class Configuration {	private Map classes = new HashMap();	private Map imports = new HashMap();	private Map collections = new HashMap();	private Map tables = new HashMap();	private Map namedQueries = new HashMap();	private Map namedSqlQueries = new HashMap();	private List secondPasses = new ArrayList();	private List propertyReferences = new ArrayList();	private Interceptor interceptor = EMPTY_INTERCEPTOR;	private Properties properties = Environment.getProperties();	private Map caches = new HashMap();		private NamingStrategy namingStrategy = DefaultNamingStrategy.INSTANCE; 	private static Log log = LogFactory.getLog(Configuration.class);	protected void reset() {		classes = new HashMap();		collections = new HashMap();		tables = new HashMap();		namedQueries = new HashMap();		namedSqlQueries = new HashMap();		secondPasses = new ArrayList();		interceptor = EMPTY_INTERCEPTOR;		properties = Environment.getProperties();	}	private Mapping mapping = new Mapping() {		/**		 * Returns the identifier type of a mapped class		 */		public Type getIdentifierType(Class persistentClass) throws MappingException {			return ( (PersistentClass) classes.get(persistentClass) ).getIdentifier().getType();		}		public String getIdentifierPropertyName(Class persistentClass) throws MappingException {			return ( (PersistentClass) classes.get(persistentClass) ).getIdentifierProperty().getName();		}		public Type getPropertyType(Class persistentClass, String propertyName) throws MappingException {			return ( (PersistentClass) classes.get(persistentClass) ).getProperty(propertyName).getType();		}	};		public Configuration() {		reset();	}	/**	 * 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	 */	private Iterator getTableMappings() {		return tables.values().iterator();	}	/**	 * Get the mapping for a particular class	 */	public PersistentClass getClassMapping(Class 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);	}	/**	 * 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).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(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(e);		}		return this;	}	/**	 * 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).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", e);			throw new MappingException(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 Exception {		try {			Binder.bindRoot( doc, createMappings() );		}		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, 			caches, 			secondPasses, 			propertyReferences,			namingStrategy		);	}	/**	 * 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).read( new InputSource(xmlInputStream) );			if ( errors.size()!=0 ) throw new MappingException( "invalid mapping", (Throwable) errors.get(0) );			add(doc);			return this;		}		catch (MappingException me) {			throw me;		}		catch (Exception e) {			log.error("Could not configure datastore from input stream", e);			throw new MappingException(e);		}		finally {			try{				xmlInputStream.close();			} 			catch (IOException ioe){				log.error("could not close input stream", ioe);				}		}	}	/**	 * Read mappings from an application resource	 * @param path a resource	 * @param classLoader a <tt>ClassLoader</tt> to use	 */	public Configuration addResource(String path, ClassLoader classLoader) throws MappingException {		log.info("Mapping resource: " + path);		InputStream rsrc = classLoader.getResourceAsStream(path);		if (rsrc==null) throw new MappingException("Resource: " + path + " not found");		try {			return addInputStream(rsrc);		} 		catch (MappingException me) {			throw new MappingException("Error reading resource: " + path, 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>.	 * @param persistentClass the mapped class	 */	public Configuration addClass(Class persistentClass) throws MappingException {		String fileName = persistentClass.getName().replace(StringHelper.DOT,'/') + ".hbm.xml";		log.info("Mapping resource: " + fileName);		InputStream rsrc = persistentClass.getClassLoader().getResourceAsStream(fileName);		if (rsrc==null) throw new MappingException("Resource: " + fileName + " not found");		try {			return addInputStream(rsrc);		} 		catch (MappingException me) {			throw new MappingException("Error reading resource: " + fileName, me);		}	}	/**	 * Read all mappings from a jar file	 * @param resource an application resource (a jar file)	 * @deprecated use <tt>addJar(java.io.File)</tt>	 */	public Configuration addJar(String resource) throws MappingException {		return addJar( new File( 			Thread.currentThread().getContextClassLoader().getResource(resource).getFile() 		) );	}	/**	 * Read all mappings from a jar file	 * @param jar a jar file	 */	public Configuration addJar(File jar) throws MappingException {		log.info( "Searching for mapping documents in jar: " + jar.getName() );		final JarFile jarFile;		try {			jarFile = new JarFile(jar);		}		catch (IOException ioe) {			log.error("Could not configure datastore from jar: " + jar.getName(), ioe);			throw new MappingException("Could not configure datastore from jar: " + jar.getName(), ioe);		}		Enumeration enum = jarFile.entries();		while( enum.hasMoreElements() ) {			ZipEntry ze = (ZipEntry) enum.nextElement();			if( ze.getName().endsWith(".hbm.xml") ) {				log.info( "Found mapping documents in jar: " + ze.getName() );				try {					addInputStream( jarFile.getInputStream(ze) );				}				catch (MappingException me) {					throw me;				}				catch (Exception e) {					log.error("Could not configure datastore from jar", e);					throw new MappingException(e);				}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -