import org.slf4j.LoggerFactory;
import org.sonar.core.extension.ServiceLoaderWrapper;
import org.sonar.core.util.SettingFormatter;
-import org.sonar.process.ConfigurationUtils;
import org.sonar.process.NetworkUtilsImpl;
import org.sonar.process.ProcessProperties;
import org.sonar.process.Props;
loadPropertiesFromEnvironment(system, p, keysOverridableFromEnv);
p.putAll(CommandLineParser.parseArguments(cliArguments));
p.setProperty(PATH_HOME.getKey(), homeDir.getAbsolutePath());
- p = ConfigurationUtils.interpolateVariables(p, system.getenv());
// the difference between Properties and Props is that the latter
// supports decryption of values, so it must be used when values
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
-import java.util.Enumeration;
-import java.util.Map;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang.text.StrSubstitutor;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.core.util.SettingFormatter;
import static org.sonar.process.FileUtils2.deleteQuietly;
public final class ConfigurationUtils {
- private static final Logger LOG = LoggerFactory.getLogger(ConfigurationUtils.class);
- private static final String ENV_VAR_INTERPOLATION_PREFIX = "${env:";
- private static final String ENV_VAR_INTERPOLATION_POSTFIX = "}";
private ConfigurationUtils() {
// Utility class
}
- public static Properties interpolateVariables(Properties properties, Map<String, String> variables) {
- Properties result = new Properties();
- Enumeration<Object> keys = properties.keys();
- while (keys.hasMoreElements()) {
- String key = (String) keys.nextElement();
- String value = (String) properties.get(key);
- if (value.contains(ENV_VAR_INTERPOLATION_PREFIX)) {
- String environmentVariableName = SettingFormatter.fromJavaPropertyToEnvVariable(key);
- LOG.warn("Referencing environment variables in configuration is deprecated and will be removed in a future version of SonarQube. " +
- "You should stop using '{}' in your configuration and use the '{}' environment variable instead.", value, environmentVariableName);
- }
-
- String interpolatedValue = StrSubstitutor.replace(value, variables, ENV_VAR_INTERPOLATION_PREFIX, ENV_VAR_INTERPOLATION_POSTFIX);
- result.setProperty(key, interpolatedValue);
- }
- return result;
- }
-
static Props loadPropsFromCommandLineArgs(String[] args) {
if (args.length != 1) {
throw new IllegalArgumentException("Only a single command-line argument is accepted " +
import java.io.File;
import java.nio.charset.StandardCharsets;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;
@Rule
public TemporaryFolder temp = new TemporaryFolder();
- @Test
- public void shouldInterpolateVariables() {
- Properties input = new Properties();
- input.setProperty("hello", "world");
- input.setProperty("url", "${env:SONAR_JDBC_URL}");
- input.setProperty("do_not_change", "${SONAR_JDBC_URL}");
- Map<String, String> variables = new HashMap<>();
- variables.put("SONAR_JDBC_URL", "jdbc:h2:mem");
-
- Properties output = ConfigurationUtils.interpolateVariables(input, variables);
-
- assertThat(output).hasSize(3);
- assertThat(output.getProperty("hello")).isEqualTo("world");
- assertThat(output.getProperty("url")).isEqualTo("jdbc:h2:mem");
- assertThat(output.getProperty("do_not_change")).isEqualTo("${SONAR_JDBC_URL}");
-
- // input is not changed
- assertThat(input).hasSize(3);
- assertThat(input.getProperty("hello")).isEqualTo("world");
- assertThat(input.getProperty("url")).isEqualTo("${env:SONAR_JDBC_URL}");
- assertThat(input.getProperty("do_not_change")).isEqualTo("${SONAR_JDBC_URL}");
- }
-
@Test
public void loadPropsFromCommandLineArgs_missing_argument() {
try {