Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2016 Network New Technologies Inc.
*
* Licensed 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 com.networknt.config;
import com.networknt.config.yml.DecryptConstructor;
import com.networknt.decrypt.Decryptor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* This class has a public method called getInjectValue which is used to generate
* the values which need to be injected from environment variables or a specific
* file called "values.yaml".
*
* Three injection order defined as following and default mode is [2]:
* [0] Inject from "values.yaml" only.
* [1] Inject from system environment first, then overwrite by "values.yaml"
* if exist.
* [2] Inject from "values.yaml" first, then overwrite with the values in the
* system environment.
* This parameter can be set through setting system property "injection_order" in
* commend line
*
* Created by jiachen on 2019-01-08.
*/
public class ConfigInjection {
// Define the injection order
private static final String INJECTION_ORDER = "injection_order";
private static final String INJECTION_ORDER_CODE = (!System.getProperty(INJECTION_ORDER, "").equals("")) ?
System.getProperty(INJECTION_ORDER, "") : "2";
// Define one of the injection value source "values.yaml" and list of exclusion config files
public static final String CENTRALIZED_MANAGEMENT = "values";
private static final String SCALABLE_CONFIG = "config";
private static final String EXCLUSION_CONFIG_FILE_LIST = "exclusionConfigFileList";
private static final String ALLOW_DEFAULT_EMPTY = "allowDefaultValueEmpty";
private static final Map exclusionMap = Config.getInstance().getJsonMapConfig(SCALABLE_CONFIG);
// Define the injection pattern which represents the injection points
private static final Pattern pattern = Pattern.compile("\\$\\{(.*?)\\}");
private static final String[] trueArray = {"y", "Y", "yes", "Yes", "YES", "true", "True", "TRUE", "on", "On", "ON"};
private static final String[] falseArray = {"n", "N", "no", "No", "NO", "false", "False", "FALSE", "off", "Off", "OFF"};
private static final Decryptor decryptor = DecryptConstructor.getInstance().getDecryptor();
public static Map decryptedValueMap = Config.getInstance().getDefaultJsonMapConfigNoCache(CENTRALIZED_MANAGEMENT);
public static Map undecryptedValueMap = Config.getNoneDecryptedInstance().getDefaultJsonMapConfigNoCache(CENTRALIZED_MANAGEMENT);
// Method used to generate the values from environment variables or "values.yaml"
public static Object getInjectValue(String string, boolean decrypt) {
Matcher m = pattern.matcher(string);
StringBuffer sb = new StringBuffer();
// Parse the content inside pattern "${}" when this pattern is found
while (m.find()) {
// Get parsing result
Object value = getValue(m.group(1), decrypt);
// Return directly when the parsing result don't need to be cast to String
if (!(value instanceof String)) {
return value;
}
String valueStr = (String)value;
if(valueStr.contains("\\$")) {
m.appendReplacement(sb, (String)value);
} else {
m.appendReplacement(sb, Matcher.quoteReplacement((String)value));
}
}
return m.appendTail(sb).toString();
}
// Return the list of exclusion files list which includes the names of config files that shouldn't be injected
// Double check values and exclusions to ensure no dead loop
public static boolean isExclusionConfigFile(String configName) {
List