Search in sources :

Example 1 with ModuleInitializationException

use of com.canoo.platform.server.spi.ModuleInitializationException 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 2 with ModuleInitializationException

use of com.canoo.platform.server.spi.ModuleInitializationException in project dolphin-platform by canoo.

the class PlatformBootstrap method init.

public void init(final ServletContext servletContext, final ServerConfiguration configuration) {
    Assert.requireNonNull(servletContext, "servletContext");
    Assert.requireNonNull(configuration, "configuration");
    ContextManagerImpl.getInstance().addGlobalContext(APPLICATION_CONTEXT, configuration.getProperty(APPLICATION_NAME_PROPERTY));
    if (configuration.getBooleanProperty(PLATFORM_ACTIVE)) {
        PlatformLogo.printLogo();
        try {
            LOG.info("Will boot Dolphin Plaform now");
            servletContext.setAttribute(CONFIGURATION_ATTRIBUTE_NAME, configuration);
            configuration.log();
            MBeanRegistry.getInstance().setMbeanSupport(configuration.getBooleanProperty(MBEAN_REGISTRATION));
            // TODO: We need to provide a container specific thread factory that contains managed threads
            // See https://github.com/canoo/dolphin-platform/issues/498
            final PlatformThreadFactory threadFactory = new SimpleDolphinPlatformThreadFactory();
            final ManagedBeanFactory beanFactory = getBeanFactory(servletContext);
            final DefaultClasspathScanner classpathScanner = new DefaultClasspathScanner(configuration.getListProperty(ROOT_PACKAGE_FOR_CLASSPATH_SCAN));
            serverCoreComponents = new ServerCoreComponentsImpl(servletContext, configuration, threadFactory, classpathScanner, beanFactory);
            final Set<Class<?>> moduleClasses = classpathScanner.getTypesAnnotatedWith(ModuleDefinition.class);
            final Map<String, ServerModule> modules = new HashMap<>();
            for (final Class<?> moduleClass : moduleClasses) {
                if (!ServerModule.class.isAssignableFrom(moduleClass)) {
                    throw new DolphinRuntimeException("Class " + moduleClass + " is annoated with " + ModuleDefinition.class.getSimpleName() + " but do not implement " + ServerModule.class.getSimpleName());
                }
                ModuleDefinition moduleDefinition = moduleClass.getAnnotation(ModuleDefinition.class);
                ServerModule instance = (ServerModule) moduleClass.newInstance();
                modules.put(instance.getName(), instance);
            }
            LOG.info("Found {} Dolphin Plaform modules", modules.size());
            if (LOG.isTraceEnabled()) {
                for (final String moduleName : modules.keySet()) {
                    LOG.trace("Found Dolphin Plaform module {}", moduleName);
                }
            }
            for (final Map.Entry<String, ServerModule> moduleEntry : modules.entrySet()) {
                LOG.debug("Will initialize Dolphin Plaform module {}", moduleEntry.getKey());
                final ServerModule module = moduleEntry.getValue();
                if (module.shouldBoot(serverCoreComponents.getConfiguration())) {
                    final List<String> neededModules = module.getModuleDependencies();
                    for (final String neededModule : neededModules) {
                        if (!modules.containsKey(neededModule)) {
                            throw new ModuleInitializationException("Module " + moduleEntry.getKey() + " depends on missing module " + neededModule);
                        }
                    }
                    module.initialize(serverCoreComponents);
                }
            }
            LOG.info("Dolphin Plaform booted");
        } catch (Exception e) {
            throw new RuntimeException("Can not boot Dolphin Platform", e);
        }
    } else {
        LOG.info("Dolphin Plaform is deactivated");
    }
}
Also used : HashMap(java.util.HashMap) DefaultClasspathScanner(com.canoo.dp.impl.server.scanner.DefaultClasspathScanner) SimpleDolphinPlatformThreadFactory(com.canoo.dp.impl.platform.core.SimpleDolphinPlatformThreadFactory) ModuleInitializationException(com.canoo.platform.server.spi.ModuleInitializationException) DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) ModuleInitializationException(com.canoo.platform.server.spi.ModuleInitializationException) ServerModule(com.canoo.platform.server.spi.ServerModule) ModuleDefinition(com.canoo.platform.server.spi.ModuleDefinition) DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) ManagedBeanFactory(com.canoo.platform.server.spi.components.ManagedBeanFactory) DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) PlatformThreadFactory(com.canoo.platform.core.PlatformThreadFactory) SimpleDolphinPlatformThreadFactory(com.canoo.dp.impl.platform.core.SimpleDolphinPlatformThreadFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ModuleInitializationException (com.canoo.platform.server.spi.ModuleInitializationException)2 ManagedBeanFactory (com.canoo.platform.server.spi.components.ManagedBeanFactory)2 SimpleDolphinPlatformThreadFactory (com.canoo.dp.impl.platform.core.SimpleDolphinPlatformThreadFactory)1 ClientSessionLifecycleHandler (com.canoo.dp.impl.server.client.ClientSessionLifecycleHandler)1 ClientSessionProvider (com.canoo.dp.impl.server.client.ClientSessionProvider)1 RemotingConfiguration (com.canoo.dp.impl.server.config.RemotingConfiguration)1 DefaultDolphinContextFactory (com.canoo.dp.impl.server.context.DefaultDolphinContextFactory)1 DolphinContextCommunicationHandler (com.canoo.dp.impl.server.context.DolphinContextCommunicationHandler)1 DolphinContextFactory (com.canoo.dp.impl.server.context.DolphinContextFactory)1 DolphinContextProvider (com.canoo.dp.impl.server.context.DolphinContextProvider)1 ControllerValidationException (com.canoo.dp.impl.server.controller.ControllerValidationException)1 AbstractEventBus (com.canoo.dp.impl.server.event.AbstractEventBus)1 DefaultClasspathScanner (com.canoo.dp.impl.server.scanner.DefaultClasspathScanner)1 DolphinPlatformServlet (com.canoo.dp.impl.server.servlet.DolphinPlatformServlet)1 InterruptServlet (com.canoo.dp.impl.server.servlet.InterruptServlet)1 DolphinRuntimeException (com.canoo.platform.core.DolphinRuntimeException)1 PlatformThreadFactory (com.canoo.platform.core.PlatformThreadFactory)1 RemotingEventBus (com.canoo.platform.remoting.server.event.RemotingEventBus)1 EventBusProvider (com.canoo.platform.remoting.server.event.spi.EventBusProvider)1 ClientSession (com.canoo.platform.server.client.ClientSession)1