Skip to content

Ensure compatibility with Spring Session module split #9554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package org.springframework.boot.autoconfigure.session;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
Expand All @@ -35,14 +38,23 @@
@EnableSpringHttpSession
@Conditional(SessionCondition.class)
@ConditionalOnMissingBean(SessionRepository.class)
@EnableConfigurationProperties(ServerProperties.class)
class HashMapSessionConfiguration {

private final ServerProperties serverProperties;

HashMapSessionConfiguration(ObjectProvider<ServerProperties> serverProperties) {
this.serverProperties = serverProperties.getIfUnique();
}

@Bean
public MapSessionRepository sessionRepository(SessionProperties properties) {
public MapSessionRepository sessionRepository() {
MapSessionRepository repository = new MapSessionRepository();
Integer timeout = properties.getTimeout();
if (timeout != null) {
repository.setDefaultMaxInactiveInterval(timeout);
if (this.serverProperties != null) {
Integer timeout = this.serverProperties.getSession().getTimeout();
if (timeout != null) {
repository.setDefaultMaxInactiveInterval(timeout);
}
}
return repository;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,14 +16,20 @@

package org.springframework.boot.autoconfigure.session;

import javax.annotation.PostConstruct;

import com.hazelcast.core.HazelcastInstance;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.SessionRepository;
import org.springframework.session.hazelcast.HazelcastSessionRepository;
import org.springframework.session.hazelcast.config.annotation.web.http.HazelcastHttpSessionConfiguration;

/**
Expand All @@ -35,24 +41,39 @@
* @author Vedran Pavic
*/
@Configuration
@ConditionalOnClass(HazelcastSessionRepository.class)
@ConditionalOnMissingBean(SessionRepository.class)
@ConditionalOnBean(HazelcastInstance.class)
@Conditional(SessionCondition.class)
@EnableConfigurationProperties({ ServerProperties.class,
HazelcastSessionProperties.class })
class HazelcastSessionConfiguration {

@Configuration
public static class SpringBootHazelcastHttpSessionConfiguration
extends HazelcastHttpSessionConfiguration {

@Autowired
public void customize(SessionProperties sessionProperties) {
Integer timeout = sessionProperties.getTimeout();
if (timeout != null) {
setMaxInactiveIntervalInSeconds(timeout);
private final HazelcastSessionProperties sessionProperties;

private final ServerProperties serverProperties;

SpringBootHazelcastHttpSessionConfiguration(
HazelcastSessionProperties sessionProperties,
ObjectProvider<ServerProperties> serverProperties) {
this.sessionProperties = sessionProperties;
this.serverProperties = serverProperties.getIfUnique();
}

@PostConstruct
public void init() {
if (this.serverProperties != null) {
Integer timeout = this.serverProperties.getSession().getTimeout();
if (timeout != null) {
setMaxInactiveIntervalInSeconds(timeout);
}
}
SessionProperties.Hazelcast hazelcast = sessionProperties.getHazelcast();
setSessionMapName(hazelcast.getMapName());
setHazelcastFlushMode(hazelcast.getFlushMode());
setSessionMapName(this.sessionProperties.getMapName());
setHazelcastFlushMode(this.sessionProperties.getFlushMode());
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.autoconfigure.session;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.session.hazelcast.HazelcastFlushMode;

/**
* Configuration properties for Hazelcast backed Spring Session.
*
* @author Vedran Pavic
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "spring.session.hazelcast")
public class HazelcastSessionProperties {

/**
* Name of the map used to store sessions.
*/
private String mapName = "spring:session:sessions";

/**
* Sessions flush mode.
*/
private HazelcastFlushMode flushMode = HazelcastFlushMode.ON_SAVE;

public String getMapName() {
return this.mapName;
}

public void setMapName(String mapName) {
this.mapName = mapName;
}

public HazelcastFlushMode getFlushMode() {
return this.flushMode;
}

public void setFlushMode(HazelcastFlushMode flushMode) {
this.flushMode = flushMode;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,18 +16,22 @@

package org.springframework.boot.autoconfigure.session;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.session.SessionRepository;
import org.springframework.session.jdbc.JdbcOperationsSessionRepository;
import org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration;

/**
Expand All @@ -38,31 +42,44 @@
* @author Vedran Pavic
*/
@Configuration
@ConditionalOnClass(JdbcTemplate.class)
@ConditionalOnClass({ JdbcTemplate.class, JdbcOperationsSessionRepository.class })
@ConditionalOnMissingBean(SessionRepository.class)
@ConditionalOnBean(DataSource.class)
@Conditional(SessionCondition.class)
@EnableConfigurationProperties({ ServerProperties.class, JdbcSessionProperties.class })
class JdbcSessionConfiguration {

@Bean
@ConditionalOnMissingBean
public JdbcSessionDatabaseInitializer jdbcSessionDatabaseInitializer(
DataSource dataSource, ResourceLoader resourceLoader,
SessionProperties properties) {
JdbcSessionProperties properties) {
return new JdbcSessionDatabaseInitializer(dataSource, resourceLoader, properties);
}

@Configuration
public static class SpringBootJdbcHttpSessionConfiguration
extends JdbcHttpSessionConfiguration {

@Autowired
public void customize(SessionProperties sessionProperties) {
Integer timeout = sessionProperties.getTimeout();
if (timeout != null) {
setMaxInactiveIntervalInSeconds(timeout);
private final JdbcSessionProperties sessionProperties;

private final ServerProperties serverProperties;

SpringBootJdbcHttpSessionConfiguration(JdbcSessionProperties sessionProperties,
ObjectProvider<ServerProperties> serverProperties) {
this.sessionProperties = sessionProperties;
this.serverProperties = serverProperties.getIfUnique();
}

@PostConstruct
public void init() {
if (this.serverProperties != null) {
Integer timeout = this.serverProperties.getSession().getTimeout();
if (timeout != null) {
setMaxInactiveIntervalInSeconds(timeout);
}
}
setTableName(sessionProperties.getJdbc().getTableName());
setTableName(this.sessionProperties.getTableName());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
*/
public class JdbcSessionDatabaseInitializer extends AbstractDatabaseInitializer {

private final SessionProperties.Jdbc properties;
private final JdbcSessionProperties properties;

public JdbcSessionDatabaseInitializer(DataSource dataSource,
ResourceLoader resourceLoader, SessionProperties properties) {
ResourceLoader resourceLoader, JdbcSessionProperties properties) {
super(dataSource, resourceLoader);
Assert.notNull(properties, "SessionProperties must not be null");
this.properties = properties.getJdbc();
Assert.notNull(properties, "JdbcSessionProperties must not be null");
this.properties = properties;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.autoconfigure.session;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* Configuration properties for JDBC backed Spring Session.
*
* @author Vedran Pavic
* @since 2.0.0
*/
@ConfigurationProperties(prefix = "spring.session.jdbc")
public class JdbcSessionProperties {

private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
+ "session/jdbc/schema-@@platform@@.sql";

private static final String DEFAULT_TABLE_NAME = "SPRING_SESSION";

/**
* Path to the SQL file to use to initialize the database schema.
*/
private String schema = DEFAULT_SCHEMA_LOCATION;

/**
* Name of database table used to store sessions.
*/
private String tableName = DEFAULT_TABLE_NAME;

private final Initializer initializer = new Initializer();

public String getSchema() {
return this.schema;
}

public void setSchema(String schema) {
this.schema = schema;
}

public String getTableName() {
return this.tableName;
}

public void setTableName(String tableName) {
this.tableName = tableName;
}

public Initializer getInitializer() {
return this.initializer;
}

public class Initializer {

/**
* Create the required session tables on startup if necessary. Enabled
* automatically if the default table name is set or a custom schema is
* configured.
*/
private Boolean enabled;

public boolean isEnabled() {
if (this.enabled != null) {
return this.enabled;
}
boolean defaultTableName = DEFAULT_TABLE_NAME.equals(getTableName());
boolean customSchema = !DEFAULT_SCHEMA_LOCATION.equals(getSchema());
return (defaultTableName || customSchema);
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

}

}
Loading