Search in sources :

Example 11 with ImmutableSession

use of org.wildfly.clustering.web.session.ImmutableSession in project wildfly by wildfly.

the class OOBSessionTestCase method isValid.

@Test
public void isValid() {
    Batcher<Batch> batcher = mock(Batcher.class);
    Batch batch = mock(Batch.class);
    when(this.manager.getBatcher()).thenReturn(batcher);
    when(batcher.createBatch()).thenReturn(batch);
    when(this.manager.readSession(this.id)).thenReturn(null);
    Assert.assertFalse(this.session.isValid());
    verify(batch).close();
    reset(batch);
    ImmutableSession session = mock(ImmutableSession.class);
    when(this.manager.readSession(this.id)).thenReturn(session);
    Assert.assertTrue(this.session.isValid());
    verify(batch).close();
}
Also used : ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) Batch(org.wildfly.clustering.ee.Batch) Test(org.junit.Test)

Example 12 with ImmutableSession

use of org.wildfly.clustering.web.session.ImmutableSession in project wildfly by wildfly.

the class HotRodSessionFactory method expired.

@ClientCacheEntryExpired
public void expired(ClientCacheEntryCustomEvent<byte[]> event) {
    RemoteCache<SessionCreationMetaDataKey, SessionCreationMetaDataEntry<L>> creationMetaDataCache = this.creationMetaDataCache;
    RemoteCache<SessionAccessMetaDataKey, SessionAccessMetaData> accessMetaDataCache = this.accessMetaDataCache;
    ImmutableSessionMetaDataFactory<CompositeSessionMetaDataEntry<L>> metaDataFactory = this.metaDataFactory;
    ImmutableSessionAttributesFactory<V> attributesFactory = this.attributesFactory;
    Remover<String> attributesRemover = this.attributesRemover;
    Collection<SessionExpirationListener> listeners = this.listeners;
    boolean nearCacheEnabled = this.nearCacheEnabled;
    Runnable task = new Runnable() {

        @Override
        public void run() {
            ByteBuffer buffer = ByteBuffer.wrap(event.getEventData());
            byte[] key = new byte[UnsignedNumeric.readUnsignedInt(buffer)];
            buffer.get(key);
            byte[] value = buffer.remaining() > 0 ? new byte[UnsignedNumeric.readUnsignedInt(buffer)] : null;
            if (value != null) {
                buffer.get(value);
            }
            Marshaller marshaller = creationMetaDataCache.getRemoteCacheManager().getConfiguration().marshaller();
            String id = null;
            try {
                SessionCreationMetaDataKey creationKey = (SessionCreationMetaDataKey) marshaller.objectFromByteBuffer(key);
                id = creationKey.getId();
                @SuppressWarnings("unchecked") SessionCreationMetaDataEntry<L> creationEntry = (value != null) ? (SessionCreationMetaDataEntry<L>) marshaller.objectFromByteBuffer(value) : new SessionCreationMetaDataEntry<>(new SimpleSessionCreationMetaData(Instant.EPOCH));
                // Ensure entry is removed from near cache
                if (nearCacheEnabled) {
                    creationMetaDataCache.withFlags(Flag.SKIP_LISTENER_NOTIFICATION).remove(creationKey);
                }
                SessionAccessMetaData accessMetaData = accessMetaDataCache.withFlags(Flag.FORCE_RETURN_VALUE).remove(new SessionAccessMetaDataKey(id));
                if (accessMetaData != null) {
                    V attributesValue = attributesFactory.findValue(id);
                    if (attributesValue != null) {
                        ImmutableSessionMetaData metaData = metaDataFactory.createImmutableSessionMetaData(id, new CompositeSessionMetaDataEntry<>(creationEntry, accessMetaData));
                        ImmutableSessionAttributes attributes = attributesFactory.createImmutableSessionAttributes(id, attributesValue);
                        ImmutableSession session = HotRodSessionFactory.this.createImmutableSession(id, metaData, attributes);
                        Logger.ROOT_LOGGER.tracef("Session %s has expired.", id);
                        for (SessionExpirationListener listener : listeners) {
                            listener.sessionExpired(session);
                        }
                        attributesRemover.remove(id);
                    }
                }
            } catch (IOException | ClassNotFoundException e) {
                Logger.ROOT_LOGGER.failedToExpireSession(e, id);
            }
        }
    };
    this.executor.submit(task);
}
Also used : ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) SessionExpirationListener(org.wildfly.clustering.web.session.SessionExpirationListener) CompositeSessionMetaDataEntry(org.wildfly.clustering.web.cache.session.CompositeSessionMetaDataEntry) Marshaller(org.infinispan.commons.marshall.Marshaller) ImmutableSessionAttributes(org.wildfly.clustering.web.session.ImmutableSessionAttributes) IOException(java.io.IOException) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) ByteBuffer(java.nio.ByteBuffer) SimpleSessionCreationMetaData(org.wildfly.clustering.web.cache.session.SimpleSessionCreationMetaData) SessionAccessMetaData(org.wildfly.clustering.web.cache.session.SessionAccessMetaData) SessionCreationMetaDataEntry(org.wildfly.clustering.web.cache.session.SessionCreationMetaDataEntry) ClientCacheEntryExpired(org.infinispan.client.hotrod.annotation.ClientCacheEntryExpired)

Example 13 with ImmutableSession

use of org.wildfly.clustering.web.session.ImmutableSession in project wildfly by wildfly.

the class HotRodSessionManager method findSession.

@Override
public Session<LC> findSession(String id) {
    Map.Entry<MV, AV> entry = this.factory.findValue(id);
    if (entry == null) {
        Logger.ROOT_LOGGER.tracef("Session %s not found", id);
        return null;
    }
    ImmutableSession session = this.factory.createImmutableSession(id, entry);
    if (session.getMetaData().isExpired()) {
        Logger.ROOT_LOGGER.tracef("Session %s was found, but has expired", id);
        this.expirationListener.sessionExpired(session);
        this.factory.remove(id);
        return null;
    }
    return new ValidSession<>(this.factory.createSession(id, entry, this.context), this.closeTask);
}
Also used : SimpleImmutableSession(org.wildfly.clustering.web.cache.session.SimpleImmutableSession) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) ValidSession(org.wildfly.clustering.web.cache.session.ValidSession) Map(java.util.Map)

Example 14 with ImmutableSession

use of org.wildfly.clustering.web.session.ImmutableSession in project wildfly by wildfly.

the class InfinispanSessionManager method activated.

@CacheEntryActivated
public void activated(CacheEntryActivatedEvent<SessionCreationMetaDataKey, ?> event) {
    if (!event.isPre() && !this.properties.isPersistent()) {
        String id = event.getKey().getValue();
        InfinispanWebLogger.ROOT_LOGGER.tracef("Session %s was activated", id);
        Map.Entry<MV, AV> value = this.factory.findValue(id);
        if (value != null) {
            ImmutableSession session = this.factory.createImmutableSession(id, value);
            this.triggerPostActivationEvents(session);
        }
    }
}
Also used : ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) Map(java.util.Map) CacheEntryActivated(org.infinispan.notifications.cachelistener.annotation.CacheEntryActivated)

Example 15 with ImmutableSession

use of org.wildfly.clustering.web.session.ImmutableSession in project wildfly by wildfly.

the class InfinispanSessionManagerFactory method createSessionManager.

@Override
public <L> SessionManager<L, TransactionBatch> createSessionManager(final SessionManagerConfiguration<L> configuration) {
    final Batcher<TransactionBatch> batcher = new InfinispanBatcher(this.config.getCache());
    final Cache<Key<String>, ?> cache = this.config.getCache();
    final CacheProperties properties = new InfinispanCacheProperties(cache.getCacheConfiguration());
    final IdentifierFactory<String> factory = new AffinityIdentifierFactory<>(configuration.getIdentifierFactory(), cache, this.config.getKeyAffinityServiceFactory());
    final CommandDispatcherFactory dispatcherFactory = this.config.getCommandDispatcherFactory();
    final NodeFactory<Address> nodeFactory = this.config.getNodeFactory();
    final int maxActiveSessions = this.config.getSessionManagerFactoryConfiguration().getMaxActiveSessions();
    InfinispanSessionManagerConfiguration config = new InfinispanSessionManagerConfiguration() {

        @Override
        public SessionExpirationListener getExpirationListener() {
            return configuration.getExpirationListener();
        }

        @Override
        public ServletContext getServletContext() {
            return configuration.getServletContext();
        }

        @Override
        public Cache<Key<String>, ?> getCache() {
            return cache;
        }

        @Override
        public CacheProperties getProperties() {
            return properties;
        }

        @Override
        public IdentifierFactory<String> getIdentifierFactory() {
            return factory;
        }

        @Override
        public Batcher<TransactionBatch> getBatcher() {
            return batcher;
        }

        @Override
        public CommandDispatcherFactory getCommandDispatcherFactory() {
            return dispatcherFactory;
        }

        @Override
        public NodeFactory<Address> getNodeFactory() {
            return nodeFactory;
        }

        @Override
        public int getMaxActiveSessions() {
            return maxActiveSessions;
        }

        @Override
        public Recordable<ImmutableSession> getInactiveSessionRecorder() {
            return configuration.getInactiveSessionRecorder();
        }
    };
    return new InfinispanSessionManager<>(this.createSessionFactory(properties, configuration.getLocalContextFactory()), config);
}
Also used : ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) Address(org.infinispan.remoting.transport.Address) TransactionBatch(org.wildfly.clustering.ee.infinispan.TransactionBatch) InfinispanBatcher(org.wildfly.clustering.ee.infinispan.InfinispanBatcher) InfinispanCacheProperties(org.wildfly.clustering.ee.infinispan.InfinispanCacheProperties) CacheProperties(org.wildfly.clustering.ee.infinispan.CacheProperties) AffinityIdentifierFactory(org.wildfly.clustering.web.infinispan.AffinityIdentifierFactory) CommandDispatcherFactory(org.wildfly.clustering.dispatcher.CommandDispatcherFactory) Key(org.wildfly.clustering.infinispan.spi.distribution.Key) InfinispanCacheProperties(org.wildfly.clustering.ee.infinispan.InfinispanCacheProperties)

Aggregations

ImmutableSession (org.wildfly.clustering.web.session.ImmutableSession)35 Test (org.junit.Test)27 Batch (org.wildfly.clustering.ee.Batch)13 ImmutableSessionMetaData (org.wildfly.clustering.web.session.ImmutableSessionMetaData)13 ServletContext (javax.servlet.ServletContext)12 ImmutableSessionAttributes (org.wildfly.clustering.web.session.ImmutableSessionAttributes)9 Map (java.util.Map)7 Instant (java.time.Instant)5 SessionExpirationListener (org.wildfly.clustering.web.session.SessionExpirationListener)4 SessionMetaData (org.wildfly.clustering.web.session.SessionMetaData)4 Session (io.undertow.server.session.Session)2 Duration (java.time.Duration)2 SimpleImmutableSession (org.wildfly.clustering.web.cache.session.SimpleImmutableSession)2 ValidSession (org.wildfly.clustering.web.cache.session.ValidSession)2 SessionAttributes (org.wildfly.clustering.web.session.SessionAttributes)2 SessionListener (io.undertow.server.session.SessionListener)1 SessionListeners (io.undertow.server.session.SessionListeners)1 Deployment (io.undertow.servlet.api.Deployment)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1