📄 configuration.java
字号:
* @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.2.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, entityResolver ).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.2.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 ); parseSessionFactory( sfNode, name ); Element secNode = doc.getRootElement().element( "security" ); if ( secNode != null ) parseSecurity( secNode ); log.info( "Configured SessionFactory: " + name ); log.debug( "properties: " + properties ); return this; } private void parseSessionFactory(Element sfNode, String name) { Iterator elements = sfNode.elementIterator(); while ( elements.hasNext() ) { Element subelement = ( Element ) elements.next(); String subelementName = subelement.getName(); if ( "mapping".equals( subelementName ) ) { parseMappingElement(subelement, name); } else if ( "class-cache".equals( subelementName ) ) { String className = subelement.attributeValue( "class" ); Attribute regionNode = subelement.attribute( "region" ); final String region = ( regionNode == null ) ? className : regionNode.getValue(); setCacheConcurrencyStrategy( className, subelement.attributeValue( "usage" ), region ); } else if ( "collection-cache".equals( subelementName ) ) { String role = subelement.attributeValue( "collection" ); Attribute regionNode = subelement.attribute( "region" ); final String region = ( regionNode == null ) ? role : regionNode.getValue(); setCollectionCacheConcurrencyStrategy( role, subelement.attributeValue( "usage" ), region ); } else if ( "listener".equals( subelementName ) ) { parseListener( subelement ); } } } protected void parseMappingElement(Element subelement, String name) { Attribute rsrc = subelement.attribute( "resource" ); Attribute file = subelement.attribute( "file" ); Attribute jar = subelement.attribute( "jar" ); Attribute pkg = subelement.attribute( "package" ); Attribute clazz = subelement.attribute( "class" ); if ( rsrc != null ) { log.debug( name + "<-" + rsrc ); addResource( rsrc.getValue() ); } else if ( jar != null ) { log.debug( name + "<-" + jar ); addJar( new File( jar.getValue() ) ); } else if ( pkg != null ) { throw new MappingException("An AnnotationConfiguration instance is required to use <mapping package=\"" + pkg.getValue() + "\"/>"); } else if ( clazz != null ) { throw new MappingException("An AnnotationConfiguration instance is required to use <mapping clazz=\"" + clazz.getValue() + "\"/>"); } else { if ( file == null ) throw new MappingException( "<mapping> element in configuration specifies no attributes" ); log.debug( name + "<-" + file ); addFile( file.getValue() ); } } private void parseSecurity(Element secNode) { String contextId = secNode.attributeValue( "context" ); log.info( "JACC contextID: " + contextId ); JACCConfiguration jcfg = new JACCConfiguration( contextId ); Iterator grantElements = secNode.elementIterator(); while ( grantElements.hasNext() ) { Element grantElement = ( Element ) grantElements.next(); String elementName = grantElement.getName(); if ( "grant".equals( elementName ) ) { jcfg.addPermission( grantElement.attributeValue( "role" ), grantElement.attributeValue( "entity-name" ), grantElement.attributeValue( "actions" ) ); } } } private void parseListener(Element element) { String type = element.attributeValue( "type" ); String impl = element.attributeValue( "class" ); log.debug( "Encountered configured listener : " + type + "=" + impl ); setListener( type, impl ); } public void setListener(String type, String listenerClass) { try { Object listener = ReflectHelper.classForName( listenerClass ).newInstance(); setListener( type, listener ); } catch ( Throwable t ) { log.warn( "Unable to instantiate specified listener class [" + listenerClass + "]", t ); } } public void setListener(String type, Object listener) { if ( "auto-flush".equals( type ) ) { sessionEventListenerConfig.setAutoFlushEventListener( ( AutoFlushEventListener ) listener ); } else if ( "merge".equals( type ) ) { sessionEventListenerConfig.setMergeEventListener( ( MergeEventListener ) listener ); } else if ( "create".equals( type ) ) { sessionEventListenerConfig.setCreateEventListener( ( PersistEventListener ) listener ); } else if ( "delete".equals( type ) ) { sessionEventListenerConfig.setDeleteEventListener( ( DeleteEventListener ) listener ); } else if ( "dirty-check".equals( type ) ) { sessionEventListenerConfig.setDirtyCheckEventListener( ( DirtyCheckEventListener ) listener ); } else if ( "evict".equals( type ) ) { sessionEventListenerConfig.setEvictEventListener( ( EvictEventListener ) listener ); } else if ( "flush".equals( type ) ) { sessionEventListenerConfig.setFlushEventListener( ( FlushEventListener ) listener ); } else if ( "flush-entity".equals( type ) ) { sessionEventListenerConfig.setFlushEntityEventListener( ( FlushEntityEventListener ) listener ); } else if ( "load".equals( type ) ) { sessionEventListenerConfig.setLoadEventListener( ( LoadEventListener ) listener ); } else if ( "load-collection".equals( type ) ) { sessionEventListenerConfig.setInitializeCollectionEventListener( ( InitializeCollectionEventListener ) listener ); } else if ( "lock".equals( type ) ) { sessionEventListenerConfig.setLockEventListener( ( LockEventListener ) listener ); } else if ( "refresh".equals( type ) ) { sessionEventListenerConfig.setRefreshEventListener( ( RefreshEventListener ) listener ); } else if ( "replicate".equals( type ) ) { sessionEventListenerConfig.setReplicateEventListener( ( ReplicateEventListener ) listener ); } else if ( "save-update".equals( type ) ) { sessionEventListenerConfig.setSaveOrUpdateEventListener( ( SaveOrUpdateEventListener ) listener ); } else if ( "save".equals( type ) ) { sessionEventListenerConfig.setSaveEventListener( ( SaveOrUpdateEventListener ) listener ); } else if ( "update".equals( type ) ) { sessionEventListenerConfig.setUpdateEventListener( ( SaveOrUpdateEventListener ) listener ); } else if ( "pre-load".equals( type ) ) { sessionEventListenerConfig.setPreLoadEventListener( ( PreLoadEventListener ) listener ); } else if ( "pre-update".equals( type ) ) { sessionEventListenerConfig.setPreUpdateEventListener( ( PreUpdateEventListener ) listener ); } else if ( "pre-delete".equals( type ) ) { sessionEventListenerConfig.setPreDeleteEventListener( ( PreDeleteEventListener ) listener ); } else if ( "pre-insert".equals( type ) ) { sessionEventListenerConfig.setPreInsertEventListener( ( PreInsertEventListener ) listener ); } else if ( "post-load".equals( type ) ) { sessionEventListenerConfig.setPostLoadEventListener( ( PostLoadEventListener ) listener ); } else if ( "post-update".equals( type ) ) { sessionEventListenerConfig.setPostUpdateEventListener( ( PostUpdateEventListener ) listener ); } else if ( "post-delete".equals( type ) ) { sessionEventListenerConfig.setPostDeleteEventListener( ( PostDeleteEventListener ) listener ); } else if ( "post-insert".equals( type ) ) { sessionEventListenerConfig.setPostInsertEventListener( ( PostInsertEventListener ) listener ); } else { log.warn( "Unrecognized listener type [" + type + "]" ); } } public SessionEventListenerConfig getSessionEventListenerConfig() { return sessionEventListenerConfig; } RootClass getRootClassMapping(String 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(String clazz, String concurrencyStrategy) throws MappingException { setCacheConcurrencyStrategy( clazz, concurrencyStrategy, clazz ); return this; } void setCacheConcurrencyStrategy(String clazz, String concurrencyStrategy, String region) throws MappingException { RootClass rootClass = getRootClassMapping( clazz ); rootClass.setCacheConcurrencyStrategy( concurrencyStrategy ); } /** * Set up a cache for a collection role * * @param collectionRole * @param concurrencyStrategy * @return Configuration * @throws MappingException */ public Configuration setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy) throws MappingException { setCollectionCacheConcurrencyStrategy( collectionRole, concurrencyStrategy, collectionRole ); return this; } void setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy, String region) throws MappingException { Collection collection = getCollectionMapping( collectionRole ); collection.setCacheConcurrencyStrategy( concurrencyStrategy ); } /** * 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 */ public 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 Configuration setNamingStrategy(NamingStrategy namingStrategy) { this.namingStrategy = namingStrategy; return this; } private Mapping buildMapping() { return new Mapping() { /** * Returns the identifier type of a mapped class */ public Type getIdentifierType(String persistentClass) throws MappingException { PersistentClass pc = ( ( PersistentClass ) classes.get( persistentClass ) ); if (pc==null) throw new MappingException("persistent class not known: " + persistentClass); return pc.getIdentifier().getType(); } public String getIdentifierPropertyName(String persistentClass) throws MappingException { final PersistentClass pc = ( PersistentClass ) classes.get( persistentClass ); if (pc==null) throw new MappingException("persistent class not known: " + persistentClass); if ( !pc.hasIdentifierProperty() ) return null; return pc.getIdentifierProperty().getName(); } public Type getPropertyType(String persistentClass, String propertyName) throws MappingException { final PersistentClass pc = ( PersistentClass ) classes.get( persistentClass ); if (pc==null) throw new MappingException("persistent class not known: " + persistentClass); Property prop = pc.getProperty(propertyName); if (prop==null) throw new MappingException("property not known: " + persistentClass + '.' + propertyName); return prop.getType(); } }; } private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException { ois.defaultReadObject(); this.mapping = buildMapping(); xmlHelper = new XMLHelper(); } public Map getFilterDefinitions() { return filterDefinitions; } public void addFilterDefinition(FilterDefinition definition) { filterDefinitions.put( definition.getFilterName(), definition ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -