From eb9a093262970d52333b8a3c81f8dd30d1a3ebb3 Mon Sep 17 00:00:00 2001 From: Emmanuel Bernard Date: Mon, 14 Feb 2011 13:59:24 +0100 Subject: [PATCH] HHH-5916 extract property name to instance logic into helper method --- .../java/org/hibernate/HibernateLogger.java | 2 +- .../org/hibernate/ejb/Ejb3Configuration.java | 124 +++++++++--------- 2 files changed, 64 insertions(+), 62 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/HibernateLogger.java b/hibernate-core/src/main/java/org/hibernate/HibernateLogger.java index cf16553f6c..79daf28b77 100644 --- a/hibernate-core/src/main/java/org/hibernate/HibernateLogger.java +++ b/hibernate-core/src/main/java/org/hibernate/HibernateLogger.java @@ -650,7 +650,7 @@ public interface HibernateLogger extends BasicLogger { void jdbc3GeneratedKeys( String enabledDisabled ); @LogMessage( level = WARN ) - @Message( value = "%s = false break the EJB3 specification", id = 144 ) + @Message( value = "%s = false breaks the EJB3 specification", id = 144 ) void jdbcAutoCommitFalseBreaksEjb3Spec( String autocommit ); @LogMessage( level = INFO ) diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java b/hibernate-entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java index 7ae0496331..080f55fd1a 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java @@ -1014,68 +1014,30 @@ public class Ejb3Configuration implements Serializable, Referenceable { } } } - if ( preparedProperties.containsKey( AvailableSettings.INTERCEPTOR ) - && ( cfg.getInterceptor() == null - || cfg.getInterceptor().equals( defaultInterceptor ) ) ) { - //cfg.setInterceptor has precedence over configuration file - String interceptorName = preparedProperties.getProperty( AvailableSettings.INTERCEPTOR ); - try { - Class interceptor = classForName( interceptorName ); - cfg.setInterceptor( (Interceptor) interceptor.newInstance() ); - } - catch (ClassNotFoundException e) { - throw new PersistenceException( - getExceptionHeader() + "Unable to find interceptor class: " + interceptorName, e - ); - } - catch (IllegalAccessException e) { - throw new PersistenceException( - getExceptionHeader() + "Unable to access interceptor class: " + interceptorName, e - ); - } - catch (InstantiationException e) { - throw new PersistenceException( - getExceptionHeader() + "Unable to instanciate interceptor class: " + interceptorName, e - ); - } - catch (ClassCastException e) { - throw new PersistenceException( - getExceptionHeader() + "Interceptor class does not implement Interceptor interface: " + interceptorName, e - ); - } + final Interceptor interceptor = instantiateCustomClassFromConfiguration( + preparedProperties, + defaultInterceptor, + cfg.getInterceptor(), + AvailableSettings.INTERCEPTOR, + "interceptor", + Interceptor.class + ); + if ( interceptor != null ) { + cfg.setInterceptor( interceptor ); } - if ( preparedProperties.containsKey( AvailableSettings.NAMING_STRATEGY ) - && ( cfg.getNamingStrategy() == null - || cfg.getNamingStrategy().equals( defaultNamingStrategy ) ) ) { - //cfg.setNamingStrategy has precedence over configuration file - String namingStrategyName = preparedProperties.getProperty( AvailableSettings.NAMING_STRATEGY ); - try { - Class namingStragegy = classForName( namingStrategyName ); - cfg.setNamingStrategy( (NamingStrategy) namingStragegy.newInstance() ); - } - catch (ClassNotFoundException e) { - throw new PersistenceException( - getExceptionHeader() + "Unable to find naming strategy class: " + namingStrategyName, e - ); - } - catch (IllegalAccessException e) { - throw new PersistenceException( - getExceptionHeader() + "Unable to access naming strategy class: " + namingStrategyName, e - ); - } - catch (InstantiationException e) { - throw new PersistenceException( - getExceptionHeader() + "Unable to instanciate naming strategy class: " + namingStrategyName, e - ); - } - catch (ClassCastException e) { - throw new PersistenceException( - getExceptionHeader() + "Naming strategyy class does not implement NmaingStrategy interface: " + namingStrategyName, - e - ); - } + final NamingStrategy namingStrategy = instantiateCustomClassFromConfiguration( + preparedProperties, + defaultNamingStrategy, + cfg.getNamingStrategy(), + AvailableSettings.NAMING_STRATEGY, + "naming strategy", + NamingStrategy.class + ); + if ( namingStrategy != null ) { + cfg.setNamingStrategy( namingStrategy ); } + if ( jaccKeys.size() > 0 ) { addSecurity( jaccKeys, preparedProperties, workingVars ); } @@ -1087,11 +1049,51 @@ public class Ejb3Configuration implements Serializable, Referenceable { //some spec compliance checking //TODO centralize that? if (!"true".equalsIgnoreCase(cfg.getProperty(Environment.AUTOCOMMIT))) LOG.jdbcAutoCommitFalseBreaksEjb3Spec(Environment.AUTOCOMMIT); - discardOnClose = preparedProperties.getProperty( AvailableSettings.DISCARD_PC_ON_CLOSE ) - .equals( "true" ); + discardOnClose = preparedProperties.getProperty(AvailableSettings.DISCARD_PC_ON_CLOSE).equals("true"); return this; } + private T instantiateCustomClassFromConfiguration( + Properties preparedProperties, + T defaultObject, + T cfgObject, + String propertyName, + String classDescription, + Class objectClass) { + if ( preparedProperties.containsKey( propertyName ) + && ( cfgObject == null || cfgObject.equals( defaultObject ) ) ) { + //cfg.setXxx has precedence over configuration file + String className = preparedProperties.getProperty( propertyName ); + try { + Class clazz = classForName( className ); + return clazz.newInstance(); + //cfg.setInterceptor( (Interceptor) instance.newInstance() ); + } + catch (ClassNotFoundException e) { + throw new PersistenceException( + getExceptionHeader() + "Unable to find " + classDescription + " class: " + className, e + ); + } + catch (IllegalAccessException e) { + throw new PersistenceException( + getExceptionHeader() + "Unable to access " + classDescription + " class: " + className, e + ); + } + catch (InstantiationException e) { + throw new PersistenceException( + getExceptionHeader() + "Unable to instantiate " + classDescription + " class: " + className, e + ); + } + catch (ClassCastException e) { + throw new PersistenceException( + getExceptionHeader() + classDescription + " class does not implement " + objectClass + " interface: " + + className, e + ); + } + } + return null; + } + @SuppressWarnings({ "unchecked" }) private void addClassesToSessionFactory(Map workingVars) { if ( workingVars.containsKey( AvailableSettings.CLASS_NAMES ) ) {