public String nonNullValue(String key) { String value = value(key); if (value == null) { throw new IllegalArgumentException("Missing property: " + key); } return value; }
@Override public Optional<String> getValue(String key) { return Optional.ofNullable(props.value(key)); }
public boolean valueAsBoolean(String key) { String s = value(key); return s != null && Boolean.parseBoolean(s); }
public boolean valueAsBoolean(String key, boolean defaultValue) { String s = value(key); return s != null ? Boolean.parseBoolean(s) : defaultValue; }
public File nonNullValueAsFile(String key) { String s = value(key); if (s == null) { throw new IllegalArgumentException("Property " + key + " is not set"); } return new File(s); }
@CheckForNull public String value(String key, @Nullable String defaultValue) { String s = value(key); return s == null ? defaultValue : s; }
@CheckForNull public Integer valueAsInt(String key) { String s = value(key); if (s != null && !"".equals(s)) { try { return Integer.parseInt(s); } catch (NumberFormatException e) { throw new IllegalStateException("Value of property " + key + " is not an integer: " + s, e); } } return null; }
private void configureOthers(Map<String, String> builder) { builder.put("action.auto_create_index", String.valueOf(false)); if (props.value("sonar.search.javaAdditionalOpts", "").contains("-D" + SECCOMP_PROPERTY + "=false")) { builder.put(SECCOMP_PROPERTY, "false"); } } }
private static String requireValue(Props props, String key) { String value = props.value(key); if (isBlank(value)) { throw new MessageException(format("Property %s is mandatory", key)); } return value; }
@CheckForNull static Level getPropertyValueAsLevel(Props props, String propertyKey) { String value = props.value(propertyKey); if (value == null) { return null; } Level level = Level.toLevel(value, Level.INFO); if (!isAllowed(level)) { throw new IllegalArgumentException(format("log level %s in property %s is not a supported value (allowed levels are %s)", level, propertyKey, Arrays.toString(ALLOWED_ROOT_LOG_LEVELS))); } return level; }
private static void ensureNotH2(Props props) { String jdbcUrl = props.value(JDBC_URL.getKey()); if (isBlank(jdbcUrl) || jdbcUrl.startsWith("jdbc:h2:")) { throw new MessageException("Embedded database is not supported in cluster mode"); } }
private static String getHost(Props props, NetworkUtils networkUtils) { String host = props.value(CLUSTER_NODE_HOST.getKey()); if (host != null && !host.isEmpty()) { return host; } return networkUtils.getHostname(); }
private static void fixPortIfZero(Props props, String addressPropertyKey, String portPropertyKey) { String port = props.value(portPropertyKey); if ("0".equals(port)) { String address = props.nonNullValue(addressPropertyKey); try { props.set(portPropertyKey, String.valueOf(NetworkUtilsImpl.INSTANCE.getNextAvailablePort(InetAddress.getByName(address)))); } catch (UnknownHostException e) { throw new IllegalStateException("Cannot resolve address [" + address + "] set by property [" + addressPropertyKey + "]", e); } } } }
@Override public Optional<String> getValue(String key) { return Optional.ofNullable(props.value(key)); }
@Test public void setDefault() { Properties p = new Properties(); p.setProperty("foo", "foo_value"); Props props = new Props(p); props.setDefault("foo", "foo_def"); props.setDefault("bar", "bar_def"); assertThat(props.value("foo")).isEqualTo("foo_value"); assertThat(props.value("bar")).isEqualTo("bar_def"); assertThat(props.value("other")).isNull(); }
@Test public void load_properties_from_file() throws Exception { File propsFile = temp.newFile(); FileUtils.write(propsFile, "sonar.foo=bar\nprocess.key=web\nprocess.index=1\nprocess.sharedDir=" + temp.newFolder().getAbsolutePath().replaceAll("\\\\", "/")); ProcessEntryPoint entryPoint = ProcessEntryPoint.createForArguments(new String[] {propsFile.getAbsolutePath()}); assertThat(entryPoint.getProps().value("sonar.foo")).isEqualTo("bar"); assertThat(entryPoint.getProps().value("process.key")).isEqualTo("web"); }
@Test public void completeDefaults_does_not_override_existing_properties() { Properties p = new Properties(); p.setProperty("sonar.jdbc.username", "angela"); Props props = new Props(p); ProcessProperties.completeDefaults(props); assertThat(props.value("sonar.jdbc.username")).isEqualTo("angela"); }
@Test @UseDataProvider("beforeAndAfterBlanks") public void constructor_trims_key_and_values_from_Properties_argument(String blankBefore, String blankAfter) { Properties properties = new Properties(); String key = RandomStringUtils.randomAlphanumeric(3); String value = RandomStringUtils.randomAlphanumeric(3); properties.put(blankBefore + key + blankAfter, blankBefore + value + blankAfter); Props underTest = new Props(properties); if (!blankBefore.isEmpty() || !blankAfter.isEmpty()) { assertThat(underTest.contains(blankBefore + key + blankAfter)).isFalse(); } assertThat(underTest.value(key)).isEqualTo(value); }
@Test public void loadPropsFromCommandLineArgs_load_properties_from_file() throws Exception { File propsFile = temp.newFile(); FileUtils.write(propsFile, "foo=bar"); Props result = ConfigurationUtils.loadPropsFromCommandLineArgs(new String[] {propsFile.getAbsolutePath()}); assertThat(result.value("foo")).isEqualTo("bar"); assertThat(result.rawProperties()).hasSize(1); }
@Override public void accept(Props props) { File homeDir = props.nonNullValueAsFile(PATH_HOME.getKey()); Provider provider = resolveProviderAndEnforceNonnullJdbcUrl(props); String url = props.value(JDBC_URL.getKey()); checkUrlParameters(provider, url); String driverPath = driverPath(homeDir, provider); props.set(JDBC_DRIVER_PATH.getKey(), driverPath); }