ConfigInitializer.java 2.8 KB
Newer Older
1
package org.skywalking.apm.util;
2 3 4 5 6 7 8

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.LinkedList;
import java.util.Properties;
import java.util.logging.Logger;

9 10 11 12 13 14
/**
 * Init a class's static fields by a {@link Properties},
 * including static fields and static inner classes.
 * <p>
 * Created by wusheng on 2017/1/9.
 */
15
public class ConfigInitializer {
16
    private static final Logger logger = Logger.getLogger(ConfigInitializer.class.getName());
17 18 19 20 21

    public static void initialize(Properties properties, Class<?> rootConfigType) throws IllegalAccessException {
        initNextLevel(properties, rootConfigType, new ConfigDesc());
    }

22
    private static void initNextLevel(Properties properties, Class<?> recentConfigType,
P
pengys5 已提交
23
                                      ConfigDesc parentDesc) throws IllegalArgumentException, IllegalAccessException {
24 25
        for (Field field : recentConfigType.getFields()) {
            if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) {
26
                String configKey = (parentDesc + "." + field.getName()).toLowerCase();
27 28
                String value = properties.getProperty(configKey);
                if (value != null) {
29 30
                    Class<?> type = field.getType();
                    if (type.equals(int.class))
31
                        field.set(null, Integer.valueOf(value));
32
                    else if (type.equals(String.class))
33
                        field.set(null, value);
34
                    else if (type.equals(long.class))
35
                        field.set(null, Long.valueOf(value));
36
                    else if (type.equals(boolean.class))
37
                        field.set(null, Boolean.valueOf(value));
38
                    else if (type.isEnum())
P
pengys5 已提交
39
                        field.set(null, Enum.valueOf((Class<Enum>) type, value.toUpperCase()));
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
                }
            }
        }
        for (Class<?> innerConfiguration : recentConfigType.getClasses()) {
            parentDesc.append(innerConfiguration.getSimpleName());
            initNextLevel(properties, innerConfiguration, parentDesc);
            parentDesc.removeLastDesc();
        }
    }
}

class ConfigDesc {
    private LinkedList<String> descs = new LinkedList<String>();

    void append(String currentDesc) {
        descs.addLast(currentDesc);
    }

    void removeLastDesc() {
        descs.removeLast();
    }

    @Override
    public String toString() {
        if (descs.size() == 0) {
            return "";
        }
        StringBuilder ret = new StringBuilder(descs.getFirst());
        boolean first = true;
        for (String desc : descs) {
            if (first) {
                first = false;
                continue;
            }
            ret.append(".").append(desc);
        }
        return ret.toString();
    }
}