Skip to content

Commit

Permalink
[KEYCLOAK-19686] - Windows support and recording if persisted properties
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroigor committed Dec 7, 2021
1 parent 3602873 commit 43c46db
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 35 deletions.
60 changes: 38 additions & 22 deletions distribution/server-x-dist/src/main/content/bin/kc.bat
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,36 @@ if "%KEY%" == "" (
goto MAIN
)
if "%KEY%" == "--debug" (
set "DEBUG_MODE=true"
set "DEBUG_PORT_VAR=%~2"
if "%DEBUG_PORT_VAR%" == "" (
set DEBUG_PORT_VAR=8787
)
shift
shift
goto READ-ARGS
set "DEBUG_MODE=true"
set "DEBUG_PORT_VAR=%~2"
if "%DEBUG_PORT_VAR%" == "" (
set DEBUG_PORT_VAR=8787
)
shift
shift
goto READ-ARGS
)
if "%KEY%" == "start-dev" (
set "CONFIG_ARGS=%CONFIG_ARGS% --profile=dev %KEY% --auto-build"
shift
shift
goto READ-ARGS
)
if not "%KEY:~0,2%"=="--" if "%KEY:~0,1%"=="-" (
set "SERVER_OPTS=%SERVER_OPTS% %KEY%=%~2"
shift
if not "%KEY:~0,2%"=="--" if "%KEY:~0,2%"=="-D" (
set "SERVER_OPTS=%SERVER_OPTS% %KEY%=%~2"
shift
)
if not "%KEY:~0,2%"=="--" if not "%KEY:~0,1%"=="-" (
set "CONFIG_ARGS=%CONFIG_ARGS% %KEY%"
set "CONFIG_ARGS=%CONFIG_ARGS% %KEY%"
)
if "%KEY:~0,2%"=="--" (
if "%~2"=="" (
set "CONFIG_ARGS=%CONFIG_ARGS% %KEY%"
) else (
set "CONFIG_ARGS=%CONFIG_ARGS% %KEY%=%~2%"
)

shift
if "%KEY:~0,2%"=="--" if not "%KEY:~0,2%"=="-D" if "%KEY:~0,1%"=="-" (
if "%~2"=="" (
set "CONFIG_ARGS=%CONFIG_ARGS% %KEY%"
) else (
set "CONFIG_ARGS=%CONFIG_ARGS% %KEY% %~2%"
)

shift
)
shift
goto READ-ARGS
Expand Down Expand Up @@ -103,8 +109,18 @@ if "x%JAVA_HOME%" == "x" (
)
)

set "CLASSPATH_OPTS=%DIRNAME%..\lib\quarkus-run.jar;%DIRNAME%..\lib\lib\main\*.*"
set "CLASSPATH_OPTS=%DIRNAME%..\lib\quarkus-run.jar"

set "JAVA_RUN_OPTS=%JAVA_OPTS% -Dkc.home.dir="%DIRNAME%.." -Djboss.server.config.dir="%DIRNAME%..\conf" -Dkeycloak.theme.dir="%DIRNAME%..\themes" %SERVER_OPTS% -cp "%CLASSPATH_OPTS%" io.quarkus.bootstrap.runner.QuarkusEntryPoint %CONFIG_ARGS%"

SetLocal EnableDelayedExpansion

set "AUTO_BUILD_OPTION=auto-build"

if not "!CONFIG_ARGS:%AUTO_BUILD_OPTION%=!"=="!CONFIG_ARGS!" (
%JAVA% -Dkc.config.rebuild-and-exit=true %JAVA_RUN_OPTS%
)

"%JAVA%" %JAVA_OPTS% -Dkc.home.dir="%DIRNAME%.." -Djboss.server.config.dir="%DIRNAME%..\conf" -Dkeycloak.theme.dir="%DIRNAME%..\themes" %SERVER_OPTS% -cp "%CLASSPATH_OPTS%" io.quarkus.bootstrap.runner.QuarkusEntryPoint %CONFIG_ARGS%
"%JAVA%" %JAVA_RUN_OPTS%

:END
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

import io.quarkus.runtime.Quarkus;
import org.keycloak.quarkus.runtime.cli.command.Build;
import org.keycloak.quarkus.runtime.cli.command.Main;
import org.keycloak.quarkus.runtime.cli.command.Start;
Expand Down Expand Up @@ -77,7 +78,11 @@ private Picocli() {
public static void parseAndRun(List<String> cliArgs) {
CommandLine cmd = createCommandLine(cliArgs);

runReAugmentationIfNeeded(cliArgs, cmd);
if (Boolean.getBoolean("kc.config.rebuild-and-exit")) {
runReAugmentationIfNeeded(cliArgs, cmd);
Quarkus.asyncExit(cmd.getCommandSpec().exitCodeOnSuccess());
return;
}

cmd.execute(cliArgs.toArray(new String[0]));
}
Expand All @@ -96,10 +101,6 @@ private static void runReAugmentationIfNeeded(List<String> cliArgs, CommandLine
runReAugmentation(cliArgs, cmd);
}
}

if (Boolean.getBoolean("kc.config.rebuild-and-exit")) {
System.exit(cmd.getCommandSpec().exitCodeOnSuccess());
}
}

private static boolean isHelpCommand(List<String> cliArgs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@

package org.keycloak.quarkus.runtime.configuration;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import io.smallrye.config.PropertiesConfigSource;
import org.keycloak.quarkus.runtime.Environment;
Expand All @@ -35,7 +40,7 @@
public final class PersistedConfigSource extends PropertiesConfigSource {

public static final String NAME = "PersistedConfigSource";
public static final String PERSISTED_PROPERTIES = "/META-INF/keycloak-persisted.properties";
public static final String PERSISTED_PROPERTIES = "META-INF/keycloak-persisted.properties";
private static final PersistedConfigSource INSTANCE = new PersistedConfigSource();

private PersistedConfigSource() {
Expand Down Expand Up @@ -91,17 +96,30 @@ private static Map<String, String> readProperties() {
}

private static InputStream loadPersistedConfig() {
URL resource = Thread.currentThread().getContextClassLoader().getResource(PERSISTED_PROPERTIES);
Path homePath = Environment.getHomePath();

if (resource == null) {
if (homePath == null) {
return null;
}

try {
return resource.openStream();
File configFile = homePath.resolve("lib").resolve("quarkus").resolve("generated-bytecode.jar").toFile();

if (!configFile.exists()) {
return null;
}

try (ZipInputStream is = new ZipInputStream(new FileInputStream(configFile))) {
ZipEntry entry;

while ((entry = is.getNextEntry()) != null) {
if (entry.getName().equals(PERSISTED_PROPERTIES)) {
return new ByteArrayInputStream(is.readAllBytes());
}
}
} catch (Exception cause) {
throw new RuntimeException("Failed to resolve persisted propertied file", cause);
throw new RuntimeException("Failed to load persisted properties from " + configFile, cause);
}
}

return null;
}
}

0 comments on commit 43c46db

Please sign in to comment.