Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove need for reflection in SpringBootPlatformProvider #37

Closed
jmax01 opened this issue Mar 31, 2021 · 1 comment
Closed

Remove need for reflection in SpringBootPlatformProvider #37

jmax01 opened this issue Mar 31, 2021 · 1 comment

Comments

@jmax01
Copy link
Contributor

jmax01 commented Mar 31, 2021

We can remove the getPlatformField workaround in SpringBootPlatform pushing the SmartApplicationListener to the Delegate and registering it as a Bean thus registering as an ApplicationListener:

@Configuration
public class EmbeddedKeycloakConfig {

        @Bean
        @ConditionalOnMissingBean
        SpringBootPlatformProvider springBootPlatformProvider() {
            return (SpringBootPlatformProvider) Platform.getPlatform();
        }
@AutoService(PlatformProvider.class)
@Log4j2
public class SpringBootPlatformProvider implements PlatformProvider, SmartApplicationListener {

    Runnable onStartup;

    Runnable onShutdown;

    @Override
    public void onApplicationEvent(final ApplicationEvent event) {

        if (event instanceof ApplicationReadyEvent) {
            startup();
        }else if (event instanceof ContextStoppedEvent) {
            shutdown();
        }
    }

    @Override
    public boolean supportsEventType(final Class<? extends ApplicationEvent> eventType) {
        return ApplicationReadyEvent.class.equals(eventType) || ContextStoppedEvent.class.equals(eventType);
    }

    @Override
    public String getListenerId() {
        return this.getClass()
                .getName();
    }

    @Override
    public void onStartup(@SuppressWarnings("hiding") final Runnable onStartup) {
        this.onStartup = onStartup;
    }

    @Override
    public void onShutdown(@SuppressWarnings("hiding") final Runnable onShutdown) {
        this.onShutdown = onShutdown;
    }

    @Override
    public void exit(final Throwable cause) {

        LOGGER.error("exit", cause);
        ServicesLogger.LOGGER.fatal(cause);
        throw new RuntimeException(cause);
    }

    private void shutdown() {
        this.onShutdown.run();

    }

    private void startup() {
        this.onStartup.run();

    }
}
@thomasdarimont
Copy link
Owner

Thanks! Fixed via 4ac3a12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants