?? configuration.java
字號:
/** * Configure an <tt>Interceptor</tt> */ public Configuration setInterceptor(Interceptor interceptor) { this.interceptor = interceptor; return this; } /** * Specify a completely new set of properties */ public Configuration setProperties(Properties properties) { this.properties = properties; return this; } /** * Set the given properties */ public Configuration addProperties(Properties extraProperties) { this.properties.putAll(extraProperties); return this; } /** * Set a property */ public Configuration setProperty(String propertyName, String value) { properties.setProperty(propertyName, value); return this; } /** * Get a property */ public String getProperty(String propertyName) { return properties.getProperty(propertyName); } private void addProperties(Element parent) { Iterator iter = parent.elementIterator("property"); while ( iter.hasNext() ) { Element node = (Element) iter.next(); String name = node.attributeValue("name"); String value = node.getText().trim(); log.debug(name + "=" + value); properties.setProperty(name, value); if ( !name.startsWith("hibernate") ) properties.setProperty("hibernate." + name, value); } Environment.verifyProperties(properties); } /** * Get the configuration file as an <tt>InputStream</tt>. Might be overridden * by subclasses to allow the configuration to be located by some arbitrary * mechanism. */ protected InputStream getConfigurationInputStream(String resource) throws HibernateException { log.info("Configuration resource: " + resource); InputStream stream = Environment.class.getResourceAsStream(resource); if (stream==null) { log.warn(resource + " not found"); throw new HibernateException(resource + " not found"); } return stream; } /** * Use the mappings and properties specified in an application * resource named <tt>hibernate.cfg.xml</tt>. */ public Configuration configure() throws HibernateException { configure("/hibernate.cfg.xml"); return this; } /** * 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 { log.info("configuring from resource: " + resource); InputStream stream = getConfigurationInputStream(resource); return doConfigure(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 { log.info( "configuring from url: " + url.toString() ); try { return doConfigure( 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 { log.info( "configuring from file: " + configFile.getName() ); try { return doConfigure( 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 doConfigure(InputStream stream, String resourceName) throws HibernateException { org.dom4j.Document doc; try { List errors = new ArrayList(); doc = XMLHelper.createSAXReader(resourceName, errors).read( new InputSource(stream) ); if ( errors.size()!=0 ) throw new MappingException( "invalid configuration", (Throwable) errors.get(0) ); } catch (Exception e) { log.error("problem parsing configuration" + resourceName, e); throw new HibernateException("problem parsing configuration" + resourceName, e); } finally { try{ stream.close(); } catch (IOException ioe){ log.error("could not close stream on: " + resourceName, ioe); } } return doConfigure(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 if there is problem in accessing the file. */ public Configuration configure(Document document) throws HibernateException { log.info("configuring from XML document"); 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 doConfigure(doc); } protected Configuration doConfigure(org.dom4j.Document doc) throws HibernateException { Element sfNode = doc.getRootElement().element("session-factory"); String name = sfNode.attributeValue("name"); if (name!=null) properties.setProperty(Environment.SESSION_FACTORY_NAME, name); addProperties(sfNode); Iterator elements = sfNode.elementIterator(); while ( elements.hasNext() ) { Element mapElement = (Element) elements.next(); String elemname = mapElement.getName(); if ( "mapping".equals(elemname) ) { Attribute rsrc = mapElement.attribute("resource"); Attribute file = mapElement.attribute("file"); Attribute jar = mapElement.attribute("jar"); if (rsrc!=null) { log.debug(name + "<-" + rsrc); try { addResource( rsrc.getValue(), Thread.currentThread().getContextClassLoader() ); } catch (MappingException me) { addResource( rsrc.getValue(), Environment.class.getClassLoader() ); } } else if ( jar!=null ) { log.debug(name + "<-" + jar); addJar( jar.getValue() ); } else { if (file==null) throw new MappingException("<mapping> element in configuration specifies no attributes"); log.debug(name + "<-" + file); addFile( file.getValue() ); } } else if ( "jcs-class-cache".equals(elemname) || "class-cache".equals(elemname) ) { String className = mapElement.attributeValue("class"); final Class clazz; try { clazz = ReflectHelper.classForName(className); } catch (ClassNotFoundException cnfe) { throw new MappingException("Could not find class: " + className, cnfe); } Attribute regionNode = mapElement.attribute("region"); final String region = (regionNode==null) ? className : regionNode.getValue(); CacheConcurrencyStrategy cache = CacheFactory.createCache( mapElement, region, getRootClassMapping(clazz).isMutable() ); setCacheConcurrencyStrategy(clazz, cache, region); } else if ( "jcs-collection-cache".equals(elemname) || "collection-cache".equals(elemname) ) { String role = mapElement.attributeValue("collection"); Collection collection = getCollectionMapping(role); Attribute regionNode = mapElement.attribute("region"); final String region = (regionNode==null) ? role : regionNode.getValue(); CacheConcurrencyStrategy cache = CacheFactory.createCache( mapElement, region, collection.getOwner().isMutable() ); setCacheConcurrencyStrategy(role, cache, region); } } log.info("Configured SessionFactory: " + name); log.debug("properties: " + properties); return this; } RootClass getRootClassMapping(Class clazz) throws MappingException { try { return (RootClass) getClassMapping(clazz); } catch (ClassCastException cce) { throw new MappingException("You may only specify a cache for root <class> mappings"); } } /** * Set up a cache for an entity class * @param clazz * @param concurrencyStrategy * @return Configuration * @throws MappingException */ public Configuration setCacheConcurrencyStrategy(Class clazz, CacheConcurrencyStrategy concurrencyStrategy) throws MappingException { setCacheConcurrencyStrategy( clazz, concurrencyStrategy, clazz.getName() ); return this; } void setCacheConcurrencyStrategy(Class clazz, CacheConcurrencyStrategy concurrencyStrategy, String region) throws MappingException { RootClass rootClass = getRootClassMapping(clazz); rootClass.setCache(concurrencyStrategy); caches.put( rootClass.getMappedClass().getName(), concurrencyStrategy ); } /** * Set up a cache for a collection role * @param collectionRole * @param concurrencyStrategy * @return Configuration * @throws MappingException */ public Configuration setCacheConcurrencyStrategy(String collectionRole, CacheConcurrencyStrategy concurrencyStrategy) throws MappingException { setCacheConcurrencyStrategy(collectionRole, concurrencyStrategy, collectionRole); return this; } void setCacheConcurrencyStrategy(String collectionRole, CacheConcurrencyStrategy concurrencyStrategy, String region) throws MappingException { Collection collection = getCollectionMapping(collectionRole); collection.setCache(concurrencyStrategy); Object old = caches.put( collection.getRole(), concurrencyStrategy ); if (old!=null) throw new MappingException("duplicate cache region"); } protected void configureCaches(Settings settings) throws HibernateException { //TODO: this is actually broken, I guess, since changing the // cache provider property and rebuilding the SessionFactory // will affect existing SessionFactory! log.info("instantiating and configuring caches"); String prefix = properties.getProperty(Environment.CACHE_REGION_PREFIX); Iterator iter = caches.entrySet().iterator(); while ( iter.hasNext() ) { Map.Entry me = (Map.Entry) iter.next(); String name = (String) me.getKey(); if (prefix != null) name = prefix + "." + name; CacheConcurrencyStrategy strat = (CacheConcurrencyStrategy) me.getValue(); if ( log.isDebugEnabled() ) log.debug("instantiating cache " + name); Cache cache; try { cache = settings.getCacheProvider().buildCache(name, properties); } catch (CacheException e) { throw new HibernateException( "Could not instantiate Cache", e ); } strat.setCache(cache); strat.setMinimalPuts( settings.isMinimalPutsEnabled() ); } caches.clear(); } /** * Get the query language imports * * @return a mapping from "import" names to fully qualified class names */ public Map getImports() { return imports; } /** * Create an object-oriented view of the configuration properties */ protected Settings buildSettings() throws HibernateException { return SettingsFactory.buildSettings(properties); } public Map getNamedSQLQueries() { return namedSqlQueries; } /** * @return the NamingStrategy. */ public NamingStrategy getNamingStrategy() { return namingStrategy; } /** * Set a custom naming strategy * * @param namingStrategy the NamingStrategy to set */ public void setNamingStrategy(NamingStrategy namingStrategy) { this.namingStrategy = namingStrategy; }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -