Skip to content

Commit 0ab781e

Browse files
committed
Consolidate Hazelcast configurations
Issue gh-1584
1 parent 849b353 commit 0ab781e

File tree

21 files changed

+649
-226
lines changed

21 files changed

+649
-226
lines changed

spring-session-hazelcast/hazelcast4/hazelcast4.gradle

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
buildscript {
2+
repositories {
3+
maven { url 'https://repo.spring.io/plugins-release' }
4+
}
5+
dependencies {
6+
classpath 'io.spring.gradle:propdeps-plugin:0.0.10.RELEASE'
7+
}
8+
}
9+
110
plugins {
211
id 'java-library'
312
id 'io.spring.convention.repository'
@@ -6,6 +15,7 @@ plugins {
615
id 'io.spring.convention.checkstyle'
716
id 'io.spring.convention.tests-configuration'
817
id 'io.spring.convention.integration-test'
18+
id 'propdeps'
919
}
1020

1121
configurations {
@@ -21,7 +31,7 @@ artifacts {
2131

2232
dependencies {
2333
compile project(':spring-session-core')
24-
compile "com.hazelcast:hazelcast:4.0.2"
34+
optional "com.hazelcast:hazelcast:4.0.2"
2535
compile "org.springframework:spring-context"
2636
compile "javax.annotation:javax.annotation-api"
2737

spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/config/annotation/web/http/EnableHazelcastHttpSession.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
@Retention(RetentionPolicy.RUNTIME)
6868
@Target(ElementType.TYPE)
6969
@Documented
70-
@Import(EnableHazelcastHttpSessionSelector.class)
70+
@Import(HazelcastHttpSessionConfiguration.class)
7171
@Configuration(proxyBeanMethods = false)
7272
public @interface EnableHazelcastHttpSession {
7373

spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/config/annotation/web/http/EnableHazelcastHttpSessionSelector.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/config/annotation/web/http/Hazelcast4HttpSessionConfiguration.java

Lines changed: 0 additions & 161 deletions
This file was deleted.

spring-session-hazelcast/src/main/java/org/springframework/session/hazelcast/config/annotation/web/http/HazelcastHttpSessionConfiguration.java

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,12 +35,15 @@
3535
import org.springframework.session.MapSession;
3636
import org.springframework.session.SaveMode;
3737
import org.springframework.session.Session;
38+
import org.springframework.session.SessionRepository;
3839
import org.springframework.session.config.SessionRepositoryCustomizer;
3940
import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
41+
import org.springframework.session.hazelcast.Hazelcast4IndexedSessionRepository;
4042
import org.springframework.session.hazelcast.HazelcastFlushMode;
4143
import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository;
4244
import org.springframework.session.hazelcast.config.annotation.SpringSessionHazelcastInstance;
4345
import org.springframework.session.web.http.SessionRepositoryFilter;
46+
import org.springframework.util.ClassUtils;
4447
import org.springframework.util.StringUtils;
4548

4649
/**
@@ -72,23 +75,23 @@ public class HazelcastHttpSessionConfiguration extends SpringHttpSessionConfigur
7275

7376
private List<SessionRepositoryCustomizer<HazelcastIndexedSessionRepository>> sessionRepositoryCustomizers;
7477

78+
private List<SessionRepositoryCustomizer<Hazelcast4IndexedSessionRepository>> hazelcast4SessionRepositoryCustomizers;
79+
80+
private static final boolean hazelcast4;
81+
82+
static {
83+
ClassLoader classLoader = HazelcastHttpSessionConfiguration.class.getClassLoader();
84+
hazelcast4 = ClassUtils.isPresent("com.hazelcast.map.IMap", classLoader);
85+
}
86+
7587
@Bean
76-
public HazelcastIndexedSessionRepository sessionRepository() {
77-
HazelcastIndexedSessionRepository sessionRepository = new HazelcastIndexedSessionRepository(
78-
this.hazelcastInstance);
79-
sessionRepository.setApplicationEventPublisher(this.applicationEventPublisher);
80-
if (this.indexResolver != null) {
81-
sessionRepository.setIndexResolver(this.indexResolver);
88+
public SessionRepository<?> sessionRepository() {
89+
if (hazelcast4) {
90+
return createHazelcast4IndexedSessionRepository();
8291
}
83-
if (StringUtils.hasText(this.sessionMapName)) {
84-
sessionRepository.setSessionMapName(this.sessionMapName);
92+
else {
93+
return createHazelcastIndexedSessionRepository();
8594
}
86-
sessionRepository.setDefaultMaxInactiveInterval(this.maxInactiveIntervalInSeconds);
87-
sessionRepository.setFlushMode(this.flushMode);
88-
sessionRepository.setSaveMode(this.saveMode);
89-
this.sessionRepositoryCustomizers
90-
.forEach((sessionRepositoryCustomizer) -> sessionRepositoryCustomizer.customize(sessionRepository));
91-
return sessionRepository;
9295
}
9396

9497
public void setMaxInactiveIntervalInSeconds(int maxInactiveIntervalInSeconds) {
@@ -139,6 +142,13 @@ public void setSessionRepositoryCustomizer(
139142
this.sessionRepositoryCustomizers = sessionRepositoryCustomizers.orderedStream().collect(Collectors.toList());
140143
}
141144

145+
@Autowired(required = false)
146+
public void setHazelcast4SessionRepositoryCustomizer(
147+
ObjectProvider<SessionRepositoryCustomizer<Hazelcast4IndexedSessionRepository>> sessionRepositoryCustomizers) {
148+
this.hazelcast4SessionRepositoryCustomizers = sessionRepositoryCustomizers.orderedStream()
149+
.collect(Collectors.toList());
150+
}
151+
142152
@Override
143153
@SuppressWarnings("deprecation")
144154
public void setImportMetadata(AnnotationMetadata importMetadata) {
@@ -159,4 +169,40 @@ public void setImportMetadata(AnnotationMetadata importMetadata) {
159169
this.saveMode = attributes.getEnum("saveMode");
160170
}
161171

172+
private HazelcastIndexedSessionRepository createHazelcastIndexedSessionRepository() {
173+
HazelcastIndexedSessionRepository sessionRepository = new HazelcastIndexedSessionRepository(
174+
this.hazelcastInstance);
175+
sessionRepository.setApplicationEventPublisher(this.applicationEventPublisher);
176+
if (this.indexResolver != null) {
177+
sessionRepository.setIndexResolver(this.indexResolver);
178+
}
179+
if (StringUtils.hasText(this.sessionMapName)) {
180+
sessionRepository.setSessionMapName(this.sessionMapName);
181+
}
182+
sessionRepository.setDefaultMaxInactiveInterval(this.maxInactiveIntervalInSeconds);
183+
sessionRepository.setFlushMode(this.flushMode);
184+
sessionRepository.setSaveMode(this.saveMode);
185+
this.sessionRepositoryCustomizers
186+
.forEach((sessionRepositoryCustomizer) -> sessionRepositoryCustomizer.customize(sessionRepository));
187+
return sessionRepository;
188+
}
189+
190+
private Hazelcast4IndexedSessionRepository createHazelcast4IndexedSessionRepository() {
191+
Hazelcast4IndexedSessionRepository sessionRepository = new Hazelcast4IndexedSessionRepository(
192+
this.hazelcastInstance);
193+
sessionRepository.setApplicationEventPublisher(this.applicationEventPublisher);
194+
if (this.indexResolver != null) {
195+
sessionRepository.setIndexResolver(this.indexResolver);
196+
}
197+
if (StringUtils.hasText(this.sessionMapName)) {
198+
sessionRepository.setSessionMapName(this.sessionMapName);
199+
}
200+
sessionRepository.setDefaultMaxInactiveInterval(this.maxInactiveIntervalInSeconds);
201+
sessionRepository.setFlushMode(this.flushMode);
202+
sessionRepository.setSaveMode(this.saveMode);
203+
this.hazelcast4SessionRepositoryCustomizers
204+
.forEach((sessionRepositoryCustomizer) -> sessionRepositoryCustomizer.customize(sessionRepository));
205+
return sessionRepository;
206+
}
207+
162208
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apply plugin: 'io.spring.convention.spring-sample-boot'
2+
3+
dependencies {
4+
compile project(':spring-session-hazelcast')
5+
compile project(':hazelcast4')
6+
compile "org.springframework.boot:spring-boot-starter-web"
7+
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
8+
compile "org.springframework.boot:spring-boot-starter-security"
9+
compile "com.hazelcast:hazelcast:4.0.2"
10+
compile "nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect"
11+
compile "org.webjars:bootstrap"
12+
compile "org.webjars:html5shiv"
13+
compile "org.webjars:webjars-locator-core"
14+
15+
testCompile "org.springframework.boot:spring-boot-starter-test"
16+
testCompile "org.junit.jupiter:junit-jupiter-api"
17+
testRuntime "org.junit.jupiter:junit-jupiter-engine"
18+
integrationTestCompile seleniumDependencies
19+
integrationTestCompile "org.testcontainers:testcontainers"
20+
}

0 commit comments

Comments
 (0)