Skip to content

Commit f3c3bb5

Browse files
authored
Removing unnecessary code paths during startup (keycloak#10131)
Closes keycloak#10130
1 parent 909740c commit f3c3bb5

File tree

8 files changed

+91
-45
lines changed

8 files changed

+91
-45
lines changed

quarkus/deployment/pom.xml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,6 @@
7878
<groupId>io.quarkus</groupId>
7979
<artifactId>quarkus-smallrye-metrics-deployment</artifactId>
8080
</dependency>
81-
<!-- Cross-DC and ISPN client dependencies -->
82-
<dependency>
83-
<groupId>io.quarkus</groupId>
84-
<artifactId>quarkus-infinispan-client-deployment</artifactId>
85-
</dependency>
86-
<dependency>
87-
<groupId>org.wildfly.security</groupId>
88-
<artifactId>wildfly-elytron-sasl-gssapi</artifactId>
89-
</dependency>
90-
<dependency>
91-
<groupId>org.wildfly.security</groupId>
92-
<artifactId>wildfly-elytron-sasl-gs2</artifactId>
93-
</dependency>
9481
<dependency>
9582
<groupId>io.quarkiverse.vault</groupId>
9683
<artifactId>quarkus-vault-deployment</artifactId>

quarkus/runtime/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@
8080
<artifactId>quarkus-smallrye-metrics</artifactId>
8181
</dependency>
8282
<dependency>
83-
<groupId>io.quarkus</groupId>
84-
<artifactId>quarkus-infinispan-client</artifactId>
83+
<groupId>org.wildfly.security</groupId>
84+
<artifactId>wildfly-elytron</artifactId>
8585
</dependency>
8686
<dependency>
8787
<groupId>io.quarkiverse.vault</groupId>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2021 Red Hat, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.keycloak.quarkus.runtime;
19+
20+
import java.io.IOException;
21+
import java.net.URL;
22+
import java.sql.Driver;
23+
import java.util.Collections;
24+
import java.util.Enumeration;
25+
26+
public class KeycloakClassLoader extends ClassLoader {
27+
28+
KeycloakClassLoader() {
29+
super(Thread.currentThread().getContextClassLoader());
30+
}
31+
32+
@Override
33+
public Enumeration<URL> getResources(String name) throws IOException {
34+
// drivers are going to be loaded lazily, and we avoid loading all available drivers
35+
// see https://github.com/quarkusio/quarkus/pull/7089
36+
if (name.contains(Driver.class.getName())) {
37+
return Collections.emptyEnumeration();
38+
}
39+
40+
return super.getResources(name);
41+
}
42+
}

quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/KeycloakMain.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@
5050
@ApplicationScoped
5151
public class KeycloakMain implements QuarkusApplication {
5252

53-
private static final Logger LOGGER = Logger.getLogger(KeycloakMain.class);
54-
5553
public static void main(String[] args) {
5654
System.setProperty("kc.version", Version.VERSION_KEYCLOAK);
5755
List<String> cliArgs = Picocli.parseArgs(args);
@@ -80,7 +78,11 @@ public static void main(String[] args) {
8078
}
8179

8280
public static void start(ExecutionExceptionHandler errorHandler, PrintWriter errStream) {
81+
ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
82+
8383
try {
84+
Thread.currentThread().setContextClassLoader(new KeycloakClassLoader());
85+
8486
Quarkus.run(KeycloakMain.class, (exitCode, cause) -> {
8587
if (cause != null) {
8688
errorHandler.error(errStream,
@@ -98,6 +100,8 @@ public static void start(ExecutionExceptionHandler errorHandler, PrintWriter err
98100
errorHandler.error(errStream,
99101
String.format("Unexpected error when starting the server in (%s) mode", getKeycloakModeFromProfile(getProfileOrDefault("prod"))),
100102
cause.getCause());
103+
} finally {
104+
Thread.currentThread().setContextClassLoader(originalCl);
101105
}
102106
}
103107

@@ -107,7 +111,7 @@ public static void start(ExecutionExceptionHandler errorHandler, PrintWriter err
107111
@Override
108112
public int run(String... args) throws Exception {
109113
if (isDevProfile()) {
110-
LOGGER.warnf("Running the server in development mode. DO NOT use this configuration in production.");
114+
Logger.getLogger(KeycloakMain.class).warnf("Running the server in development mode. DO NOT use this configuration in production.");
111115
}
112116

113117
int exitCode = ApplicationLifecycleManager.getExitCode();

quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/KeycloakRecorder.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,7 @@ public void configSessionFactory(
6262

6363
public RuntimeValue<CacheManagerFactory> createCacheInitializer(String config, ShutdownContext shutdownContext) {
6464
try {
65-
ConfigurationBuilderHolder builder = new ParserRegistry().parse(config);
66-
67-
if (builder.getNamedConfigurationBuilders().get("sessions").clustering().cacheMode().isClustered()) {
68-
configureTransportStack(builder);
69-
}
70-
71-
// For Infinispan 10, we go with the JBoss marshalling.
72-
// TODO: This should be replaced later with the marshalling recommended by infinispan. Probably protostream.
73-
// See https://infinispan.org/docs/stable/titles/developing/developing.html#marshalling for the details
74-
builder.getGlobalConfigurationBuilder().serialization().marshaller(new JBossUserMarshaller());
75-
CacheManagerFactory cacheManagerFactory = new CacheManagerFactory(builder);
65+
CacheManagerFactory cacheManagerFactory = new CacheManagerFactory(config);
7666

7767
shutdownContext.addShutdownTask(new Runnable() {
7868
@Override
@@ -91,15 +81,6 @@ public void run() {
9181
}
9282
}
9383

94-
private void configureTransportStack(ConfigurationBuilderHolder builder) {
95-
String transportStack = Configuration.getRawValue("kc.cache-stack");
96-
97-
if (transportStack != null) {
98-
builder.getGlobalConfigurationBuilder().transport().defaultTransport()
99-
.addProperty("configurationFile", "default-configs/default-jgroups-" + transportStack + ".xml");
100-
}
101-
}
102-
10384
public void registerShutdownHook(ShutdownContext shutdownContext) {
10485
shutdownContext.addShutdownTask(new Runnable() {
10586
@Override

quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/storage/database/jpa/QuarkusJpaConnectionProviderFactory.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,11 @@ private void addSpecificNamedQueries(KeycloakSession session, Connection connect
122122
try {
123123
Map<String, Object> unitProperties = emf.getProperties();
124124

125-
unitProperties.entrySet().stream()
126-
.filter(entry -> entry.getKey().startsWith(QUERY_PROPERTY_PREFIX))
127-
.forEach(entry -> configureNamedQuery(entry.getKey().substring(QUERY_PROPERTY_PREFIX.length()), entry.getValue().toString(), em));
125+
for (Map.Entry<String, Object> entry : unitProperties.entrySet()) {
126+
if (entry.getKey().startsWith(QUERY_PROPERTY_PREFIX)) {
127+
configureNamedQuery(entry.getKey().substring(QUERY_PROPERTY_PREFIX.length()), entry.getValue().toString(), em);
128+
}
129+
}
128130
} finally {
129131
JpaUtils.closeEntityManager(em);
130132
}

quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/storage/infinispan/CacheManagerFactory.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,21 @@
2323
import java.util.concurrent.ThreadFactory;
2424
import java.util.concurrent.TimeUnit;
2525
import org.infinispan.configuration.parsing.ConfigurationBuilderHolder;
26+
import org.infinispan.configuration.parsing.ParserRegistry;
27+
import org.infinispan.jboss.marshalling.core.JBossUserMarshaller;
2628
import org.infinispan.manager.DefaultCacheManager;
2729
import org.jboss.logging.Logger;
30+
import org.keycloak.quarkus.runtime.configuration.Configuration;
2831

2932
public class CacheManagerFactory {
3033

31-
private ConfigurationBuilderHolder config;
34+
private String config;
3235
private DefaultCacheManager cacheManager;
3336
private Future<DefaultCacheManager> cacheManagerFuture;
3437
private ExecutorService executor;
3538
private boolean initialized;
3639

37-
public CacheManagerFactory(ConfigurationBuilderHolder config) {
40+
public CacheManagerFactory(String config) {
3841
this.config = config;
3942
this.executor = createThreadPool();
4043
this.cacheManagerFuture = executor.submit(this::startCacheManager);
@@ -69,7 +72,18 @@ public Thread newThread(Runnable r) {
6972
}
7073

7174
private DefaultCacheManager startCacheManager() {
72-
return new DefaultCacheManager(config, isStartEagerly());
75+
ConfigurationBuilderHolder builder = new ParserRegistry().parse(config);
76+
77+
if (builder.getNamedConfigurationBuilders().get("sessions").clustering().cacheMode().isClustered()) {
78+
configureTransportStack(builder);
79+
}
80+
81+
// For Infinispan 10, we go with the JBoss marshalling.
82+
// TODO: This should be replaced later with the marshalling recommended by infinispan. Probably protostream.
83+
// See https://infinispan.org/docs/stable/titles/developing/developing.html#marshalling for the details
84+
builder.getGlobalConfigurationBuilder().serialization().marshaller(new JBossUserMarshaller());
85+
86+
return new DefaultCacheManager(builder, isStartEagerly());
7387
}
7488

7589
private boolean isStartEagerly() {
@@ -101,4 +115,13 @@ private void shutdownThreadPool() {
101115
}
102116
}
103117
}
118+
119+
private void configureTransportStack(ConfigurationBuilderHolder builder) {
120+
String transportStack = Configuration.getRawValue("kc.cache-stack");
121+
122+
if (transportStack != null) {
123+
builder.getGlobalConfigurationBuilder().transport().defaultTransport()
124+
.addProperty("configurationFile", "default-configs/default-jgroups-" + transportStack + ".xml");
125+
}
126+
}
104127
}

services/src/main/java/org/keycloak/credential/WebAuthnCredentialProviderFactory.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,30 @@
1616

1717
package org.keycloak.credential;
1818

19+
import org.keycloak.Config;
1920
import org.keycloak.common.Profile;
2021
import org.keycloak.models.KeycloakSession;
2122

2223
import com.webauthn4j.converter.util.ObjectConverter;
24+
import org.keycloak.models.KeycloakSessionFactory;
2325
import org.keycloak.provider.EnvironmentDependentProviderFactory;
2426

2527
public class WebAuthnCredentialProviderFactory implements CredentialProviderFactory<WebAuthnCredentialProvider>, EnvironmentDependentProviderFactory {
2628

2729
public static final String PROVIDER_ID = "keycloak-webauthn";
2830

29-
private static ObjectConverter converter = new ObjectConverter();
31+
private ObjectConverter converter;
3032

3133
@Override
3234
public CredentialProvider create(KeycloakSession session) {
3335
return new WebAuthnCredentialProvider(session, converter);
3436
}
3537

38+
@Override
39+
public void init(Config.Scope config) {
40+
converter = new ObjectConverter();
41+
}
42+
3643
@Override
3744
public String getId() {
3845
return PROVIDER_ID;

0 commit comments

Comments
 (0)