janhoy commented on code in PR #1935: URL: https://github.com/apache/solr/pull/1935#discussion_r1421434794
########## solr/core/src/java/org/apache/solr/util/EnvUtils.java: ########## @@ -0,0 +1,279 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.util; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.Properties; +import java.util.SortedMap; +import java.util.TreeMap; +import java.util.stream.Collectors; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.util.StrUtils; +import org.apache.solr.common.util.Utils; + +/** + * This class is a unified provider of environment variables and system properties. It exposes a + * mutable copy of the environment variables. It also converts 'SOLR_FOO' variables to system + * properties 'solr.foo' and provide various convenience accessors for them. + */ +public class EnvUtils { + private static final SortedMap<String, String> ENV = new TreeMap<>(System.getenv()); + private static final Map<String, String> CUSTOM_MAPPINGS = new HashMap<>(); + private static boolean initialized = false; + + static { + try { + Properties props = new Properties(); + try (InputStream stream = + EnvUtils.class.getClassLoader().getResourceAsStream("EnvToSyspropMappings.properties")) { + props.load(new InputStreamReader(Objects.requireNonNull(stream), StandardCharsets.UTF_8)); + for (String key : props.stringPropertyNames()) { + CUSTOM_MAPPINGS.put(key, props.getProperty(key)); + } + init(false); + } + } catch (IOException e) { + throw new SolrException( + SolrException.ErrorCode.INVALID_STATE, "Failed loading env.var->properties mapping", e); + } + } + + /** + * Get Solr's mutable copy of all environment variables. + * + * @return sorted map of environment variables + */ + public static SortedMap<String, String> getEnvs() { + return ENV; + } + + /** Get a single environment variable as string */ + public static String getEnv(String key) { + return ENV.get(key); + } + + /** Get a single environment variable as string, or default */ + public static String getEnv(String key, String defaultValue) { + return ENV.getOrDefault(key, defaultValue); + } + + /** Get an environment variable as long */ + public static long getEnvAsLong(String key) { + return Long.parseLong(ENV.get(key)); + } + + /** Get an environment variable as long, or default value */ + public static long getEnvAsLong(String key, long defaultValue) { + String value = ENV.get(key); + if (value == null) { + return defaultValue; + } + return Long.parseLong(value); + } + + /** Get an env var as boolean */ + public static boolean getEnvAsBool(String key) { + return StrUtils.parseBool(ENV.get(key)); + } + + /** Get an env var as boolean, or default value */ + public static boolean getEnvAsBool(String key, boolean defaultValue) { + String value = ENV.get(key); + if (value == null) { + return defaultValue; + } + return StrUtils.parseBool(value); + } + + /** Get comma separated strings from env as List */ + public static List<String> getEnvAsList(String key) { + return getEnv(key) != null ? stringValueToList(getEnv(key)) : null; + } + + /** Get comma separated strings from env as List */ + public static List<String> getEnvAsList(String key, List<String> defaultValue) { + return ENV.get(key) != null ? getEnvAsList(key) : defaultValue; + } + + /** Set an environment variable */ + public static void setEnv(String key, String value) { + ENV.put(key, value); + } + + /** Set all environment variables */ + public static synchronized void setEnvs(Map<String, String> env) { + ENV.clear(); + ENV.putAll(env); + } + + /** Get all Solr system properties as a sorted map */ + public static SortedMap<String, String> getProps() { Review Comment: I don't have a strong defence, but it is convenient to work with both in actual CLI/UI, in debug print and in tests, especially since the list of vars is so long. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
