Search in sources :

Example 1 with ClientSession

use of com.canoo.platform.server.client.ClientSession in project dolphin-platform by canoo.

the class DefaultDolphinEventBusTest method create.

private DefaultDolphinEventBus create(final DolphinContext context) {
    DefaultDolphinEventBus eventBus = new DefaultDolphinEventBus();
    eventBus.init(new DolphinContextProvider() {

        @Override
        public DolphinContext getContext(ClientSession clientSession) {
            return getContextById(clientSession.getId());
        }

        @Override
        public DolphinContext getContextById(String clientSessionId) {
            if (context != null && context.getId().equals(clientSessionId)) {
                return context;
            }
            return null;
        }

        @Override
        public DolphinContext getCurrentDolphinContext() {
            return context;
        }
    }, new ClientSessionLifecycleHandlerImpl());
    return eventBus;
}
Also used : DolphinContext(com.canoo.dp.impl.server.context.DolphinContext) ClientSessionLifecycleHandlerImpl(com.canoo.dp.impl.server.client.ClientSessionLifecycleHandlerImpl) DolphinContextProvider(com.canoo.dp.impl.server.context.DolphinContextProvider) ClientSession(com.canoo.platform.server.client.ClientSession)

Example 2 with ClientSession

use of com.canoo.platform.server.client.ClientSession in project dolphin-platform by canoo.

the class DolphinContextTaskQueue method executeTasks.

public void executeTasks() {
    final ClientSession currentSession = sessionProvider.getCurrentClientSession();
    if (currentSession == null || !dolphinSessionId.equals(currentSession.getId())) {
        throw new IllegalStateException("Not in Dolphin Platform session " + dolphinSessionId);
    }
    LOG.trace("Running {} tasks in Dolphin Platform session {}", tasks.size(), dolphinSessionId);
    final long startTime = System.currentTimeMillis();
    final long endTime = startTime + maxExecutionTimeUnit.toMillis(maxExecutionTime);
    while (!communicationManager.hasResponseCommands()) {
        if (interrupted.get()) {
            interrupted.set(false);
            break;
        }
        final Runnable task = tasks.poll();
        if (task == null) {
            try {
                taskLock.lock();
                try {
                    if (tasks.isEmpty() && !taskCondition.await(endTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS)) {
                        interrupted.set(false);
                        break;
                    }
                } finally {
                    taskLock.unlock();
                }
                if (tasks.isEmpty()) {
                    break;
                }
            } catch (InterruptedException e) {
                String exceptionMessage = String.format("Concurrency error in task executor for Dolphin Platform session %s", dolphinSessionId);
                LOG.error(exceptionMessage, e);
                throw new IllegalStateException(exceptionMessage, e);
            }
        } else {
            try {
                task.run();
                LOG.trace("Task executor executed task in Dolphin Platform session {}", dolphinSessionId);
            } catch (Exception e) {
                String exceptionMessage = String.format("Error in running task in Dolphin Platform session %s", dolphinSessionId);
                LOG.error(exceptionMessage, e);
                throw new DolphinTaskException(exceptionMessage, e);
            }
        }
    }
    final long runTime = System.currentTimeMillis() - startTime;
    LOG.trace("Task executor for Dolphin Platform session {} ended after {} seconds with {} task still open", dolphinSessionId, maxExecutionTimeUnit.toSeconds(runTime), tasks.size());
}
Also used : ClientSession(com.canoo.platform.server.client.ClientSession)

Example 3 with ClientSession

use of com.canoo.platform.server.client.ClientSession in project dolphin-platform by canoo.

the class RemotingModule method initialize.

@Override
public void initialize(ServerCoreComponents coreComponents) throws ModuleInitializationException {
    LOG.info("Starting Dolphin Platform");
    try {
        final ServletContext servletContext = coreComponents.getInstance(ServletContext.class);
        final ClasspathScanner classpathScanner = coreComponents.getInstance(ClasspathScanner.class);
        final ManagedBeanFactory beanFactory = coreComponents.getInstance(ManagedBeanFactory.class);
        final RemotingConfiguration configuration = new RemotingConfiguration(coreComponents.getConfiguration());
        final ClientSessionProvider sessionProvider = coreComponents.getInstance(ClientSessionProvider.class);
        final DolphinContextFactory dolphinContextFactory = new DefaultDolphinContextFactory(configuration, sessionProvider, beanFactory, classpathScanner);
        final DolphinContextCommunicationHandler communicationHandler = new DolphinContextCommunicationHandler(sessionProvider, dolphinContextFactory);
        final DolphinContextProvider contextProvider = new DolphinContextProvider() {

            @Override
            public DolphinContext getContext(final ClientSession clientSession) {
                return communicationHandler.getContext(clientSession);
            }

            @Override
            public DolphinContext getContextById(String clientSessionId) {
                return communicationHandler.getContextById(clientSessionId);
            }

            @Override
            public DolphinContext getCurrentDolphinContext() {
                return communicationHandler.getCurrentDolphinContext();
            }
        };
        coreComponents.provideInstance(DolphinContextProvider.class, contextProvider);
        final ClientSessionLifecycleHandler lifecycleHandler = coreComponents.getInstance(ClientSessionLifecycleHandler.class);
        servletContext.addServlet(DOLPHIN_SERVLET_NAME, new DolphinPlatformServlet(communicationHandler)).addMapping(configuration.getDolphinPlatformServletMapping());
        servletContext.addServlet(INTERRUPT_SERVLET_NAME, new InterruptServlet(contextProvider)).addMapping(configuration.getDolphinPlatformInterruptServletMapping());
        LOG.debug("Dolphin Platform initialized under context \"" + servletContext.getContextPath() + "\"");
        LOG.debug("Dolphin Platform endpoint defined as " + configuration.getDolphinPlatformServletMapping());
        Iterator<EventBusProvider> iterator = ServiceLoader.load(EventBusProvider.class).iterator();
        boolean providerFound = false;
        boolean flag = false;
        while (iterator.hasNext()) {
            EventBusProvider provider = iterator.next();
            if (configuration.getEventbusType().equals(provider.getType())) {
                if (providerFound) {
                    throw new IllegalStateException("More than 1 event bus provider found");
                }
                LOG.debug("Using event bus of type {} with provider class {}", provider.getType(), provider.getClass());
                providerFound = true;
                RemotingEventBus eventBus = provider.create(configuration);
                if (eventBus instanceof AbstractEventBus) {
                    ((AbstractEventBus) eventBus).init(contextProvider, lifecycleHandler);
                }
                coreComponents.provideInstance(RemotingEventBus.class, eventBus);
                flag = true;
            }
        }
        if (!flag) {
            throw new ModuleInitializationException("Configured event bus is not on the classpath.");
        }
    } catch (ControllerValidationException cve) {
        throw new ModuleInitializationException("Can not start Remote Presentation Model support based on bad controller definition", cve);
    }
}
Also used : DolphinContextCommunicationHandler(com.canoo.dp.impl.server.context.DolphinContextCommunicationHandler) InterruptServlet(com.canoo.dp.impl.server.servlet.InterruptServlet) AbstractEventBus(com.canoo.dp.impl.server.event.AbstractEventBus) ModuleInitializationException(com.canoo.platform.server.spi.ModuleInitializationException) DolphinPlatformServlet(com.canoo.dp.impl.server.servlet.DolphinPlatformServlet) ClasspathScanner(com.canoo.platform.server.spi.components.ClasspathScanner) RemotingEventBus(com.canoo.platform.remoting.server.event.RemotingEventBus) ClientSessionProvider(com.canoo.dp.impl.server.client.ClientSessionProvider) DolphinContextFactory(com.canoo.dp.impl.server.context.DolphinContextFactory) DefaultDolphinContextFactory(com.canoo.dp.impl.server.context.DefaultDolphinContextFactory) DefaultDolphinContextFactory(com.canoo.dp.impl.server.context.DefaultDolphinContextFactory) DolphinContextProvider(com.canoo.dp.impl.server.context.DolphinContextProvider) RemotingConfiguration(com.canoo.dp.impl.server.config.RemotingConfiguration) EventBusProvider(com.canoo.platform.remoting.server.event.spi.EventBusProvider) ClientSession(com.canoo.platform.server.client.ClientSession) ManagedBeanFactory(com.canoo.platform.server.spi.components.ManagedBeanFactory) ServletContext(javax.servlet.ServletContext) ControllerValidationException(com.canoo.dp.impl.server.controller.ControllerValidationException) ClientSessionLifecycleHandler(com.canoo.dp.impl.server.client.ClientSessionLifecycleHandler)

Example 4 with ClientSession

use of com.canoo.platform.server.client.ClientSession in project dolphin-platform by canoo.

the class ClientSessionImplTest method testImmutableAttributeSet.

@Test(expectedExceptions = UnsupportedOperationException.class)
public void testImmutableAttributeSet() {
    // given:
    ClientSession dolphinSession = new HttpClientSessionImpl(new HttpSessionMock());
    // then:
    dolphinSession.getAttributeNames().add("att");
}
Also used : HttpSessionMock(com.canoo.impl.server.util.HttpSessionMock) ClientSession(com.canoo.platform.server.client.ClientSession) HttpClientSessionImpl(com.canoo.dp.impl.server.client.HttpClientSessionImpl) Test(org.testng.annotations.Test)

Example 5 with ClientSession

use of com.canoo.platform.server.client.ClientSession in project dolphin-platform by canoo.

the class ClientSessionImplTest method testMultipleAttributes.

@Test
public void testMultipleAttributes() {
    // given:
    ClientSession dolphinSession = new HttpClientSessionImpl(new HttpSessionMock());
    // when:
    dolphinSession.setAttribute("test-attribute1", "Hello Dolphin Session");
    dolphinSession.setAttribute("test-attribute2", "Yeah!");
    dolphinSession.setAttribute("test-attribute3", "Dolphin Platform");
    // then:
    Assert.assertEquals(3, dolphinSession.getAttributeNames().size());
    Assert.assertTrue(dolphinSession.getAttributeNames().contains("test-attribute1"));
    Assert.assertTrue(dolphinSession.getAttributeNames().contains("test-attribute2"));
    Assert.assertTrue(dolphinSession.getAttributeNames().contains("test-attribute3"));
    Assert.assertEquals("Hello Dolphin Session", dolphinSession.getAttribute("test-attribute1"));
    Assert.assertEquals("Yeah!", dolphinSession.getAttribute("test-attribute2"));
    Assert.assertEquals("Dolphin Platform", dolphinSession.getAttribute("test-attribute3"));
}
Also used : HttpSessionMock(com.canoo.impl.server.util.HttpSessionMock) ClientSession(com.canoo.platform.server.client.ClientSession) HttpClientSessionImpl(com.canoo.dp.impl.server.client.HttpClientSessionImpl) Test(org.testng.annotations.Test)

Aggregations

ClientSession (com.canoo.platform.server.client.ClientSession)18 HttpClientSessionImpl (com.canoo.dp.impl.server.client.HttpClientSessionImpl)7 HttpSessionMock (com.canoo.impl.server.util.HttpSessionMock)7 Test (org.testng.annotations.Test)7 ClientSessionLifecycleHandlerImpl (com.canoo.dp.impl.server.client.ClientSessionLifecycleHandlerImpl)3 DolphinContextProvider (com.canoo.dp.impl.server.context.DolphinContextProvider)3 ClientSessionProvider (com.canoo.dp.impl.server.client.ClientSessionProvider)2 DolphinContext (com.canoo.dp.impl.server.context.DolphinContext)2 ClasspathScanner (com.canoo.platform.server.spi.components.ClasspathScanner)2 ManagedBeanFactory (com.canoo.platform.server.spi.components.ManagedBeanFactory)2 ServletContext (javax.servlet.ServletContext)2 HttpSession (javax.servlet.http.HttpSession)2 CreateContextCommand (com.canoo.dp.impl.remoting.commands.CreateContextCommand)1 Command (com.canoo.dp.impl.remoting.legacy.communication.Command)1 ClientSessionFilter (com.canoo.dp.impl.server.client.ClientSessionFilter)1 ClientSessionLifecycleHandler (com.canoo.dp.impl.server.client.ClientSessionLifecycleHandler)1 ClientSessionManager (com.canoo.dp.impl.server.client.ClientSessionManager)1 ClientSessionMutextHolder (com.canoo.dp.impl.server.client.ClientSessionMutextHolder)1 HttpSessionCleanerListener (com.canoo.dp.impl.server.client.HttpSessionCleanerListener)1 RemotingConfiguration (com.canoo.dp.impl.server.config.RemotingConfiguration)1