Skip to content

Commit

Permalink
Avoid reflection when registering Spring-Boot as a Keycloak Platform
Browse files Browse the repository at this point in the history
Fixes #37
  • Loading branch information
thomasdarimont committed Apr 8, 2021
1 parent 0e85a2b commit 4ac3a12
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import com.github.thomasdarimont.keycloak.embedded.support.DynamicJndiContextFactoryBuilder;
import com.github.thomasdarimont.keycloak.embedded.support.KeycloakUndertowRequestFilter;
import com.github.thomasdarimont.keycloak.embedded.support.SpringBootConfigProvider;
import com.github.thomasdarimont.keycloak.embedded.support.SpringBootPlatform;
import com.github.thomasdarimont.keycloak.embedded.support.SpringBootPlatformProvider;
import lombok.extern.slf4j.Slf4j;
import org.infinispan.configuration.parsing.ConfigurationBuilderHolder;
import org.infinispan.configuration.parsing.ParserRegistry;
import org.infinispan.manager.DefaultCacheManager;
import org.jboss.resteasy.plugins.server.servlet.HttpServlet30Dispatcher;
import org.jboss.resteasy.plugins.server.servlet.ResteasyContextParameters;
import org.keycloak.platform.Platform;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.ServerProperties;
Expand Down Expand Up @@ -39,8 +40,8 @@ protected EmbeddedKeycloakServer embeddedKeycloakServer(ServerProperties serverP

@Bean
@ConditionalOnMissingBean(name = "springBootPlatform")
protected SpringBootPlatform springBootPlatform() {
return new SpringBootPlatform();
protected SpringBootPlatformProvider springBootPlatform() {
return (SpringBootPlatformProvider) Platform.getPlatform();
}

@Bean
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.github.thomasdarimont.keycloak.embedded.support;

import com.google.auto.service.AutoService;
import lombok.extern.slf4j.Slf4j;
import org.keycloak.platform.PlatformProvider;
import org.keycloak.services.ServicesLogger;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.context.event.SmartApplicationListener;

@Slf4j
@AutoService(PlatformProvider.class)
public class SpringBootPlatformProvider implements PlatformProvider, SmartApplicationListener {

Runnable onStartup;

Runnable onShutdown;

@Override
public void onApplicationEvent(ApplicationEvent event) {

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

@Override
public boolean supportsEventType(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") Runnable onStartup) {
this.onStartup = onStartup;
}

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

@Override
public void exit(Throwable cause) {
log.error("exit", cause);
ServicesLogger.LOGGER.fatal(cause);
throw new RuntimeException(cause);
}

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

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

0 comments on commit 4ac3a12

Please sign in to comment.