2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.config.api;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.List;
24 public class DynamicConfiguration<T> {
26 private String tenant;
28 private String namespace;
32 private Configuration configuration;
36 private T defaultValue;
38 public static <K> DynamicConfiguration<List<K>> getDynConfiguration(String tenant, String namespace, String key,
39 Class<K> clazz, K defaultValue, Configuration configuration) {
40 if (clazz.isPrimitive()) {
41 throw new RuntimeException("Only Wrapper classes like Integer, Long, Double, "
42 + "Boolean etc including String are supported.");
44 return getDynamicConfiguration(tenant, namespace, key, getArrayClass(clazz),
45 Collections.singletonList(defaultValue), configuration);
48 public static <T> DynamicConfiguration<T> getDynamicConfiguration(String tenant, String namespace, String key,
49 Class<T> clazz, T defaultValue, Configuration configuration) {
50 DynamicConfiguration<T> dynamicConfiguration = new DynamicConfiguration<>();
51 dynamicConfiguration.tenant = tenant;
52 dynamicConfiguration.namespace = namespace;
53 dynamicConfiguration.key = key;
54 dynamicConfiguration.clazz = clazz;
55 dynamicConfiguration.defaultValue = defaultValue;
56 dynamicConfiguration.configuration = configuration;
57 return dynamicConfiguration;
60 public static Class getArrayClass(Class clazz) {
61 Class arrayClass = null;
62 switch (clazz.getName()) {
63 case "java.lang.Byte":
64 arrayClass = Byte[].class;
66 case "java.lang.Short":
67 arrayClass = Short[].class;
69 case "java.lang.Integer":
70 arrayClass = Integer[].class;
72 case "java.lang.Long":
73 arrayClass = Long[].class;
75 case "java.lang.Float":
76 arrayClass = Float[].class;
78 case "java.lang.Double":
79 arrayClass = Double[].class;
81 case "java.lang.Boolean":
82 arrayClass = Boolean[].class;
84 case "java.lang.Character":
85 arrayClass = Character[].class;
87 case "java.lang.String":
88 arrayClass = String[].class;
96 Object toReturn = configuration.get(tenant, namespace, key, clazz, Hint.LATEST_LOOKUP, Hint.EXTERNAL_LOOKUP,
98 if (toReturn != null && toReturn.getClass().isArray()) {
99 toReturn = Arrays.asList((Object[]) toReturn);
101 return toReturn == null ? defaultValue : (T) toReturn;