All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bidib.wizard.config.ApplicationRunnerConfig Maven / Gradle / Ivy

There is a newer version: 2.0.29
Show newest version
package org.bidib.wizard.config;

import java.awt.EventQueue;

import javax.swing.RepaintManager;

import org.apache.commons.lang3.SystemUtils;
import org.bidib.wizard.api.context.ApplicationContext;
import org.bidib.wizard.client.common.boot.SwingApplicationInitializer;
import org.bidib.wizard.common.context.DefaultApplicationContext;
import org.bidib.wizard.main.BiDiBWizardStandaloneApplication;
import org.bidib.wizard.mvc.error.controller.ErrorController;
import org.bidib.wizard.mvc.main.controller.DefaultBidibPiController;
import org.bidib.wizard.mvc.main.controller.MainController;
import org.bidib.wizard.utils.ThreadCheckingRepaintManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ApplicationRunnerConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationRunnerConfig.class);

    @Bean
    SwingApplicationInitializer swingApplicationInitializer(
        final org.springframework.context.ApplicationContext context) {
        return new SwingApplicationInitializer(context);
    }

    @Bean
    ApplicationRunner applicationRunner(final org.springframework.context.ApplicationContext ctx) {

        return args -> {
            EventQueue.invokeLater(() -> {
                LOGGER.info("Create the application context from the EDT. Started the spring boot application.");

                try {

                    RepaintManager.setCurrentManager(new ThreadCheckingRepaintManager());

                    final ApplicationArguments applicationArguments =
                        ctx.getBean("springApplicationArguments", ApplicationArguments.class);

                    SwingApplicationInitializer swingApplicationInitializer =
                        ctx.getBean(SwingApplicationInitializer.class);
                    swingApplicationInitializer.run(applicationArguments);

                    MainController mainController = ctx.getBean(MainController.class);

                    // TODO this is a hack
                    ApplicationContext applicationContext = DefaultApplicationContext.getInstance();
                    applicationContext.register(DefaultApplicationContext.KEY_SPRINGCONTEXT, ctx);

                    if (SystemUtils.IS_OS_LINUX) {
                        LoggerFactory
                            .getLogger(BiDiBWizardStandaloneApplication.class)
                            .info("Check if we are running on BiDiB-Pi infrastructure.");

                        try {
                            DefaultBidibPiController bidibPiController = new DefaultBidibPiController();
                            bidibPiController.start(applicationContext);

                            LoggerFactory
                                .getLogger(BiDiBWizardStandaloneApplication.class)
                                .info("The Pi connector was initialized.");
                        }
                        catch (IllegalArgumentException ex) {
                            LoggerFactory
                                .getLogger(BiDiBWizardStandaloneApplication.class)
                                .warn("The Pi connector was not initialized.");
                        }
                    }

                    new ErrorController().start(() -> mainController.hasStartupPassed());

                    mainController.start();

                    LOGGER.info("Start application passed.");
                }
                catch (Exception ex) {
                    LOGGER.warn("Start application failed.", ex);

                    SpringApplication.exit(ctx, () -> 1);
                }
            });
        };
    };

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy