📄 configuration.java
字号:
private EventListeners getInitializedEventListeners() {
EventListeners result = (EventListeners) eventListeners.shallowCopy();
result.initializeListeners( this );
return result;
}
/**
* Return the configured <tt>Interceptor</tt>
*/
public Interceptor getInterceptor() {
return interceptor;
}
/**
* Get all properties
*/
public Properties getProperties() {
return properties;
}
/**
* 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;
}
/**
* Adds the incoming properties to the internap properties structure,
* as long as the internal structure does not already contain an
* entry for the given key.
*
* @param properties
* @return this
*/
public Configuration mergeProperties(Properties properties) {
Iterator itr = properties.entrySet().iterator();
while ( itr.hasNext() ) {
final Map.Entry entry = ( Map.Entry ) itr.next();
if ( this.properties.containsKey( entry.getKey() ) ) {
continue;
}
this.properties.setProperty( ( String ) entry.getKey(), ( String ) entry.getValue() );
}
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 );
return ConfigHelper.getResourceAsStream( resource );
}
/**
* 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-3.0.dtd</tt>.
* <p/>
* 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-3.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-3.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-3.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, entityResolver )
.read( new InputSource( stream ) );
if ( errors.size() != 0 ) {
throw new MappingException(
"invalid configuration",
(Throwable) errors.get( 0 )
);
}
}
catch (DocumentException e) {
throw new HibernateException(
"Could not parse configuration: " + resourceName,
e
);
}
finally {
try {
stream.close();
}
catch (IOException ioe) {
log.warn( "could not close input stream for: " + 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-3.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" );
return doConfigure( xmlHelper.createDOMReader().read( document ) );
}
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();
boolean includeLazy = !"non-lazy".equals( subelement.attributeValue( "include" ) );
setCacheConcurrencyStrategy( className, subelement.attributeValue( "usage" ), region, includeLazy );
}
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 );
}
else if ( "event".equals( subelementName ) ) {
parseEvent( 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 class=\"" +
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" );
setProperty(Environment.JACC_CONTEXTID, contextId);
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 parseEvent(Element element) {
String type = element.attributeValue( "type" );
List listeners = element.elements();
String[] listenerClasses = new String[ listeners.size() ];
for ( int i = 0; i < listeners.size() ; i++ ) {
listenerClasses[i] = ( (Element) listeners.get( i ) ).attributeValue( "class" );
}
log.debug( "Event listeners: " + type + "=" + StringHelper.toString( listenerClasses ) );
setListeners( type, listenerClasses );
}
private void parseListener(Element element) {
String type = element.attributeValue( "type" );
if ( type == null ) throw new MappingException( "No type specified for listener" );
String impl = element.attributeValue( "class" );
log.debug( "Event listener: " + type + "=" + impl );
setListeners( type, new String[]{impl} );
}
public void setListeners(String type, String[] listenerClasses) {
Object[] listeners = (Object[]) Array.newInstance( eventListeners.getListenerClassFor(type), listenerClasses.length );
for ( int i = 0; i < listeners.length ; i++ ) {
try {
listeners[i] = ReflectHelper.classForName( listenerClasses[i] ).newInstance();
}
catch (Exception e) {
throw new MappingException(
"Unable to instantiate specified event (" + type + ") listener class: " + listenerClasses[i],
e
);
}
}
setListeners( type, listeners );
}
public void setListener(String type, Object listener) {
if ( listener == null ) {
setListener( type, null );
}
else {
Object[] listeners = (Object[]) Array.newInstance( eventListeners.getListenerClassFor(type), 1 );
listeners[0] = listener;
setListeners( type, listeners );
}
}
public void setListeners(String type, Object[] listeners) {
if ( "auto-flush".equals( type ) ) {
if ( listeners == null ) {
eventListeners.setAutoFlushEventListeners( new AutoFlushEventListener[]{} );
}
else {
eventListeners.setAutoFlushEventListeners( (AutoFlushEventListener[]) listeners );
}
}
else if ( "merge".equals( type ) ) {
if ( listeners == null ) {
eventListeners.setMergeEventListeners( new MergeEventListener[]{} );
}
else {
eventListeners.setMergeEventListeners( (MergeEventListener[]) listeners );
}
}
else if ( "create".equals( type ) ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -