use of org.wildfly.clustering.web.hotrod.session.HotRodSessionManagerFactory in project wildfly-clustering-spring-session by wildfly-clustering.
the class HotRodSessionRepository method afterPropertiesSet.
@Override
public void afterPropertiesSet() throws Exception {
ServletContext context = this.configuration.getServletContext();
// Deployment name = host name + context path + version
String deploymentName = context.getVirtualServerName() + context.getContextPath();
String templateName = this.configuration.getTemplateName();
Integer maxActiveSessions = this.configuration.getMaxActiveSessions();
SessionAttributePersistenceStrategy strategy = this.configuration.getPersistenceStrategy();
ClassLoader containerLoader = WildFlySecurityManager.getClassLoaderPrivileged(HotRodSessionManagerFactory.class);
URI uri = this.configuration.getUri();
Configuration configuration = ((uri != null) ? HotRodURI.create(uri).toConfigurationBuilder() : new ConfigurationBuilder()).withProperties(this.configuration.getProperties()).marshaller(new ProtoStreamMarshaller(new SimpleClassLoaderMarshaller(containerLoader), builder -> builder.load(containerLoader))).classLoader(containerLoader).build();
configuration.addRemoteCache(deploymentName, builder -> builder.forceReturnValues(false).nearCacheMode((maxActiveSessions == null) || (maxActiveSessions.intValue() <= 0) ? NearCacheMode.DISABLED : NearCacheMode.INVALIDATED).transactionMode(TransactionMode.NONE).templateName(templateName));
RemoteCacheContainer container = new RemoteCacheManager(this.getClass().getName(), configuration, this);
container.start();
this.stopTasks.add(container::stop);
ByteBufferMarshaller marshaller = this.configuration.getMarshallerFactory().apply(context.getClassLoader());
MarshalledValueFactory<ByteBufferMarshaller> marshalledValueFactory = new ByteBufferMarshalledValueFactory(marshaller);
ServiceLoader<Immutability> loadedImmutability = ServiceLoader.load(Immutability.class, Immutability.class.getClassLoader());
Immutability immutability = new CompositeImmutability(new CompositeIterable<>(EnumSet.allOf(DefaultImmutability.class), EnumSet.allOf(SessionAttributeImmutability.class), EnumSet.allOf(SpringSecurityImmutability.class), loadedImmutability));
SessionManagerFactory<ServletContext, Void, TransactionBatch> managerFactory = new HotRodSessionManagerFactory<>(new HotRodSessionManagerFactoryConfiguration<HttpSession, ServletContext, HttpSessionActivationListener, ByteBufferMarshaller, Void>() {
@Override
public Integer getMaxActiveSessions() {
return maxActiveSessions;
}
@Override
public SessionAttributePersistenceStrategy getAttributePersistenceStrategy() {
return strategy;
}
@Override
public String getDeploymentName() {
return deploymentName;
}
@Override
public MarshalledValueFactory<ByteBufferMarshaller> getMarshalledValueFactory() {
return marshalledValueFactory;
}
@Override
public String getServerName() {
return context.getVirtualServerName();
}
@Override
public LocalContextFactory<Void> getLocalContextFactory() {
return HotRodSessionRepository.this;
}
@Override
public <K, V> RemoteCache<K, V> getCache() {
String cacheName = this.getDeploymentName();
try (RemoteCacheContainer.NearCacheRegistration registration = container.registerNearCacheFactory(cacheName, new SessionManagerNearCacheFactory<>(this.getMaxActiveSessions(), this.getAttributePersistenceStrategy()))) {
return container.getCache(cacheName);
}
}
@Override
public Immutability getImmutability() {
return immutability;
}
@Override
public SpecificationProvider<HttpSession, ServletContext, HttpSessionActivationListener> getSpecificationProvider() {
return SpringSpecificationProvider.INSTANCE;
}
});
this.stopTasks.add(managerFactory::close);
Supplier<String> identifierFactory = this.configuration.getIdentifierFactory();
Map<String, String> indexes = this.configuration.getIndexes();
Map<String, SSOManager<Void, String, String, Void, TransactionBatch>> managers = indexes.isEmpty() ? Collections.emptyMap() : new HashMap<>();
for (Map.Entry<String, String> entry : indexes.entrySet()) {
String cacheName = String.format("%s/%s", deploymentName, entry.getKey());
String indexName = entry.getValue();
configuration.addRemoteCache(cacheName, builder -> builder.forceReturnValues(false).nearCacheMode(NearCacheMode.DISABLED).transactionMode(TransactionMode.NONE).templateName(templateName));
SSOManagerFactory<Void, String, String, TransactionBatch> ssoManagerFactory = new HotRodSSOManagerFactory<>(new HotRodSSOManagerFactoryConfiguration() {
@Override
public <K, V> RemoteCache<K, V> getRemoteCache() {
return container.getCache(cacheName);
}
});
SSOManager<Void, String, String, Void, TransactionBatch> ssoManager = ssoManagerFactory.createSSOManager(new SSOManagerConfiguration<ByteBufferMarshaller, Void>() {
@Override
public Supplier<String> getIdentifierFactory() {
return identifierFactory;
}
@Override
public MarshalledValueFactory<ByteBufferMarshaller> getMarshalledValueFactory() {
return marshalledValueFactory;
}
@Override
public LocalContextFactory<Void> getLocalContextFactory() {
return HotRodSessionRepository.this;
}
});
managers.put(indexName, ssoManager);
}
IndexResolver<Session> resolver = this.configuration.getIndexResolver();
IndexingConfiguration<TransactionBatch> indexing = new IndexingConfiguration<TransactionBatch>() {
@Override
public Map<String, SSOManager<Void, String, String, Void, TransactionBatch>> getSSOManagers() {
return managers;
}
@Override
public IndexResolver<Session> getIndexResolver() {
return resolver;
}
};
ApplicationEventPublisher publisher = this.configuration.getEventPublisher();
BiConsumer<ImmutableSession, BiFunction<Object, Session, ApplicationEvent>> sessionDestroyAction = new ImmutableSessionDestroyAction<>(publisher, context, indexing);
SessionExpirationListener expirationListener = new ImmutableSessionExpirationListener(context, sessionDestroyAction);
SessionManager<Void, TransactionBatch> manager = managerFactory.createSessionManager(new SessionManagerConfiguration<ServletContext>() {
@Override
public ServletContext getServletContext() {
return context;
}
@Override
public Supplier<String> getIdentifierFactory() {
return identifierFactory;
}
@Override
public SessionExpirationListener getExpirationListener() {
return expirationListener;
}
@Override
public Recordable<ImmutableSessionMetaData> getInactiveSessionRecorder() {
// Spring session has no metrics capability
return null;
}
});
Optional<Duration> defaultTimeout = setDefaultMaxInactiveInterval(manager, Duration.ofMinutes(context.getSessionTimeout()));
manager.start();
this.stopTasks.add(manager::stop);
this.repository = new DistributableSessionRepository<>(new DistributableSessionRepositoryConfiguration<TransactionBatch>() {
@Override
public SessionManager<Void, TransactionBatch> getSessionManager() {
return manager;
}
@Override
public Optional<Duration> getDefaultTimeout() {
return defaultTimeout;
}
@Override
public ApplicationEventPublisher getEventPublisher() {
return publisher;
}
@Override
public ServletContext getServletContext() {
return context;
}
@Override
public BiConsumer<ImmutableSession, BiFunction<Object, Session, ApplicationEvent>> getSessionDestroyAction() {
return sessionDestroyAction;
}
@Override
public IndexingConfiguration<TransactionBatch> getIndexingConfiguration() {
return indexing;
}
});
}
Aggregations