Search in sources :

Example 1 with HotRodSSOManagerFactoryConfiguration

use of org.wildfly.clustering.web.hotrod.sso.HotRodSSOManagerFactoryConfiguration 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;
        }
    });
}
Also used : ListIterator(java.util.ListIterator) CompositeImmutability(org.wildfly.clustering.ee.immutable.CompositeImmutability) SpringSpecificationProvider(org.wildfly.clustering.web.spring.SpringSpecificationProvider) BiFunction(java.util.function.BiFunction) DistributableSessionRepositoryConfiguration(org.wildfly.clustering.web.spring.DistributableSessionRepositoryConfiguration) IndexResolver(org.springframework.session.IndexResolver) SSOManager(org.wildfly.clustering.web.sso.SSOManager) RemoteCache(org.infinispan.client.hotrod.RemoteCache) TransactionBatch(org.wildfly.clustering.ee.cache.tx.TransactionBatch) SessionManager(org.wildfly.clustering.web.session.SessionManager) SpringSecurityImmutability(org.wildfly.clustering.web.spring.security.SpringSecurityImmutability) HotRodSessionManagerFactory(org.wildfly.clustering.web.hotrod.session.HotRodSessionManagerFactory) Duration(java.time.Duration) Map(java.util.Map) HotRodSSOManagerFactoryConfiguration(org.wildfly.clustering.web.hotrod.sso.HotRodSSOManagerFactoryConfiguration) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) TransactionMode(org.infinispan.client.hotrod.configuration.TransactionMode) LocalContextFactory(org.wildfly.clustering.web.LocalContextFactory) URI(java.net.URI) FindByIndexNameSessionRepository(org.springframework.session.FindByIndexNameSessionRepository) EnumSet(java.util.EnumSet) HttpSession(javax.servlet.http.HttpSession) SpecificationProvider(org.wildfly.clustering.web.session.SpecificationProvider) SessionManagerFactory(org.wildfly.clustering.web.session.SessionManagerFactory) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) RemoteCacheManager(org.wildfly.clustering.infinispan.client.manager.RemoteCacheManager) ServiceLoader(java.util.ServiceLoader) HotRodSSOManagerFactory(org.wildfly.clustering.web.hotrod.sso.HotRodSSOManagerFactory) Registration(org.wildfly.clustering.Registration) List(java.util.List) WildFlySecurityManager(org.wildfly.security.manager.WildFlySecurityManager) HotRodSessionManagerFactoryConfiguration(org.wildfly.clustering.web.hotrod.session.HotRodSessionManagerFactoryConfiguration) DisposableBean(org.springframework.beans.factory.DisposableBean) Optional(java.util.Optional) ImmutableSessionDestroyAction(org.wildfly.clustering.web.spring.ImmutableSessionDestroyAction) SessionManagerConfiguration(org.wildfly.clustering.web.session.SessionManagerConfiguration) SessionManagerNearCacheFactory(org.wildfly.clustering.web.hotrod.session.SessionManagerNearCacheFactory) DefaultImmutability(org.wildfly.clustering.ee.immutable.DefaultImmutability) HotRodURI(org.infinispan.client.hotrod.impl.HotRodURI) SSOManagerConfiguration(org.wildfly.clustering.web.sso.SSOManagerConfiguration) HashMap(java.util.HashMap) ByteBufferMarshaller(org.wildfly.clustering.marshalling.spi.ByteBufferMarshaller) HttpSessionActivationListener(javax.servlet.http.HttpSessionActivationListener) Supplier(java.util.function.Supplier) InitializingBean(org.springframework.beans.factory.InitializingBean) MarshalledValueFactory(org.wildfly.clustering.marshalling.spi.MarshalledValueFactory) Registrar(org.wildfly.clustering.Registrar) Immutability(org.wildfly.clustering.ee.Immutability) BiConsumer(java.util.function.BiConsumer) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) CompositeIterable(org.wildfly.clustering.ee.CompositeIterable) LinkedList(java.util.LinkedList) IndexingConfiguration(org.wildfly.clustering.web.spring.IndexingConfiguration) RemoteCacheContainer(org.wildfly.clustering.infinispan.client.RemoteCacheContainer) ByteBufferMarshalledValueFactory(org.wildfly.clustering.marshalling.spi.ByteBufferMarshalledValueFactory) SessionAttributeImmutability(org.wildfly.clustering.web.session.SessionAttributeImmutability) SessionAttributePersistenceStrategy(org.wildfly.clustering.web.session.SessionAttributePersistenceStrategy) Configuration(org.infinispan.client.hotrod.configuration.Configuration) ConfigurationBuilder(org.infinispan.client.hotrod.configuration.ConfigurationBuilder) Session(org.springframework.session.Session) SessionExpirationListener(org.wildfly.clustering.web.session.SessionExpirationListener) SpringSession(org.wildfly.clustering.web.spring.SpringSession) SimpleClassLoaderMarshaller(org.wildfly.clustering.marshalling.protostream.SimpleClassLoaderMarshaller) ApplicationEvent(org.springframework.context.ApplicationEvent) Recordable(org.wildfly.clustering.ee.Recordable) NearCacheMode(org.infinispan.client.hotrod.configuration.NearCacheMode) ImmutableSessionExpirationListener(org.wildfly.clustering.web.spring.ImmutableSessionExpirationListener) SSOManagerFactory(org.wildfly.clustering.web.sso.SSOManagerFactory) DistributableSessionRepository(org.wildfly.clustering.web.spring.DistributableSessionRepository) ServletContext(javax.servlet.ServletContext) ProtoStreamMarshaller(org.wildfly.clustering.infinispan.marshalling.protostream.ProtoStreamMarshaller) Collections(java.util.Collections) MarshalledValueFactory(org.wildfly.clustering.marshalling.spi.MarshalledValueFactory) ByteBufferMarshalledValueFactory(org.wildfly.clustering.marshalling.spi.ByteBufferMarshalledValueFactory) ConfigurationBuilder(org.infinispan.client.hotrod.configuration.ConfigurationBuilder) HotRodSSOManagerFactory(org.wildfly.clustering.web.hotrod.sso.HotRodSSOManagerFactory) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) DistributableSessionRepositoryConfiguration(org.wildfly.clustering.web.spring.DistributableSessionRepositoryConfiguration) HotRodSSOManagerFactoryConfiguration(org.wildfly.clustering.web.hotrod.sso.HotRodSSOManagerFactoryConfiguration) HotRodSessionManagerFactoryConfiguration(org.wildfly.clustering.web.hotrod.session.HotRodSessionManagerFactoryConfiguration) SessionManagerConfiguration(org.wildfly.clustering.web.session.SessionManagerConfiguration) SSOManagerConfiguration(org.wildfly.clustering.web.sso.SSOManagerConfiguration) IndexingConfiguration(org.wildfly.clustering.web.spring.IndexingConfiguration) Configuration(org.infinispan.client.hotrod.configuration.Configuration) RemoteCacheContainer(org.wildfly.clustering.infinispan.client.RemoteCacheContainer) DistributableSessionRepositoryConfiguration(org.wildfly.clustering.web.spring.DistributableSessionRepositoryConfiguration) SessionExpirationListener(org.wildfly.clustering.web.session.SessionExpirationListener) ImmutableSessionExpirationListener(org.wildfly.clustering.web.spring.ImmutableSessionExpirationListener) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) ByteBufferMarshaller(org.wildfly.clustering.marshalling.spi.ByteBufferMarshaller) ImmutableSessionExpirationListener(org.wildfly.clustering.web.spring.ImmutableSessionExpirationListener) Duration(java.time.Duration) CompositeImmutability(org.wildfly.clustering.ee.immutable.CompositeImmutability) SpringSecurityImmutability(org.wildfly.clustering.web.spring.security.SpringSecurityImmutability) DefaultImmutability(org.wildfly.clustering.ee.immutable.DefaultImmutability) Immutability(org.wildfly.clustering.ee.Immutability) SessionAttributeImmutability(org.wildfly.clustering.web.session.SessionAttributeImmutability) RemoteCacheManager(org.wildfly.clustering.infinispan.client.manager.RemoteCacheManager) SpringSpecificationProvider(org.wildfly.clustering.web.spring.SpringSpecificationProvider) SpecificationProvider(org.wildfly.clustering.web.session.SpecificationProvider) ByteBufferMarshalledValueFactory(org.wildfly.clustering.marshalling.spi.ByteBufferMarshalledValueFactory) HotRodSessionManagerFactory(org.wildfly.clustering.web.hotrod.session.HotRodSessionManagerFactory) RemoteCache(org.infinispan.client.hotrod.RemoteCache) Map(java.util.Map) HashMap(java.util.HashMap) SSOManager(org.wildfly.clustering.web.sso.SSOManager) SessionManagerNearCacheFactory(org.wildfly.clustering.web.hotrod.session.SessionManagerNearCacheFactory) SimpleClassLoaderMarshaller(org.wildfly.clustering.marshalling.protostream.SimpleClassLoaderMarshaller) URI(java.net.URI) HotRodURI(org.infinispan.client.hotrod.impl.HotRodURI) HttpSessionActivationListener(javax.servlet.http.HttpSessionActivationListener) ImmutableSessionDestroyAction(org.wildfly.clustering.web.spring.ImmutableSessionDestroyAction) ProtoStreamMarshaller(org.wildfly.clustering.infinispan.marshalling.protostream.ProtoStreamMarshaller) ServletContext(javax.servlet.ServletContext) Supplier(java.util.function.Supplier) TransactionBatch(org.wildfly.clustering.ee.cache.tx.TransactionBatch) HttpSession(javax.servlet.http.HttpSession) IndexingConfiguration(org.wildfly.clustering.web.spring.IndexingConfiguration) SessionAttributePersistenceStrategy(org.wildfly.clustering.web.session.SessionAttributePersistenceStrategy) LocalContextFactory(org.wildfly.clustering.web.LocalContextFactory) Recordable(org.wildfly.clustering.ee.Recordable) HotRodSSOManagerFactoryConfiguration(org.wildfly.clustering.web.hotrod.sso.HotRodSSOManagerFactoryConfiguration) BiFunction(java.util.function.BiFunction) CompositeImmutability(org.wildfly.clustering.ee.immutable.CompositeImmutability) HttpSession(javax.servlet.http.HttpSession) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) Session(org.springframework.session.Session) SpringSession(org.wildfly.clustering.web.spring.SpringSession)

Aggregations

URI (java.net.URI)1 Duration (java.time.Duration)1 Collections (java.util.Collections)1 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1 Map (java.util.Map)1 Optional (java.util.Optional)1 ServiceLoader (java.util.ServiceLoader)1 BiConsumer (java.util.function.BiConsumer)1 BiFunction (java.util.function.BiFunction)1 Supplier (java.util.function.Supplier)1 ServletContext (javax.servlet.ServletContext)1 HttpSession (javax.servlet.http.HttpSession)1 HttpSessionActivationListener (javax.servlet.http.HttpSessionActivationListener)1 RemoteCache (org.infinispan.client.hotrod.RemoteCache)1 Configuration (org.infinispan.client.hotrod.configuration.Configuration)1 ConfigurationBuilder (org.infinispan.client.hotrod.configuration.ConfigurationBuilder)1