diff options
Diffstat (limited to 'common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java')
-rw-r--r-- | common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java | 711 |
1 files changed, 212 insertions, 499 deletions
diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java index 953ee155ec..528941b923 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java @@ -81,16 +81,10 @@ import org.onap.config.type.ConfigurationType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * The type Configuration utils. - */ public class ConfigurationUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationUtils.class); private static final String CONFIGURATION_TYPE_NOT_SUPPORTED = "Configuration type not supported:"; - - private ConfigurationUtils() { - } - private static final Map<Class, Class> ARRAY_CLASS_MAP; static { @@ -107,11 +101,9 @@ public class ConfigurationUtils { ARRAY_CLASS_MAP = Collections.unmodifiableMap(arrayTypes); } - /** - * Gets thread factory. - * - * @return the thread factory - */ + private ConfigurationUtils() { + } + public static ThreadFactory getThreadFactory() { return r1 -> { Thread thread = Executors.privilegedThreadFactory().newThread(r1); @@ -120,14 +112,6 @@ public class ConfigurationUtils { }; } - /** - * Gets all files. - * - * @param file the file - * @param recursive the recursive - * @param onlyDirectory the only directory - * @return the all files - */ public static Collection<File> getAllFiles(File file, boolean recursive, boolean onlyDirectory) { ArrayList<File> collection = new ArrayList<>(); if (file.isDirectory() && file.exists()) { @@ -146,206 +130,81 @@ public class ConfigurationUtils { return collection; } - /** - * Gets comma saperated list. - * - * @param list the list - * @return the comma separated list - */ - public static String getCommaSeparatedList(List list) { - return ((Stream<String>) list.stream().filter(o -> o != null && !o.toString().trim().isEmpty()).map(o -> o.toString().trim())).collect(Collectors.joining(",")); - } - - /** - * Gets comma saperated list. - * - * @param list the list - * @return the comma saperated list - */ public static String getCommaSeparatedList(String[] list) { - return getCommaSeparatedList(list == null ? Arrays.asList() : Arrays.asList(list)); + return getCommaSeparatedList(list == null ? Collections.emptyList() : Arrays.asList(list)); } - /** - * Gets config type. - * - * @param url the url - * @return the config type - */ - public static ConfigurationType getConfigType(URL url) { - return Enum.valueOf(ConfigurationType.class, - url.getFile().substring(url.getFile().lastIndexOf('.') + 1).toUpperCase()); - } - - /** - * Gets config type. - * - * @param file the file - * @return the config type - */ - public static ConfigurationType getConfigType(File file) { - return Enum.valueOf(ConfigurationType.class, - file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf('.') + 1) - .toUpperCase()); + public static String getCommaSeparatedList(List list) { + return ((Stream<String>) list.stream().filter(o -> o != null && !o.toString().trim().isEmpty()) + .map(o -> o.toString().trim())).collect(Collectors.joining(",")); } - /** - * Is config boolean. - * - * @param url the url - * @return the boolean - */ public static boolean isConfig(URL url) { return isConfig(url.getFile()); } - /** - * Is config boolean. - * - * @param file the file - * @return the boolean - */ - public static boolean isConfig(File file) { - return file != null && file.exists() && isConfig(file.getName()); - } - - /** - * Is config boolean. - * - * @param file the file - * @return the boolean - */ public static boolean isConfig(String file) { file = file.toUpperCase().substring(file.lastIndexOf('!') + 1); file = file.substring(file.lastIndexOf('/') + 1); return file.matches( - "CONFIG(-\\w*){0,1}(-" + "(" + ConfigurationMode.OVERRIDE + "|" + ConfigurationMode.MERGE - + "|" + ConfigurationMode.UNION + ")){0,1}" + "\\.(" - + ConfigurationType.PROPERTIES.name() + "|" + ConfigurationType.XML.name() + "|" - + ConfigurationType.JSON.name() + "|" + ConfigurationType.YAML.name() + ")$") - || file.matches("CONFIG(.)*\\.(" + ConfigurationType.PROPERTIES.name() + "|" - + ConfigurationType.XML.name() + "|" + ConfigurationType.JSON.name() + "|" - + ConfigurationType.YAML.name() + ")$"); - } - - /** - * Gets namespace. - * - * @param url the url - * @return the namespace - */ - public static String getNamespace(URL url) { - - Optional<String> namespace = getConfiguration(url).flatMap(ConfigurationUtils::getNamespace).map(String::toUpperCase); - - return namespace.orElseGet(() -> getNamespace(url.getFile().toUpperCase())); + "CONFIG(-\\w*){0,1}(-" + "(" + ConfigurationMode.OVERRIDE + "|" + ConfigurationMode.MERGE + "|" + + ConfigurationMode.UNION + ")){0,1}" + "\\.(" + ConfigurationType.PROPERTIES.name() + "|" + + ConfigurationType.XML.name() + "|" + ConfigurationType.JSON.name() + "|" + + ConfigurationType.YAML.name() + ")$") || file.matches( + "CONFIG(.)*\\.(" + ConfigurationType.PROPERTIES.name() + "|" + ConfigurationType.XML.name() + "|" + + ConfigurationType.JSON.name() + "|" + ConfigurationType.YAML.name() + ")$"); } - /** - * Gets namespace. - * - * @param file the file - * @return the namespace - */ - public static String getNamespace(File file) { - Optional<String> namespace = getConfiguration(file) - .flatMap(ConfigurationUtils::getNamespace) - .map(String::toUpperCase); - return namespace.orElseGet(() -> getNamespace(file.getName().toUpperCase())); + public static boolean isConfig(File file) { + return file != null && file.exists() && isConfig(file.getName()); } private static Optional<String> getNamespace(Configuration config) { - return ofNullable(config) - .flatMap(configuration -> ofNullable(configuration.getString(Constants.NAMESPACE_KEY))) - .map(String::toUpperCase); + return ofNullable(config).flatMap(configuration -> ofNullable(configuration.getString(Constants.NAMESPACE_KEY))) + .map(String::toUpperCase); } - /** - * Gets namespace. - * - * @param file the file - * @return the namespace - */ - public static String getNamespace(String file) { - file = file.toUpperCase().substring(file.lastIndexOf('!') + 1); - file = file.substring(file.lastIndexOf('/') + 1); - Pattern pattern = Pattern.compile( - "CONFIG(-\\w*){0,1}(-" + "(" + ConfigurationMode.OVERRIDE + "|" + ConfigurationMode.MERGE - + "|" + ConfigurationMode.UNION + ")){0,1}" + "\\.(" - + ConfigurationType.PROPERTIES.name() + "|" + ConfigurationType.XML.name() + "|" - + ConfigurationType.JSON.name() + "|" + ConfigurationType.YAML.name() + ")$"); - Matcher matcher = pattern.matcher(file); - boolean b1 = matcher.matches(); - if (b1) { - if (matcher.group(1) != null) { - String moduleName = matcher.group(1).substring(1); - return moduleName.equalsIgnoreCase(ConfigurationMode.OVERRIDE.name()) - || moduleName.equalsIgnoreCase(ConfigurationMode.UNION.name()) - || moduleName.equalsIgnoreCase(ConfigurationMode.MERGE.name()) - ? Constants.DEFAULT_NAMESPACE : moduleName; - } else { - return Constants.DEFAULT_NAMESPACE; - } - } else if (isConfig(file)) { - return Constants.DEFAULT_NAMESPACE; - } - - return null; - } - - /** - * Gets merge strategy. - * - * @param url the url - * @return the merge strategy - */ public static ConfigurationMode getMergeStrategy(URL url) { - Optional<ConfigurationMode> configurationMode = getConfiguration(url).flatMap(ConfigurationUtils::getMergeStrategy).flatMap(ConfigurationUtils::convertConfigurationMode); + Optional<ConfigurationMode> configurationMode = + getConfiguration(url).flatMap(ConfigurationUtils::getMergeStrategy) + .flatMap(ConfigurationUtils::convertConfigurationMode); return configurationMode.orElseGet(() -> getMergeStrategy(url.getFile().toUpperCase())); } - private static Optional<ConfigurationMode> convertConfigurationMode(String configMode) { - ConfigurationMode configurationMode = null; + public static Optional<FileBasedConfiguration> getConfiguration(URL url) { + FileBasedConfiguration builder = null; try { - configurationMode = ConfigurationMode.valueOf(configMode); - } catch (Exception exception) { - LOGGER.error("Could not find convert {} into configuration mode", configMode); + ConfigurationType configType = ConfigurationUtils.getConfigType(url); + switch (configType) { + case PROPERTIES: + builder = new Configurations().fileBased(PropertiesConfiguration.class, url); + break; + case XML: + builder = new Configurations().fileBased(XMLConfiguration.class, url); + break; + case JSON: + builder = new Configurations().fileBased(JsonConfiguration.class, url); + break; + case YAML: + builder = new Configurations().fileBased(YamlConfiguration.class, url); + break; + default: + throw new ConfigurationException(CONFIGURATION_TYPE_NOT_SUPPORTED + configType); + } + } catch (ConfigurationException exception) { + exception.printStackTrace(); } - return Optional.ofNullable(configurationMode); - } - - private static Optional<String> getMergeStrategy(Configuration config) { - return ofNullable(config) - .flatMap(configuration -> ofNullable(configuration.getString(Constants.MODE_KEY))) - .map(String::toUpperCase); - } - - - /** - * Gets merge strategy. - * - * @param file the file - * @return the merge strategy - */ - public static ConfigurationMode getMergeStrategy(File file) { - Optional<ConfigurationMode> configurationMode = getConfiguration(file).flatMap(ConfigurationUtils::getMergeStrategy).flatMap(ConfigurationUtils::convertConfigurationMode); - return configurationMode.orElseGet(() -> getMergeStrategy(file.getName().toUpperCase())); + return ofNullable(builder); } - /** - * Gets merge strategy. - * - * @param file the file - * @return the merge strategy - */ public static ConfigurationMode getMergeStrategy(String file) { file = file.toUpperCase().substring(file.lastIndexOf('!') + 1); file = file.substring(file.lastIndexOf('/') + 1); Pattern pattern = Pattern.compile( - "CONFIG(-\\w*){0,1}(-" + "(" + ConfigurationMode.OVERRIDE + "|" + ConfigurationMode.MERGE - + "|" + ConfigurationMode.UNION + ")){0,1}" + "\\.(" - + ConfigurationType.PROPERTIES.name() + "|" + ConfigurationType.XML.name() + "|" - + ConfigurationType.JSON.name() + "|" + ConfigurationType.YAML.name() + ")$"); + "CONFIG(-\\w*){0,1}(-" + "(" + ConfigurationMode.OVERRIDE + "|" + ConfigurationMode.MERGE + "|" + + ConfigurationMode.UNION + ")){0,1}" + "\\.(" + ConfigurationType.PROPERTIES.name() + "|" + + ConfigurationType.XML.name() + "|" + ConfigurationType.JSON.name() + "|" + + ConfigurationType.YAML.name() + ")$"); Matcher matcher = pattern.matcher(file); boolean b1 = matcher.matches(); if (b1) { @@ -365,44 +224,33 @@ public class ConfigurationUtils { return null; } - /** - * Gets configuration. - * - * @param url the url - * @return the configuration - */ - public static Optional<FileBasedConfiguration> getConfiguration(URL url) { - FileBasedConfiguration builder = null; + public static ConfigurationType getConfigType(URL url) { + return Enum.valueOf(ConfigurationType.class, + url.getFile().substring(url.getFile().lastIndexOf('.') + 1).toUpperCase()); + } + + private static Optional<ConfigurationMode> convertConfigurationMode(String configMode) { + ConfigurationMode configurationMode = null; try { - ConfigurationType configType = ConfigurationUtils.getConfigType(url); - switch (configType) { - case PROPERTIES: - builder = new Configurations().fileBased(PropertiesConfiguration.class, url); - break; - case XML: - builder = new Configurations().fileBased(XMLConfiguration.class, url); - break; - case JSON: - builder = new Configurations().fileBased(JsonConfiguration.class, url); - break; - case YAML: - builder = new Configurations().fileBased(YamlConfiguration.class, url); - break; - default: - throw new ConfigurationException(CONFIGURATION_TYPE_NOT_SUPPORTED + configType); - } - } catch (ConfigurationException exception) { - exception.printStackTrace(); + configurationMode = ConfigurationMode.valueOf(configMode); + } catch (Exception exception) { + LOGGER.error("Could not find convert {} into configuration mode", configMode); } - return ofNullable(builder); + return Optional.ofNullable(configurationMode); + } + + private static Optional<String> getMergeStrategy(Configuration config) { + return ofNullable(config).flatMap(configuration -> ofNullable(configuration.getString(Constants.MODE_KEY))) + .map(String::toUpperCase); + } + + public static ConfigurationMode getMergeStrategy(File file) { + Optional<ConfigurationMode> configurationMode = + getConfiguration(file).flatMap(ConfigurationUtils::getMergeStrategy) + .flatMap(ConfigurationUtils::convertConfigurationMode); + return configurationMode.orElseGet(() -> getMergeStrategy(file.getName().toUpperCase())); } - /** - * Gets configuration. - * - * @param file the file - * @return the configuration - */ public static Optional<FileBasedConfiguration> getConfiguration(File file) { FileBasedConfiguration builder = null; try { @@ -429,12 +277,11 @@ public class ConfigurationUtils { return ofNullable(builder); } - /** - * Gets collection generic type. - * - * @param field the field - * @return the collection generic type - */ + public static ConfigurationType getConfigType(File file) { + return Enum.valueOf(ConfigurationType.class, + file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf('.') + 1).toUpperCase()); + } + public static Class getCollectionGenericType(Field field) { Type type = field.getGenericType(); @@ -442,74 +289,44 @@ public class ConfigurationUtils { ParameterizedType paramType = (ParameterizedType) type; Type[] arr = paramType.getActualTypeArguments(); - - for (Type tp : arr) { - Class<?> clzz = (Class<?>) tp; - if (isWrapperClass(clzz)) { - return clzz; + if (arr.length > 0) { + Class<?> clazz = (Class<?>) arr[0]; + if (isWrapperClass(clazz)) { + return clazz; } else { - throw new RuntimeException("Collection of type " + clzz.getName() + " not supported."); + throw new RuntimeException("Collection of type " + clazz.getName() + " not supported."); } } } + return String[].class; } + public static boolean isWrapperClass(Class clazz) { + return clazz == String.class || clazz == Boolean.class || clazz == Character.class + || Number.class.isAssignableFrom(clazz); + } - /** - * Gets array class. - * - * @param clazz the clazz - * @return the array class - */ public static Class getArrayClass(Class clazz) { return ARRAY_CLASS_MAP.getOrDefault(clazz, null); } - /** - * Gets all class path resources. - * - * @return the all class path resources - */ public static List<URL> getAllClassPathResources() { return CPScanner.scanResources(new ResourceFilter()); } - /** - * Gets configuration builder. - * - * @param url the url - * @return the configuration builder - */ public static BasicConfigurationBuilder<FileBasedConfiguration> getConfigurationBuilder(URL url) { ConfigurationType configType = ConfigurationUtils.getConfigType(url); - ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builder = getFileBasedConfigurationReloadingFileBasedConfigurationBuilder( - configType); - builder.configure(new Parameters().fileBased().setURL(url) - .setListDelimiterHandler(new DefaultListDelimiterHandler(','))); + ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builder = + getFileBasedConfigurationReloadingFileBasedConfigurationBuilder(configType); + builder.configure( + new Parameters().fileBased().setURL(url).setListDelimiterHandler(new DefaultListDelimiterHandler(','))); return builder; } - /** - * Gets configuration builder. - * - * @param file the file - * @param autoSave the auto save - * @return the configuration builder - */ - public static BasicConfigurationBuilder<FileBasedConfiguration> getConfigurationBuilder(File file, - boolean autoSave) { - ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builder; - ConfigurationType configType = ConfigurationUtils.getConfigType(file); - builder = getFileBasedConfigurationReloadingFileBasedConfigurationBuilder(configType); - builder.configure(new Parameters().fileBased().setFile(file) - .setListDelimiterHandler(new DefaultListDelimiterHandler(','))); - builder.setAutoSave(autoSave); - return builder; - } + private static ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> + getFileBasedConfigurationReloadingFileBasedConfigurationBuilder(ConfigurationType configType) { - private static ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> getFileBasedConfigurationReloadingFileBasedConfigurationBuilder( - ConfigurationType configType) { ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builder; switch (configType) { case PROPERTIES: @@ -530,30 +347,28 @@ public class ConfigurationUtils { return builder; } - /** - * Read t. - * - * @param <T> the type parameter - * @param config the config - * @param clazz the clazz - * @param keyPrefix the key prefix - * @return the t - * @throws Exception the exception - */ - public static <T> T read(Configuration config, Class<T> clazz, String keyPrefix) - throws Exception { - Config confAnnot = - clazz.getAnnotation(Config.class); - if (confAnnot != null) { - keyPrefix += (confAnnot.key() + "."); + public static BasicConfigurationBuilder<FileBasedConfiguration> getConfigurationBuilder(File file, + boolean autoSave) { + ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builder; + ConfigurationType configType = ConfigurationUtils.getConfigType(file); + builder = getFileBasedConfigurationReloadingFileBasedConfigurationBuilder(configType); + builder.configure(new Parameters().fileBased().setFile(file) + .setListDelimiterHandler(new DefaultListDelimiterHandler(','))); + builder.setAutoSave(autoSave); + return builder; + } + + public static <T> T read(Configuration config, Class<T> clazz, String keyPrefix) throws Exception { + Config confAnnotation = clazz.getAnnotation(Config.class); + if (confAnnotation != null) { + keyPrefix += (confAnnotation.key() + "."); } T objToReturn = clazz.newInstance(); for (Field field : clazz.getDeclaredFields()) { - Config fieldConfAnnot = - field.getAnnotation(Config.class); - if (fieldConfAnnot != null) { + Config fieldAnnotation = field.getAnnotation(Config.class); + if (fieldAnnotation != null) { field.setAccessible(true); - field.set(objToReturn, config.getProperty(keyPrefix + fieldConfAnnot.key())); + field.set(objToReturn, config.getProperty(keyPrefix + fieldAnnotation.key())); } else if (field.getType().getAnnotation(Config.class) != null) { field.set(objToReturn, read(config, field.getType(), keyPrefix)); } @@ -561,42 +376,6 @@ public class ConfigurationUtils { return objToReturn; } - /** - * Gets property. - * - * @param config the config - * @param key the key - * @param processingHints the processing hints - * @return the property - */ - public static Object getProperty(Configuration config, String key, int processingHints) { - if (!isDirectLookup(processingHints)) { - if (config instanceof AgglomerateConfiguration) { - return ((AgglomerateConfiguration) config).getPropertyValue(key); - } else if (config instanceof CompositeConfiguration) { - CompositeConfiguration conf = (CompositeConfiguration) config; - for (int i = 0; i < conf.getNumberOfConfigurations(); i++) { - if (conf.getConfiguration(i) instanceof AgglomerateConfiguration) { - return ((AgglomerateConfiguration) conf.getConfiguration(i)).getPropertyValue(key); - } else if (isNodeSpecific(processingHints)) { - Object obj = conf.getConfiguration(i).getProperty(key); - if (obj != null) { - return obj; - } - } - } - } - } - return config.getProperty(key); - } - - /** - * Gets primitive array. - * - * @param collection the collection - * @param clazz the clazz - * @return the primitive array - */ public static Object getPrimitiveArray(Collection collection, Class clazz) { if (clazz == int.class) { int[] array = new int[collection.size()]; @@ -657,23 +436,6 @@ public class ConfigurationUtils { return null; } - /** - * Is wrapper class boolean. - * - * @param clazz the clazz - * @return the boolean - */ - public static boolean isWrapperClass(Class clazz) { - return clazz == String.class || clazz == Boolean.class || clazz == Character.class - || Number.class.isAssignableFrom(clazz); - } - - /** - * Gets collection string. - * - * @param input the input - * @return the collection string - */ public static String getCollectionString(String input) { Pattern pattern = Pattern.compile("^\\[(.*)\\]$"); Matcher matcher = pattern.matcher(input); @@ -683,26 +445,6 @@ public class ConfigurationUtils { return input; } - /** - * Is collection boolean. - * - * @param input the input - * @return the boolean - */ - public static boolean isCollection(String input) { - Pattern pattern = Pattern.compile("^\\[(.*)\\]$"); - Matcher matcher = pattern.matcher(input); - return matcher.matches(); - } - - /** - * Process variables if present string. - * - * @param tenant the tenant - * @param namespace the namespace - * @param data the data - * @return the string - */ public static String processVariablesIfPresent(String tenant, String namespace, String data) { Pattern pattern = Pattern.compile("^.*\\$\\{(.*)\\}.*"); Matcher matcher = pattern.matcher(data); @@ -727,12 +469,6 @@ public class ConfigurationUtils { } } - /** - * Gets file contents. - * - * @param path the path - * @return the file contents - */ public static String getFileContents(String path) { try { if (path != null) { @@ -744,12 +480,6 @@ public class ConfigurationUtils { return null; } - /** - * Gets file contents. - * - * @param path the path - * @return the file contents - */ public static String getFileContents(Path path) { try { if (path != null) { @@ -761,40 +491,6 @@ public class ConfigurationUtils { return null; } - /** - * Gets concrete collection. - * - * @param clazz the clazz - * @return the concrete collection - */ - public static Collection getConcreteCollection(Class clazz) { - switch (clazz.getName()) { - case "java.util.Collection": - case "java.util.List": - return new ArrayList<>(); - case "java.util.Set": - return new HashSet<>(); - case "java.util.SortedSet": - return new TreeSet<>(); - case "java.util.Queue": - return new ConcurrentLinkedQueue<>(); - case "java.util.Deque": - return new ArrayDeque<>(); - case "java.util.concurrent.TransferQueue": - return new LinkedTransferQueue<>(); - case "java.util.concurrent.BlockingQueue": - return new LinkedBlockingQueue<>(); - default: - return null; - } - } - - /** - * Gets default for. - * - * @param clazz the clazz - * @return the default for - */ public static Object getDefaultFor(Class clazz) { if (byte.class == clazz) { return new Byte("0"); @@ -814,12 +510,6 @@ public class ConfigurationUtils { return (char) 0; } - /** - * Gets compatible collection for abstract def. - * - * @param clazz the clazz - * @return the compatible collection for abstract def - */ public static Collection getCompatibleCollectionForAbstractDef(Class clazz) { if (BlockingQueue.class.isAssignableFrom(clazz)) { return getConcreteCollection(BlockingQueue.class); @@ -845,12 +535,33 @@ public class ConfigurationUtils { return null; } - /** - * Gets configuration repository key. - * - * @param array the array - * @return the configuration repository key - */ + public static Collection getConcreteCollection(Class clazz) { + switch (clazz.getName()) { + case "java.util.Collection": + case "java.util.List": + return new ArrayList<>(); + case "java.util.Set": + return new HashSet<>(); + case "java.util.SortedSet": + return new TreeSet<>(); + case "java.util.Queue": + return new ConcurrentLinkedQueue<>(); + case "java.util.Deque": + return new ArrayDeque<>(); + case "java.util.concurrent.TransferQueue": + return new LinkedTransferQueue<>(); + case "java.util.concurrent.BlockingQueue": + return new LinkedBlockingQueue<>(); + default: + return null; + } + } + + public static String getConfigurationRepositoryKey(File file) { + return getConfigurationRepositoryKey( + ConfigurationUtils.getNamespace(file).split(Constants.TENANT_NAMESPACE_SEPARATOR)); + } + public static String getConfigurationRepositoryKey(String[] array) { Deque<String> stack = new ArrayDeque<>(); stack.push(Constants.DEFAULT_TENANT); @@ -858,44 +569,61 @@ public class ConfigurationUtils { stack.push(element); } String toReturn = stack.pop(); - return stack.pop() + Constants.KEY_ELEMENTS_DELEMETER + toReturn; + return stack.pop() + Constants.KEY_ELEMENTS_DELIMETER + toReturn; } - /** - * Gets configuration repository key. - * - * @param file the file - * @return the configuration repository key - */ - public static String getConfigurationRepositoryKey(File file) { - return getConfigurationRepositoryKey( - ConfigurationUtils.getNamespace(file).split(Constants.TENANT_NAMESPACE_SAPERATOR)); + public static String getNamespace(File file) { + Optional<String> namespace = + getConfiguration(file).flatMap(ConfigurationUtils::getNamespace).map(String::toUpperCase); + return namespace.orElseGet(() -> getNamespace(file.getName().toUpperCase())); + } + + public static String getNamespace(String file) { + file = file.toUpperCase().substring(file.lastIndexOf('!') + 1); + file = file.substring(file.lastIndexOf('/') + 1); + Pattern pattern = Pattern.compile( + "CONFIG(-\\w*){0,1}(-" + "(" + ConfigurationMode.OVERRIDE + "|" + ConfigurationMode.MERGE + "|" + + ConfigurationMode.UNION + ")){0,1}" + "\\.(" + ConfigurationType.PROPERTIES.name() + "|" + + ConfigurationType.XML.name() + "|" + ConfigurationType.JSON.name() + "|" + + ConfigurationType.YAML.name() + ")$"); + Matcher matcher = pattern.matcher(file); + boolean b1 = matcher.matches(); + if (b1) { + if (matcher.group(1) != null) { + String moduleName = matcher.group(1).substring(1); + return moduleName.equalsIgnoreCase(ConfigurationMode.OVERRIDE.name()) || moduleName.equalsIgnoreCase( + ConfigurationMode.UNION.name()) || moduleName.equalsIgnoreCase(ConfigurationMode.MERGE.name()) + ? Constants.DEFAULT_NAMESPACE : moduleName; + } else { + return Constants.DEFAULT_NAMESPACE; + } + } else if (isConfig(file)) { + return Constants.DEFAULT_NAMESPACE; + } + + return null; } - /** - * Gets configuration repository key. - * - * @param url the url - * @return the configuration repository key - */ public static String getConfigurationRepositoryKey(URL url) { return getConfigurationRepositoryKey( - ConfigurationUtils.getNamespace(url).split(Constants.TENANT_NAMESPACE_SAPERATOR)); + ConfigurationUtils.getNamespace(url).split(Constants.TENANT_NAMESPACE_SEPARATOR)); + } + + public static String getNamespace(URL url) { + + Optional<String> namespace = + getConfiguration(url).flatMap(ConfigurationUtils::getNamespace).map(String::toUpperCase); + + return namespace.orElseGet(() -> getNamespace(url.getFile().toUpperCase())); } - /** - * To map linked hash map. - * - * @param config the config - * @return the linked hash map - */ public static LinkedHashMap toMap(Configuration config) { Iterator<String> iterator = config.getKeys(); LinkedHashMap<String, String> map = new LinkedHashMap<>(); while (iterator.hasNext()) { String key = iterator.next(); - if (!(key.equals(Constants.MODE_KEY) || key.equals(Constants.NAMESPACE_KEY) - || key.equals(Constants.LOAD_ORDER_KEY))) { + if (!(key.equals(Constants.MODE_KEY) || key.equals(Constants.NAMESPACE_KEY) || key.equals( + Constants.LOAD_ORDER_KEY))) { map.put(key, config.getProperty(key).toString()); } } @@ -903,13 +631,6 @@ public class ConfigurationUtils { return map; } - /** - * Diff map. - * - * @param orig the orig - * @param latest the latest - * @return the map - */ public static Map diff(LinkedHashMap orig, LinkedHashMap latest) { orig = new LinkedHashMap<>(orig); latest = new LinkedHashMap<>(latest); @@ -930,54 +651,52 @@ public class ConfigurationUtils { return new HashMap<>(latest); } - /** - * Is array boolean. - * - * @param tenant the tenant - * @param namespace the namespace - * @param key the key - * @param processingHints the processing hints - * @return the boolean - * @throws Exception the exception - */ - public static boolean isArray(String tenant, String namespace, String key, int processingHints) - throws Exception { + public static boolean isArray(String tenant, String namespace, String key, int processingHints) throws Exception { Object obj = ConfigurationUtils - .getProperty(ConfigurationRepository.lookup().getConfigurationFor(tenant, namespace), key, - processingHints); + .getProperty(ConfigurationRepository.lookup().getConfigurationFor(tenant, namespace), key, + processingHints); return (obj != null) && ConfigurationUtils.isCollection(obj.toString()); } - /** - * Is direct lookup boolean. - * - * @param hints the hints - * @return the boolean - */ - public static boolean isDirectLookup(int hints) { - return (hints & LATEST_LOOKUP.value()) == LATEST_LOOKUP.value(); + public static Object getProperty(Configuration config, String key, int processingHints) { + if (!isDirectLookup(processingHints)) { + if (config instanceof AgglomerateConfiguration) { + return ((AgglomerateConfiguration) config).getPropertyValue(key); + } else if (config instanceof CompositeConfiguration) { + CompositeConfiguration conf = (CompositeConfiguration) config; + for (int i = 0; i < conf.getNumberOfConfigurations(); i++) { + if (conf.getConfiguration(i) instanceof AgglomerateConfiguration) { + return ((AgglomerateConfiguration) conf.getConfiguration(i)).getPropertyValue(key); + } else if (isNodeSpecific(processingHints)) { + Object obj = conf.getConfiguration(i).getProperty(key); + if (obj != null) { + return obj; + } + } + } + } + } + return config.getProperty(key); } - /** - * Is external lookup boolean. - * - * @param hints the hints - * @return the boolean - */ - public static boolean isExternalLookup(int hints) { - return (hints & EXTERNAL_LOOKUP.value()) == EXTERNAL_LOOKUP.value(); + public static boolean isCollection(String input) { + Pattern pattern = Pattern.compile("^\\[(.*)\\]$"); + Matcher matcher = pattern.matcher(input); + return matcher.matches(); + } + + public static boolean isDirectLookup(int hints) { + return (hints & LATEST_LOOKUP.value()) == LATEST_LOOKUP.value(); } - /** - * Is node specific boolean. - * - * @param hints the hints - * @return the boolean - */ public static boolean isNodeSpecific(int hints) { return (hints & NODE_SPECIFIC.value()) == NODE_SPECIFIC.value(); } + public static boolean isExternalLookup(int hints) { + return (hints & EXTERNAL_LOOKUP.value()) == EXTERNAL_LOOKUP.value(); + } + public static boolean isZeroLengthArray(Class clazz, Object obj) { if (clazz.isArray() && clazz.getComponentType().isPrimitive()) { if (clazz.getComponentType() == int.class) { @@ -1002,12 +721,6 @@ public class ConfigurationUtils { return false; } - /** - * Checks if value is blank - * - * @param value - * @return - */ public static boolean isBlank(String value) { return value == null || value.trim().length() == 0; } |