HHH-5916 extract property name to instance logic into helper method

This commit is contained in:
Emmanuel Bernard 2011-02-14 13:59:24 +01:00
parent e18799b036
commit d5d492744f
1 changed files with 62 additions and 59 deletions

View File

@ -1025,67 +1025,29 @@ 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
final Interceptor interceptor = instantiateCustomClassFromConfiguration(
preparedProperties,
defaultInterceptor,
cfg.getInterceptor(),
AvailableSettings.INTERCEPTOR,
"interceptor",
Interceptor.class
);
if ( interceptor != null ) {
cfg.setInterceptor( interceptor );
}
catch (IllegalAccessException e) {
throw new PersistenceException(
getExceptionHeader() + "Unable to access interceptor class: " + interceptorName, e
final NamingStrategy namingStrategy = instantiateCustomClassFromConfiguration(
preparedProperties,
defaultNamingStrategy,
cfg.getNamingStrategy(),
AvailableSettings.NAMING_STRATEGY,
"naming strategy",
NamingStrategy.class
);
if ( namingStrategy != null ) {
cfg.setNamingStrategy( namingStrategy );
}
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
);
}
}
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
);
}
}
if ( jaccKeys.size() > 0 ) {
addSecurity( jaccKeys, preparedProperties, workingVars );
@ -1105,6 +1067,47 @@ public class Ejb3Configuration implements Serializable, Referenceable {
return this;
}
private <T> T instantiateCustomClassFromConfiguration(
Properties preparedProperties,
T defaultObject,
T cfgObject,
String propertyName,
String classDescription,
Class<T> objectClass) {
if ( preparedProperties.containsKey( propertyName )
&& ( cfgObject == null || cfgObject.equals( defaultObject ) ) ) {
//cfg.setXxx has precedence over configuration file
String className = preparedProperties.getProperty( propertyName );
try {
Class<T> clazz = (Class<T>) 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 ) ) {