Search in sources :

Example 11 with BatchContext

use of org.wildfly.clustering.ee.BatchContext in project wildfly by wildfly.

the class DistributableSession method getAttribute.

@Override
public Object getAttribute(String name) {
    Session<LocalSessionContext> session = this.entry.getKey();
    validate(session);
    try (BatchContext context = this.manager.getSessionManager().getBatcher().resumeBatch(this.batch)) {
        if (CachedAuthenticatedSessionHandler.ATTRIBUTE_NAME.equals(name)) {
            AuthenticatedSession auth = (AuthenticatedSession) session.getAttributes().getAttribute(name);
            return (auth != null) ? auth : session.getLocalContext().getAuthenticatedSession();
        }
        return session.getAttributes().getAttribute(name);
    }
}
Also used : AuthenticatedSession(io.undertow.security.api.AuthenticatedSessionManager.AuthenticatedSession) BatchContext(org.wildfly.clustering.ee.BatchContext)

Example 12 with BatchContext

use of org.wildfly.clustering.ee.BatchContext in project wildfly by wildfly.

the class DistributableSessionManagerFactory method createSessionManager.

@Override
public io.undertow.server.session.SessionManager createSessionManager(final Deployment deployment) {
    DeploymentInfo info = deployment.getDeploymentInfo();
    boolean statisticsEnabled = info.getMetricsCollector() != null;
    RecordableInactiveSessionStatistics inactiveSessionStatistics = statisticsEnabled ? new RecordableInactiveSessionStatistics() : null;
    IdentifierFactory<String> factory = new IdentifierFactoryAdapter(info.getSessionIdGenerator());
    LocalContextFactory<LocalSessionContext> localContextFactory = new LocalSessionContextFactory();
    SessionListeners listeners = new SessionListeners();
    SessionExpirationListener expirationListener = new UndertowSessionExpirationListener(deployment, listeners);
    SessionManagerConfiguration<LocalSessionContext> configuration = new SessionManagerConfiguration<LocalSessionContext>() {

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

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

        @Override
        public SessionExpirationListener getExpirationListener() {
            return expirationListener;
        }

        @Override
        public LocalContextFactory<LocalSessionContext> getLocalContextFactory() {
            return localContextFactory;
        }

        @Override
        public Recordable<ImmutableSession> getInactiveSessionRecorder() {
            return inactiveSessionStatistics;
        }
    };
    SessionManager<LocalSessionContext, Batch> manager = this.factory.createSessionManager(configuration);
    Batcher<Batch> batcher = manager.getBatcher();
    info.addThreadSetupAction(new ThreadSetupHandler() {

        @Override
        public <T, C> Action<T, C> create(Action<T, C> action) {
            return new Action<T, C>() {

                @Override
                public T call(HttpServerExchange exchange, C context) throws Exception {
                    Batch batch = batcher.suspendBatch();
                    try (BatchContext ctx = batcher.resumeBatch(batch)) {
                        return action.call(exchange, context);
                    }
                }
            };
        }
    });
    RecordableSessionManagerStatistics statistics = (inactiveSessionStatistics != null) ? new DistributableSessionManagerStatistics(manager, inactiveSessionStatistics) : null;
    return new DistributableSessionManager(info.getDeploymentName(), manager, listeners, statistics);
}
Also used : ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) SessionManagerConfiguration(org.wildfly.clustering.web.session.SessionManagerConfiguration) HttpServerExchange(io.undertow.server.HttpServerExchange) ThreadSetupHandler(io.undertow.servlet.api.ThreadSetupHandler) SessionExpirationListener(org.wildfly.clustering.web.session.SessionExpirationListener) SessionListeners(io.undertow.server.session.SessionListeners) Batch(org.wildfly.clustering.ee.Batch) IdentifierFactoryAdapter(org.wildfly.clustering.web.undertow.IdentifierFactoryAdapter) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) BatchContext(org.wildfly.clustering.ee.BatchContext)

Example 13 with BatchContext

use of org.wildfly.clustering.ee.BatchContext in project wildfly by wildfly.

the class DistributableSessionTestCase method setMaxInactiveInterval.

@Test
public void setMaxInactiveInterval() {
    int interval = 3600;
    this.validate(session -> session.setMaxInactiveInterval(interval));
    SessionManager<LocalSessionContext, Batch> manager = mock(SessionManager.class);
    Batcher<Batch> batcher = mock(Batcher.class);
    BatchContext context = mock(BatchContext.class);
    SessionMetaData metaData = mock(SessionMetaData.class);
    when(this.manager.getSessionManager()).thenReturn(manager);
    when(manager.getBatcher()).thenReturn(batcher);
    when(batcher.resumeBatch(this.batch)).thenReturn(context);
    when(this.session.getMetaData()).thenReturn(metaData);
    this.adapter.setMaxInactiveInterval(interval);
    verify(metaData).setMaxInactiveInterval(Duration.ofSeconds(interval));
    verify(context).close();
}
Also used : Batch(org.wildfly.clustering.ee.Batch) SessionMetaData(org.wildfly.clustering.web.session.SessionMetaData) BatchContext(org.wildfly.clustering.ee.BatchContext) Test(org.junit.Test)

Example 14 with BatchContext

use of org.wildfly.clustering.ee.BatchContext in project wildfly by wildfly.

the class DistributableSessionTestCase method setNullAttribute.

@Test
public void setNullAttribute() {
    String name = "name";
    Object value = null;
    this.validate(session -> session.setAttribute(name, value));
    SessionManager<LocalSessionContext, Batch> manager = mock(SessionManager.class);
    Batcher<Batch> batcher = mock(Batcher.class);
    BatchContext context = mock(BatchContext.class);
    SessionAttributes attributes = mock(SessionAttributes.class);
    SessionListener listener = mock(SessionListener.class);
    SessionListeners listeners = new SessionListeners();
    listeners.addSessionListener(listener);
    Object expected = new Object();
    when(this.session.getAttributes()).thenReturn(attributes);
    when(attributes.removeAttribute(name)).thenReturn(expected);
    when(this.manager.getSessionListeners()).thenReturn(listeners);
    when(this.manager.getSessionManager()).thenReturn(manager);
    when(manager.getBatcher()).thenReturn(batcher);
    when(batcher.resumeBatch(this.batch)).thenReturn(context);
    Object result = this.adapter.setAttribute(name, value);
    assertSame(expected, result);
    verify(listener, never()).attributeAdded(this.adapter, name, value);
    verify(listener, never()).attributeUpdated(same(this.adapter), same(name), same(value), any());
    verify(listener).attributeRemoved(this.adapter, name, expected);
    verify(context).close();
}
Also used : Batch(org.wildfly.clustering.ee.Batch) SessionListeners(io.undertow.server.session.SessionListeners) SessionAttributes(org.wildfly.clustering.web.session.SessionAttributes) BatchContext(org.wildfly.clustering.ee.BatchContext) SessionListener(io.undertow.server.session.SessionListener) Test(org.junit.Test)

Example 15 with BatchContext

use of org.wildfly.clustering.ee.BatchContext in project wildfly by wildfly.

the class DistributableSessionTestCase method setNewAttribute.

@Test
public void setNewAttribute() {
    String name = "name";
    Integer value = Integer.valueOf(1);
    this.validate(session -> session.setAttribute(name, value));
    SessionManager<LocalSessionContext, Batch> manager = mock(SessionManager.class);
    Batcher<Batch> batcher = mock(Batcher.class);
    BatchContext context = mock(BatchContext.class);
    SessionAttributes attributes = mock(SessionAttributes.class);
    SessionListener listener = mock(SessionListener.class);
    SessionListeners listeners = new SessionListeners();
    listeners.addSessionListener(listener);
    Object expected = null;
    when(this.session.getAttributes()).thenReturn(attributes);
    when(attributes.setAttribute(name, value)).thenReturn(expected);
    when(this.manager.getSessionListeners()).thenReturn(listeners);
    when(this.manager.getSessionManager()).thenReturn(manager);
    when(manager.getBatcher()).thenReturn(batcher);
    when(batcher.resumeBatch(this.batch)).thenReturn(context);
    Object result = this.adapter.setAttribute(name, value);
    assertSame(expected, result);
    verify(listener).attributeAdded(this.adapter, name, value);
    verify(listener, never()).attributeUpdated(same(this.adapter), same(name), same(value), any());
    verify(listener, never()).attributeRemoved(same(this.adapter), same(name), any());
    verify(context).close();
}
Also used : Batch(org.wildfly.clustering.ee.Batch) SessionListeners(io.undertow.server.session.SessionListeners) SessionAttributes(org.wildfly.clustering.web.session.SessionAttributes) BatchContext(org.wildfly.clustering.ee.BatchContext) SessionListener(io.undertow.server.session.SessionListener) Test(org.junit.Test)

Aggregations

BatchContext (org.wildfly.clustering.ee.BatchContext)42 Test (org.junit.Test)30 Batch (org.wildfly.clustering.ee.Batch)19 AuthenticatedSession (io.undertow.security.api.AuthenticatedSessionManager.AuthenticatedSession)15 SessionAttributes (org.wildfly.clustering.web.session.SessionAttributes)11 SessionListeners (io.undertow.server.session.SessionListeners)9 SessionListener (io.undertow.server.session.SessionListener)8 Session (io.undertow.server.session.Session)6 SessionManager (io.undertow.server.session.SessionManager)6 HttpServerExchange (io.undertow.server.HttpServerExchange)5 SessionMetaData (org.wildfly.clustering.web.session.SessionMetaData)5 Account (io.undertow.security.idm.Account)4 Instant (java.time.Instant)3 SessionConfig (io.undertow.server.session.SessionConfig)2 CachedAuthenticatedSessionHandler (io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler)2 Transaction (javax.transaction.Transaction)2 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)1 ThreadSetupHandler (io.undertow.servlet.api.ThreadSetupHandler)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1