Skip to content

Commit

Permalink
Polish JDBC Bean ClassLoader
Browse files Browse the repository at this point in the history
Issue gh-610
  • Loading branch information
Rob Winch committed Sep 7, 2016
1 parent cbd9699 commit 2052ec8
Showing 1 changed file with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
import org.springframework.session.jdbc.JdbcOperationsSessionRepository;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

/**
Expand Down Expand Up @@ -98,17 +99,37 @@ public JdbcOperationsSessionRepository sessionRepository(
else if (this.conversionService != null) {
sessionRepository.setConversionService(this.conversionService);
}
else if (this.classLoader != null) {
GenericConversionService conversionService = new GenericConversionService();
conversionService.addConverter(Object.class, byte[].class,
new SerializingConverter());
conversionService.addConverter(byte[].class, Object.class,
new DeserializingConverter(this.classLoader));
else if (deserializingConverterSupportsCustomClassLoader()) {
GenericConversionService conversionService = createConversionServiceWithBeanClassLoader();
sessionRepository.setConversionService(conversionService);
}
return sessionRepository;
}

/**
* This must be a separate method because some ClassLoaders load the entire method
* definition even if an if statement guards against it loading. This means that older
* versions of Spring would cause a NoSuchMethodError if this were defined in
* {@link #sessionRepository(JdbcOperations, PlatformTransactionManager)}.
*
* @return the default {@link ConversionService}
*/
private GenericConversionService createConversionServiceWithBeanClassLoader() {
GenericConversionService conversionService = new GenericConversionService();
conversionService.addConverter(Object.class, byte[].class,
new SerializingConverter());
conversionService.addConverter(byte[].class, Object.class,
new DeserializingConverter(this.classLoader));
return conversionService;
}

/* (non-Javadoc)
* @see org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader(java.lang.ClassLoader)
*/
public void setBeanClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}

@Autowired(required = false)
@Qualifier("springSessionLobHandler")
public void setLobHandler(LobHandler lobHandler) {
Expand Down Expand Up @@ -137,14 +158,8 @@ private String getTableName() {
return this.tableName;
}

public void setBeanClassLoader(ClassLoader classLoader) {
try {
DeserializingConverter.class.getConstructor(ClassLoader.class);
}
catch (NoSuchMethodException e) {
return;
}
this.classLoader = classLoader;
private boolean deserializingConverterSupportsCustomClassLoader() {
return ClassUtils.hasConstructor(DeserializingConverter.class, ClassLoader.class);
}

public void setImportMetadata(AnnotationMetadata importMetadata) {
Expand Down

0 comments on commit 2052ec8

Please sign in to comment.