diff --git a/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/support/FixedDurationExpirationSessionRepository.java b/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/support/FixedDurationExpirationSessionRepository.java new file mode 100644 index 0000000..bf8c662 --- /dev/null +++ b/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/support/FixedDurationExpirationSessionRepository.java @@ -0,0 +1,99 @@ +/* + * Copyright 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.session.data.gemfire.support; + +import java.time.Duration; +import java.time.Instant; +import java.util.Optional; + +import org.springframework.session.Session; +import org.springframework.session.SessionRepository; + +/** + * The FixedDurationExpirationSessionRepository class... + * + * @author John Blum + * @since 1.0.0 + */ +@SuppressWarnings("unused") +public class FixedDurationExpirationSessionRepository implements SessionRepository { + + private final SessionRepository delegate; + + private final Duration expirationDuration; + + public FixedDurationExpirationSessionRepository(SessionRepository sessionRepository, Duration expirationDuration) { + + this.delegate = Optional.ofNullable(sessionRepository) + .orElseThrow(() -> new IllegalArgumentException("SessionRepository is required")); + + this.expirationDuration = expirationDuration; + } + + protected SessionRepository getDelegate() { + return this.delegate; + } + + public Optional getExpirationDuration() { + return Optional.ofNullable(this.expirationDuration); + } + + @Override + public S createSession() { + return getDelegate().createSession(); + } + + @Override + @SuppressWarnings("unchecked") + public S findById(String id) { + + return Optional.ofNullable(getDelegate().findById(id)) + .map(session -> + getExpirationDuration() + .map(expirationDuration -> handleExpired(session, expirationDuration)) + .orElse(session) + ).orElse(null); + } + + private S handleExpired(S session, Duration expirationDuration) { + + if (isExpired(session, expirationDuration)) { + deleteById(session.getId()); + session = null; + } + + return session; + } + + private boolean isExpired(S session, Duration expirationDuration) { + + Instant sessionCreationTime = session.getCreationTime(); + Instant now = Instant.now(); + + return now.minusMillis(expirationDuration.toMillis()).isAfter(sessionCreationTime); + } + + @Override + public void save(S session) { + getDelegate().save(session); + } + + @Override + public void deleteById(String id) { + getDelegate().deleteById(id); + } +} diff --git a/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/support/FixedDurationExpirationSessionRepositoryBeanPostProcessor.java b/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/support/FixedDurationExpirationSessionRepositoryBeanPostProcessor.java new file mode 100644 index 0000000..45ae188 --- /dev/null +++ b/spring-session-data-geode/src/main/java/org/springframework/session/data/gemfire/support/FixedDurationExpirationSessionRepositoryBeanPostProcessor.java @@ -0,0 +1,49 @@ +/* + * Copyright 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.session.data.gemfire.support; + +import java.time.Duration; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.lang.Nullable; +import org.springframework.session.SessionRepository; + +/** + * The FixedDurationExpirationSessionRepositoryBeanPostProcessor class... + * + * @author John Blum + * @since 1.0.0 + */ +@SuppressWarnings("unused") +public class FixedDurationExpirationSessionRepositoryBeanPostProcessor implements BeanPostProcessor { + + private final Duration expirationDuration; + + public FixedDurationExpirationSessionRepositoryBeanPostProcessor(Duration expirationDuration) { + this.expirationDuration = expirationDuration; + } + + @Nullable @Override @SuppressWarnings("unchecked") + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + + return (bean instanceof SessionRepository + ? new FixedDurationExpirationSessionRepository<>((SessionRepository) bean, this.expirationDuration) + : bean); + + } +}