1 package org.onap.config;
3 import com.google.common.collect.ImmutableMap;
4 import com.virtlink.commons.configuration2.jackson.JsonConfiguration;
5 import net.sf.corn.cps.CPScanner;
6 import net.sf.corn.cps.ResourceFilter;
7 import org.apache.commons.configuration2.CompositeConfiguration;
8 import org.apache.commons.configuration2.Configuration;
9 import org.apache.commons.configuration2.FileBasedConfiguration;
10 import org.apache.commons.configuration2.PropertiesConfiguration;
11 import org.apache.commons.configuration2.XMLConfiguration;
12 import org.apache.commons.configuration2.builder.BasicConfigurationBuilder;
13 import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder;
14 import org.apache.commons.configuration2.builder.fluent.Configurations;
15 import org.apache.commons.configuration2.builder.fluent.Parameters;
16 import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
17 import org.apache.commons.configuration2.ex.ConfigurationException;
18 import org.apache.commons.io.IOUtils;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.onap.config.api.Config;
22 import org.onap.config.api.ConfigurationManager;
23 import org.onap.config.impl.ConfigurationRepository;
24 import org.onap.config.impl.YamlConfiguration;
25 import org.onap.config.impl.AgglomerateConfiguration;
26 import org.onap.config.impl.ConfigurationDataSource;
27 import org.onap.config.type.ConfigurationMode;
28 import org.onap.config.type.ConfigurationType;
30 import javax.sql.DataSource;
32 import java.lang.reflect.Field;
33 import java.lang.reflect.ParameterizedType;
34 import java.lang.reflect.Type;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.sql.Connection;
39 import java.sql.PreparedStatement;
40 import java.sql.ResultSet;
41 import java.sql.Statement;
42 import java.util.ArrayDeque;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.Collection;
46 import java.util.Deque;
47 import java.util.HashMap;
48 import java.util.HashSet;
49 import java.util.Iterator;
50 import java.util.LinkedHashMap;
51 import java.util.List;
53 import java.util.Optional;
54 import java.util.Queue;
56 import java.util.SortedSet;
57 import java.util.TreeSet;
58 import java.util.concurrent.BlockingQueue;
59 import java.util.concurrent.ConcurrentLinkedQueue;
60 import java.util.concurrent.Executors;
61 import java.util.concurrent.LinkedBlockingQueue;
62 import java.util.concurrent.LinkedTransferQueue;
63 import java.util.concurrent.ThreadFactory;
64 import java.util.concurrent.TransferQueue;
65 import java.util.regex.Matcher;
66 import java.util.regex.Pattern;
67 import java.util.stream.Collectors;
68 import java.util.stream.Stream;
70 import static com.google.common.collect.ImmutableMap.builder;
72 import static java.util.Optional.ofNullable;
73 import static org.onap.config.api.Hint.EXTERNAL_LOOKUP;
74 import static org.onap.config.api.Hint.LATEST_LOOKUP;
75 import static org.onap.config.api.Hint.NODE_SPECIFIC;
78 * The type Configuration utils.
80 public class ConfigurationUtils {
81 private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationUtils.class);
83 private ConfigurationUtils() {
86 private static ImmutableMap<Class, Class> arrayClassMap;
89 ImmutableMap.Builder<Class, Class> builder = builder();
90 builder.put(Byte.class, Byte[].class).put(Short.class, Short[].class)
91 .put(Integer.class, Integer[].class).put(Long.class, Long[].class)
92 .put(Float.class, Float[].class).put(Double.class, Double[].class)
93 .put(Boolean.class, Boolean[].class).put(Character.class, Character[].class)
94 .put(String.class, String[].class);
95 arrayClassMap = builder.build();
99 * Gets thread factory.
101 * @return the thread factory
103 public static ThreadFactory getThreadFactory() {
105 Thread thread = Executors.privilegedThreadFactory().newThread(r1);
106 thread.setDaemon(true);
114 * @param file the file
115 * @param recursive the recursive
116 * @param onlyDirectory the only directory
117 * @return the all files
119 public static Collection<File> getAllFiles(File file, boolean recursive, boolean onlyDirectory) {
120 ArrayList<File> collection = new ArrayList<>();
121 if (file.isDirectory() && file.exists()) {
122 File[] files = file.listFiles();
123 for (File innerFile : files) {
124 if (innerFile.isFile() && !onlyDirectory) {
125 collection.add(innerFile);
126 } else if (innerFile.isDirectory()) {
127 collection.add(innerFile);
129 collection.addAll(getAllFiles(innerFile, recursive, onlyDirectory));
138 * Gets comma saperated list.
140 * @param list the list
141 * @return the comma separated list
143 public static String getCommaSeparatedList(List list) {
144 return ((Stream<String>) list.stream().filter(o -> o != null && !o.toString().trim().isEmpty()).map(o -> o.toString().trim())).collect(Collectors.joining(","));
148 * Gets comma saperated list.
150 * @param list the list
151 * @return the comma saperated list
153 public static String getCommaSeparatedList(String[] list) {
154 return getCommaSeparatedList(list == null ? Arrays.asList() : Arrays.asList(list));
161 * @return the config type
163 public static ConfigurationType getConfigType(URL url) {
164 return Enum.valueOf(ConfigurationType.class,
165 url.getFile().substring(url.getFile().lastIndexOf('.') + 1).toUpperCase());
171 * @param file the file
172 * @return the config type
174 public static ConfigurationType getConfigType(File file) {
175 return Enum.valueOf(ConfigurationType.class,
176 file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf('.') + 1)
184 * @return the boolean
186 public static boolean isConfig(URL url) {
187 return isConfig(url.getFile());
193 * @param file the file
194 * @return the boolean
196 public static boolean isConfig(File file) {
197 return file != null && file.exists() && isConfig(file.getName());
203 * @param file the file
204 * @return the boolean
206 public static boolean isConfig(String file) {
207 file = file.toUpperCase().substring(file.lastIndexOf('!') + 1);
208 file = file.substring(file.lastIndexOf('/') + 1);
210 "CONFIG(-\\w*){0,1}(-" + "(" + ConfigurationMode.OVERRIDE + "|" + ConfigurationMode.MERGE
211 + "|" + ConfigurationMode.UNION + ")){0,1}" + "\\.("
212 + ConfigurationType.PROPERTIES.name() + "|" + ConfigurationType.XML.name() + "|"
213 + ConfigurationType.JSON.name() + "|" + ConfigurationType.YAML.name() + ")$")
214 || file.matches("CONFIG(.)*\\.(" + ConfigurationType.PROPERTIES.name() + "|"
215 + ConfigurationType.XML.name() + "|" + ConfigurationType.JSON.name() + "|"
216 + ConfigurationType.YAML.name() + ")$");
223 * @return the namespace
225 public static String getNamespace(URL url) {
227 Optional<String> namespace = getConfiguration(url).flatMap(ConfigurationUtils::getNamespace).map(String::toUpperCase);
229 return namespace.orElseGet(() -> getNamespace(url.getFile().toUpperCase()));
235 * @param file the file
236 * @return the namespace
238 public static String getNamespace(File file) {
239 Optional<String> namespace = getConfiguration(file)
240 .flatMap(ConfigurationUtils::getNamespace)
241 .map(String::toUpperCase);
242 return namespace.orElseGet(() -> getNamespace(file.getName().toUpperCase()));
245 private static Optional<String> getNamespace(Configuration config) {
246 return ofNullable(config)
247 .flatMap(configuration -> ofNullable(configuration.getString(Constants.NAMESPACE_KEY)))
248 .map(String::toUpperCase);
254 * @param file the file
255 * @return the namespace
257 public static String getNamespace(String file) {
258 file = file.toUpperCase().substring(file.lastIndexOf('!') + 1);
259 file = file.substring(file.lastIndexOf('/') + 1);
260 Pattern pattern = Pattern.compile(
261 "CONFIG(-\\w*){0,1}(-" + "(" + ConfigurationMode.OVERRIDE + "|" + ConfigurationMode.MERGE
262 + "|" + ConfigurationMode.UNION + ")){0,1}" + "\\.("
263 + ConfigurationType.PROPERTIES.name() + "|" + ConfigurationType.XML.name() + "|"
264 + ConfigurationType.JSON.name() + "|" + ConfigurationType.YAML.name() + ")$");
265 Matcher matcher = pattern.matcher(file);
266 boolean b1 = matcher.matches();
268 if (matcher.group(1) != null) {
269 String moduleName = matcher.group(1).substring(1);
270 return moduleName.equalsIgnoreCase(ConfigurationMode.OVERRIDE.name())
271 || moduleName.equalsIgnoreCase(ConfigurationMode.UNION.name())
272 || moduleName.equalsIgnoreCase(ConfigurationMode.MERGE.name())
273 ? Constants.DEFAULT_NAMESPACE : moduleName;
275 return Constants.DEFAULT_NAMESPACE;
277 } else if (isConfig(file)) {
278 return Constants.DEFAULT_NAMESPACE;
285 * Gets merge strategy.
288 * @return the merge strategy
290 public static ConfigurationMode getMergeStrategy(URL url) {
291 Optional<ConfigurationMode> configurationMode = getConfiguration(url).flatMap(ConfigurationUtils::getMergeStrategy).flatMap(ConfigurationUtils::convertConfigurationMode);
292 return configurationMode.orElseGet(() -> getMergeStrategy(url.getFile().toUpperCase()));
295 private static Optional<ConfigurationMode> convertConfigurationMode(String configMode) {
296 ConfigurationMode configurationMode = null;
298 configurationMode = ConfigurationMode.valueOf(configMode);
299 } catch (Exception exception) {
300 LOGGER.error("Could not find convert {} into configuration mode", configMode);
302 return Optional.ofNullable(configurationMode);
305 private static Optional<String> getMergeStrategy(Configuration config) {
306 return ofNullable(config)
307 .flatMap(configuration -> ofNullable(configuration.getString(Constants.MODE_KEY)))
308 .map(String::toUpperCase);
313 * Gets merge strategy.
315 * @param file the file
316 * @return the merge strategy
318 public static ConfigurationMode getMergeStrategy(File file) {
319 Optional<ConfigurationMode> configurationMode = getConfiguration(file).flatMap(ConfigurationUtils::getMergeStrategy).flatMap(ConfigurationUtils::convertConfigurationMode);
320 return configurationMode.orElseGet(() -> getMergeStrategy(file.getName().toUpperCase()));
324 * Gets merge strategy.
326 * @param file the file
327 * @return the merge strategy
329 public static ConfigurationMode getMergeStrategy(String file) {
330 file = file.toUpperCase().substring(file.lastIndexOf('!') + 1);
331 file = file.substring(file.lastIndexOf('/') + 1);
332 Pattern pattern = Pattern.compile(
333 "CONFIG(-\\w*){0,1}(-" + "(" + ConfigurationMode.OVERRIDE + "|" + ConfigurationMode.MERGE
334 + "|" + ConfigurationMode.UNION + ")){0,1}" + "\\.("
335 + ConfigurationType.PROPERTIES.name() + "|" + ConfigurationType.XML.name() + "|"
336 + ConfigurationType.JSON.name() + "|" + ConfigurationType.YAML.name() + ")$");
337 Matcher matcher = pattern.matcher(file);
338 boolean b1 = matcher.matches();
340 for (int i = 1; i <= matcher.groupCount(); i++) {
341 String modeName = matcher.group(i);
342 if (modeName != null) {
343 modeName = modeName.substring(1);
346 return Enum.valueOf(ConfigurationMode.class, modeName);
347 } catch (Exception exception) {
357 * Gets configuration.
360 * @return the configuration
362 public static Optional<FileBasedConfiguration> getConfiguration(URL url) {
363 FileBasedConfiguration builder = null;
365 ConfigurationType configType = ConfigurationUtils.getConfigType(url);
366 switch (configType) {
368 builder = new Configurations().fileBased(PropertiesConfiguration.class, url);
371 builder = new Configurations().fileBased(XMLConfiguration.class, url);
374 builder = new Configurations().fileBased(JsonConfiguration.class, url);
377 builder = new Configurations().fileBased(YamlConfiguration.class, url);
380 throw new ConfigurationException("Configuration type not supported:" + configType);
382 } catch (ConfigurationException exception) {
383 exception.printStackTrace();
385 return ofNullable(builder);
389 * Gets configuration.
391 * @param file the file
392 * @return the configuration
394 public static Optional<FileBasedConfiguration> getConfiguration(File file) {
395 FileBasedConfiguration builder = null;
397 ConfigurationType configType = ConfigurationUtils.getConfigType(file);
398 switch (configType) {
400 builder = new Configurations().fileBased(PropertiesConfiguration.class, file);
403 builder = new Configurations().fileBased(XMLConfiguration.class, file);
406 builder = new Configurations().fileBased(JsonConfiguration.class, file);
409 builder = new Configurations().fileBased(YamlConfiguration.class, file);
412 throw new ConfigurationException("Configuration type not supported:" + configType);
414 } catch (ConfigurationException exception) {
415 exception.printStackTrace();
417 return ofNullable(builder);
421 * Gets collection generic type.
423 * @param field the field
424 * @return the collection generic type
426 public static Class getCollectionGenericType(Field field) {
427 Type type = field.getGenericType();
429 if (type instanceof ParameterizedType) {
431 ParameterizedType paramType = (ParameterizedType) type;
432 Type[] arr = paramType.getActualTypeArguments();
434 for (Type tp : arr) {
435 Class<?> clzz = (Class<?>) tp;
436 if (isWrapperClass(clzz)) {
439 throw new RuntimeException("Collection of type " + clzz.getName() + " not supported.");
443 return String[].class;
450 * @param clazz the clazz
451 * @return the array class
453 public static Class getArrayClass(Class clazz) {
454 return arrayClassMap.getOrDefault(clazz, null);
458 * Gets all class path resources.
460 * @return the all class path resources
462 public static List<URL> getAllClassPathResources() {
463 return CPScanner.scanResources(new ResourceFilter());
467 * Execute ddlsql boolean.
470 * @return the boolean
471 * @throws Exception the exception
473 public static boolean executeDdlSql(String sql) throws Exception {
474 DataSource datasource = ConfigurationDataSource.lookup();
475 if (datasource == null) {
476 System.err.println("DB configuration not found. Configuration management will be using "
477 + "in-memory persistence.");
480 try (Connection con = datasource.getConnection(); Statement stmt = con.createStatement()) {
481 stmt.executeQuery(sql);
482 } catch (Exception exception) {
483 System.err.println("Datasource initialization error. Configuration management will be using in-memory persistence.");
490 * Gets configuration builder.
493 * @return the configuration builder
495 public static BasicConfigurationBuilder<FileBasedConfiguration> getConfigurationBuilder(URL url) {
496 ConfigurationType configType = ConfigurationUtils.getConfigType(url);
497 ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builder = getFileBasedConfigurationReloadingFileBasedConfigurationBuilder(
499 builder.configure(new Parameters().fileBased().setURL(url)
500 .setListDelimiterHandler(new DefaultListDelimiterHandler(',')));
505 * Gets configuration builder.
507 * @param file the file
508 * @param autoSave the auto save
509 * @return the configuration builder
511 public static BasicConfigurationBuilder<FileBasedConfiguration> getConfigurationBuilder(File file,
513 ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builder;
514 ConfigurationType configType = ConfigurationUtils.getConfigType(file);
515 builder = getFileBasedConfigurationReloadingFileBasedConfigurationBuilder(configType);
516 builder.configure(new Parameters().fileBased().setFile(file)
517 .setListDelimiterHandler(new DefaultListDelimiterHandler(',')));
518 builder.setAutoSave(autoSave);
522 private static ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> getFileBasedConfigurationReloadingFileBasedConfigurationBuilder(
523 ConfigurationType configType) {
524 ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builder;
525 switch (configType) {
527 builder = new ReloadingFileBasedConfigurationBuilder<>(PropertiesConfiguration.class);
530 builder = new ReloadingFileBasedConfigurationBuilder<>(XMLConfiguration.class);
533 builder = new ReloadingFileBasedConfigurationBuilder<>(JsonConfiguration.class);
536 builder = new ReloadingFileBasedConfigurationBuilder<>(YamlConfiguration.class);
539 throw new IllegalArgumentException("Configuration type not supported:" + configType);
546 * Execute select sql collection.
549 * @param params the params
550 * @return the collection
551 * @throws Exception the exception
553 public static Collection<String> executeSelectSql(String sql, String[] params) throws Exception {
554 Collection<String> coll = new ArrayList<>();
555 DataSource datasource = ConfigurationDataSource.lookup();
556 try (Connection con = datasource.getConnection();
557 PreparedStatement stmt = con.prepareStatement(sql)) {
558 if (params != null) {
559 for (int i = 0; i < params.length; i++) {
560 stmt.setString(i + 1, params[i]);
564 try (ResultSet rs = stmt.executeQuery()) {
567 coll.add(rs.getString(1));
571 } catch (Exception exception) {
572 //exception.printStackTrace();
580 * Execute insert sql boolean.
583 * @param params the params
584 * @return the boolean
585 * @throws Exception the exception
587 public static boolean executeInsertSql(String sql, Object[] params) throws Exception {
588 DataSource datasource = ConfigurationDataSource.lookup();
589 try (Connection con = datasource.getConnection();
590 PreparedStatement stmt = con.prepareStatement(sql)) {
591 if (params != null) {
593 for (Object obj : params) {
597 switch (obj.getClass().getName()) {
598 case "java.lang.String":
599 stmt.setString(++counter, obj.toString());
601 case "java.lang.Integer":
602 stmt.setInt(++counter, ((Integer) obj).intValue());
604 case "java.lang.Long":
605 stmt.setLong(++counter, ((Long) obj).longValue());
608 stmt.setString(++counter, obj.toString());
613 stmt.executeUpdate();
615 } catch (Exception exception) {
616 exception.printStackTrace();
624 * @param <T> the type parameter
625 * @param config the config
626 * @param clazz the clazz
627 * @param keyPrefix the key prefix
629 * @throws Exception the exception
631 public static <T> T read(Configuration config, Class<T> clazz, String keyPrefix)
634 clazz.getAnnotation(Config.class);
635 if (confAnnot != null) {
636 keyPrefix += (confAnnot.key() + ".");
638 T objToReturn = clazz.newInstance();
639 for (Field field : clazz.getDeclaredFields()) {
640 Config fieldConfAnnot =
641 field.getAnnotation(Config.class);
642 if (fieldConfAnnot != null) {
643 field.setAccessible(true);
644 field.set(objToReturn, config.getProperty(keyPrefix + fieldConfAnnot.key()));
645 } else if (field.getType().getAnnotation(Config.class) != null) {
646 field.set(objToReturn, read(config, field.getType(), keyPrefix));
653 * Gets db configuration builder.
655 * @param configName the config name
656 * @return the db configuration builder
657 * @throws Exception the exception
659 public static BasicConfigurationBuilder<AgglomerateConfiguration> getDbConfigurationBuilder(
660 String configName) throws Exception {
661 Configuration dbConfig = ConfigurationRepository.lookup()
662 .getConfigurationFor(Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE);
663 BasicConfigurationBuilder<AgglomerateConfiguration> builder =
664 new BasicConfigurationBuilder<AgglomerateConfiguration>(AgglomerateConfiguration.class);
666 new Parameters().database()
667 .setDataSource(ConfigurationDataSource.lookup())
668 .setTable(dbConfig.getString("config.Table"))
669 .setKeyColumn(dbConfig.getString("configKey"))
670 .setValueColumn(dbConfig.getString("configValue"))
671 .setConfigurationNameColumn(dbConfig.getString("configNameColumn"))
672 .setConfigurationName(configName)
681 * @param config the config
683 * @param processingHints the processing hints
684 * @return the property
686 public static Object getProperty(Configuration config, String key, int processingHints) {
687 if (!isDirectLookup(processingHints)) {
688 if (config instanceof AgglomerateConfiguration) {
689 return ((AgglomerateConfiguration) config).getPropertyValue(key);
690 } else if (config instanceof CompositeConfiguration) {
691 CompositeConfiguration conf = (CompositeConfiguration) config;
692 for (int i = 0; i < conf.getNumberOfConfigurations(); i++) {
693 if (conf.getConfiguration(i) instanceof AgglomerateConfiguration) {
694 return ((AgglomerateConfiguration) conf.getConfiguration(i)).getPropertyValue(key);
695 } else if (isNodeSpecific(processingHints)) {
696 Object obj = conf.getConfiguration(i).getProperty(key);
704 return config.getProperty(key);
708 * Gets primitive array.
710 * @param collection the collection
711 * @param clazz the clazz
712 * @return the primitive array
714 public static Object getPrimitiveArray(Collection collection, Class clazz) {
715 if (clazz == int.class) {
716 int[] array = new int[collection.size()];
717 Object[] objArray = collection.toArray();
718 for (int i = 0; i < collection.size(); i++) {
719 array[i] = (int) objArray[i];
723 if (clazz == byte.class) {
724 byte[] array = new byte[collection.size()];
725 Object[] objArray = collection.toArray();
726 for (int i = 0; i < collection.size(); i++) {
727 array[i] = (byte) objArray[i];
731 if (clazz == short.class) {
732 short[] array = new short[collection.size()];
733 Object[] objArray = collection.toArray();
734 for (int i = 0; i < collection.size(); i++) {
735 array[i] = (short) objArray[i];
739 if (clazz == long.class) {
740 long[] array = new long[collection.size()];
741 Object[] objArray = collection.toArray();
742 for (int i = 0; i < collection.size(); i++) {
743 array[i] = (long) objArray[i];
747 if (clazz == float.class) {
748 float[] array = new float[collection.size()];
749 Object[] objArray = collection.toArray();
750 for (int i = 0; i < collection.size(); i++) {
751 array[i] = (float) objArray[i];
755 if (clazz == double.class) {
756 double[] array = new double[collection.size()];
757 Object[] objArray = collection.toArray();
758 for (int i = 0; i < collection.size(); i++) {
759 array[i] = (double) objArray[i];
763 if (clazz == boolean.class) {
764 boolean[] array = new boolean[collection.size()];
765 Object[] objArray = collection.toArray();
766 for (int i = 0; i < collection.size(); i++) {
767 array[i] = (boolean) objArray[i];
776 * Is wrapper class boolean.
778 * @param clazz the clazz
779 * @return the boolean
781 public static boolean isWrapperClass(Class clazz) {
782 return clazz == String.class || clazz == Boolean.class || clazz == Character.class
783 || Number.class.isAssignableFrom(clazz);
787 * Gets collection string.
789 * @param input the input
790 * @return the collection string
792 public static String getCollectionString(String input) {
793 Pattern pattern = Pattern.compile("^\\[(.*)\\]$");
794 Matcher matcher = pattern.matcher(input);
795 if (matcher.matches()) {
796 input = matcher.group(1);
802 * Is collection boolean.
804 * @param input the input
805 * @return the boolean
807 public static boolean isCollection(String input) {
808 Pattern pattern = Pattern.compile("^\\[(.*)\\]$");
809 Matcher matcher = pattern.matcher(input);
810 return matcher.matches();
814 * Process variables if present string.
816 * @param tenant the tenant
817 * @param namespace the namespace
818 * @param data the data
821 public static String processVariablesIfPresent(String tenant, String namespace, String data) {
822 Pattern pattern = Pattern.compile("^.*\\$\\{(.*)\\}.*");
823 Matcher matcher = pattern.matcher(data);
824 if (matcher.matches()) {
825 String key = matcher.group(1);
826 if (key.toUpperCase().startsWith("ENV:")) {
827 String envValue = System.getenv(key.substring(4));
828 return processVariablesIfPresent(tenant, namespace, data.replaceAll("\\$\\{" + key + "\\}",
829 envValue == null ? "" : envValue.replace("\\", "\\\\")));
830 } else if (key.toUpperCase().startsWith("SYS:")) {
831 String sysValue = System.getProperty(key.substring(4));
832 return processVariablesIfPresent(tenant, namespace, data.replaceAll("\\$\\{" + key + "\\}",
833 sysValue == null ? "" : sysValue.replace("\\", "\\\\")));
835 String propertyValue = ConfigurationUtils.getCollectionString(
836 ConfigurationManager.lookup().getAsStringValues(tenant, namespace, key).toString());
837 return processVariablesIfPresent(tenant, namespace, data.replaceAll("\\$\\{" + key + "\\}",
838 propertyValue == null ? "" : propertyValue.replace("\\", "\\\\")));
846 * Gets file contents.
848 * @param path the path
849 * @return the file contents
851 public static String getFileContents(String path) {
854 return IOUtils.toString(new URL(path));
856 } catch (Exception exception) {
857 exception.printStackTrace();
863 * Gets file contents.
865 * @param path the path
866 * @return the file contents
868 public static String getFileContents(Path path) {
871 return new String(Files.readAllBytes(path));
873 } catch (Exception exception) {
874 exception.printStackTrace();
880 * Gets concrete collection.
882 * @param clazz the clazz
883 * @return the concrete collection
885 public static Collection getConcreteCollection(Class clazz) {
886 switch (clazz.getName()) {
887 case "java.util.Collection":
888 case "java.util.List":
889 return new ArrayList<>();
890 case "java.util.Set":
891 return new HashSet<>();
892 case "java.util.SortedSet":
893 return new TreeSet<>();
894 case "java.util.Queue":
895 return new ConcurrentLinkedQueue<>();
896 case "java.util.Deque":
897 return new ArrayDeque<>();
898 case "java.util.concurrent.TransferQueue":
899 return new LinkedTransferQueue<>();
900 case "java.util.concurrent.BlockingQueue":
901 return new LinkedBlockingQueue<>();
910 * @param clazz the clazz
911 * @return the default for
913 public static Object getDefaultFor(Class clazz) {
914 if (byte.class == clazz) {
915 return new Byte("0");
916 } else if (short.class == clazz) {
917 return new Short("0");
918 } else if (int.class == clazz) {
919 return new Integer("0");
920 } else if (float.class == clazz) {
921 return new Float("0");
922 } else if (long.class == clazz) {
923 return new Long("0");
924 } else if (double.class == clazz) {
925 return new Double("0");
926 } else if (boolean.class == clazz) {
927 return Boolean.FALSE;
933 * Gets compatible collection for abstract def.
935 * @param clazz the clazz
936 * @return the compatible collection for abstract def
938 public static Collection getCompatibleCollectionForAbstractDef(Class clazz) {
939 if (BlockingQueue.class.isAssignableFrom(clazz)) {
940 return getConcreteCollection(BlockingQueue.class);
942 if (TransferQueue.class.isAssignableFrom(clazz)) {
943 return getConcreteCollection(TransferQueue.class);
945 if (Deque.class.isAssignableFrom(clazz)) {
946 return getConcreteCollection(Deque.class);
948 if (Queue.class.isAssignableFrom(clazz)) {
949 return getConcreteCollection(Queue.class);
951 if (SortedSet.class.isAssignableFrom(clazz)) {
952 return getConcreteCollection(SortedSet.class);
954 if (Set.class.isAssignableFrom(clazz)) {
955 return getConcreteCollection(Set.class);
957 if (List.class.isAssignableFrom(clazz)) {
958 return getConcreteCollection(List.class);
964 * Gets configuration repository key.
966 * @param array the array
967 * @return the configuration repository key
969 public static String getConfigurationRepositoryKey(String[] array) {
970 Deque<String> stack = new ArrayDeque<>();
971 stack.push(Constants.DEFAULT_TENANT);
972 for (String element : array) {
975 String toReturn = stack.pop();
976 return stack.pop() + Constants.KEY_ELEMENTS_DELEMETER + toReturn;
980 * Gets configuration repository key.
982 * @param file the file
983 * @return the configuration repository key
985 public static String getConfigurationRepositoryKey(File file) {
986 return getConfigurationRepositoryKey(
987 ConfigurationUtils.getNamespace(file).split(Constants.TENANT_NAMESPACE_SAPERATOR));
991 * Gets configuration repository key.
994 * @return the configuration repository key
996 public static String getConfigurationRepositoryKey(URL url) {
997 return getConfigurationRepositoryKey(
998 ConfigurationUtils.getNamespace(url).split(Constants.TENANT_NAMESPACE_SAPERATOR));
1002 * To map linked hash map.
1004 * @param config the config
1005 * @return the linked hash map
1007 public static LinkedHashMap toMap(Configuration config) {
1008 Iterator<String> iterator = config.getKeys();
1009 LinkedHashMap<String, String> map = new LinkedHashMap<>();
1010 while (iterator.hasNext()) {
1011 String key = iterator.next();
1012 if (!(key.equals(Constants.MODE_KEY) || key.equals(Constants.NAMESPACE_KEY)
1013 || key.equals(Constants.LOAD_ORDER_KEY))) {
1014 map.put(key, config.getProperty(key).toString());
1024 * @param orig the orig
1025 * @param latest the latest
1028 public static Map diff(LinkedHashMap orig, LinkedHashMap latest) {
1029 orig = new LinkedHashMap<>(orig);
1030 latest = new LinkedHashMap<>(latest);
1031 List<String> set = new ArrayList(orig.keySet());
1032 for (String key : set) {
1033 if (latest.remove(key, orig.get(key))) {
1037 Set<String> keys = latest.keySet();
1038 for (String key : keys) {
1041 set = new ArrayList(orig.keySet());
1042 for (String key : set) {
1043 latest.put(key, "");
1045 return new HashMap<>(latest);
1051 * @param tenant the tenant
1052 * @param namespace the namespace
1053 * @param key the key
1054 * @param processingHints the processing hints
1055 * @return the boolean
1056 * @throws Exception the exception
1058 public static boolean isArray(String tenant, String namespace, String key, int processingHints)
1060 Object obj = ConfigurationUtils
1061 .getProperty(ConfigurationRepository.lookup().getConfigurationFor(tenant, namespace), key,
1063 return (obj != null) && ConfigurationUtils.isCollection(obj.toString());
1067 * Is direct lookup boolean.
1069 * @param hints the hints
1070 * @return the boolean
1072 public static boolean isDirectLookup(int hints) {
1073 return (hints & LATEST_LOOKUP.value()) == LATEST_LOOKUP.value();
1077 * Is external lookup boolean.
1079 * @param hints the hints
1080 * @return the boolean
1082 public static boolean isExternalLookup(int hints) {
1083 return (hints & EXTERNAL_LOOKUP.value()) == EXTERNAL_LOOKUP.value();
1087 * Is node specific boolean.
1089 * @param hints the hints
1090 * @return the boolean
1092 public static boolean isNodeSpecific(int hints) {
1093 return (hints & NODE_SPECIFIC.value()) == NODE_SPECIFIC.value();
1096 public static boolean isZeroLengthArray(Class clazz, Object obj) {
1097 if (clazz.isArray() && clazz.getComponentType().isPrimitive()) {
1098 if (clazz.getComponentType() == int.class) {
1099 return ((int[]) obj).length == 0;
1100 } else if (clazz.getComponentType() == byte.class) {
1101 return ((byte[]) obj).length == 0;
1102 } else if (clazz.getComponentType() == short.class) {
1103 return ((short[]) obj).length == 0;
1104 } else if (clazz.getComponentType() == float.class) {
1105 return ((float[]) obj).length == 0;
1106 } else if (clazz.getComponentType() == boolean.class) {
1107 return ((boolean[]) obj).length == 0;
1108 } else if (clazz.getComponentType() == double.class) {
1109 return ((double[]) obj).length == 0;
1110 } else if (clazz.getComponentType() == long.class) {
1111 return ((long[]) obj).length == 0;
1113 return ((Object[]) obj).length == 0;
1121 * Checks if value is blank
1126 public static boolean isBlank(String value) {
1127 return value == null || value.trim().length() == 0;