package org.qfox.jestful.commons;

import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Created by yangchangpei on 17/3/28.
 */
public abstract class Config {
    private static Map<String, Properties> cache = new HashMap<String, Properties>();

    protected Config() {
        Class<?> clazz = this.getClass();
        Properties properties = load(clazz);

        for (String name : properties.stringPropertyNames()) {
            Class<?> c = clazz;
            while (c != Object.class) {
                try {
                    Field field = c.getDeclaredField(name);
                    if (Modifier.isFinal(field.getModifiers())) {
                        break;
                    }
                    if (field.getType() != String.class) {
                        break;
                    }
                    field.setAccessible(true);
                    field.set(this, properties.getProperty(name));
                    break;
                } catch (NoSuchFieldException e) {
                    c = c.getSuperclass();
                } catch (IllegalAccessException e) {
                    break;
                }
            }
        }

        try {
            PropertyDescriptor[] descriptors = Introspector.getBeanInfo(clazz).getPropertyDescriptors();
            for (PropertyDescriptor descriptor : descriptors) {
                if ("class".equals(descriptor.getName())) {
                    continue;
                }

                Method setter = descriptor.getWriteMethod();
                if (setter == null || setter.getParameterTypes().length != 1 || setter.getParameterTypes()[0] != String.class) {
                    continue;
                }

                String name = descriptor.getName();
                if (!properties.containsKey(name)) {
                    continue;
                }

                String value = properties.getProperty(name);
                setter.invoke(this, value);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Properties load(Class<?> clazz) throws IllegalArgumentException {
        String path = "/" + clazz.getName().replace('.', '/') + ".properties";
        Properties properties = cache.get(path);
        if (properties != null) {
            return properties;
        }
        synchronized (clazz) {
            if (properties != null) {
                return properties;
            }
            InputStream in = null;
            try {
                URL resource = clazz.getResource(path);
                if (resource == null) {
                    throw new FileNotFoundException(path);
                }
                properties = new Properties();
                properties.load(in = resource.openStream());
                cache.put(path, properties);
                return properties;
            } catch (IOException e) {
                throw new IllegalArgumentException(e);
            } finally {
                IOKit.close(in);
            }
        }
    }

}

@@ -32,78 +32,8 @@
 s;%0A%0A
-import java.beans.Introspector;%0Aimport java.beans.PropertyDescriptor;%0A
 impo
@@ -158,41 +158,8 @@
 ld;%0A
-import java.lang.reflect.Method;%0A
 impo
@@ -1364,32 +1364,34 @@
    %7D%0A        %7D%0A%0A
+//
         try %7B%0A  
@@ -1380,32 +1380,34 @@
 //        try %7B%0A
+//
             Prop
@@ -1491,24 +1491,26 @@
 criptors();%0A
+//
             
@@ -1557,24 +1557,26 @@
 criptors) %7B%0A
+//
             
@@ -1619,24 +1619,26 @@
 tName())) %7B%0A
+//
             
@@ -1647,32 +1647,34 @@
       continue;%0A
+//
                 
@@ -1667,33 +1667,37 @@
               %7D%0A
-%0A
+//%0A//
                 
@@ -1737,24 +1737,26 @@
 teMethod();%0A
+//
             
@@ -1864,32 +1864,34 @@
 String.class) %7B%0A
+//
                 
@@ -1896,32 +1896,34 @@
       continue;%0A
+//
                 
@@ -1916,33 +1916,37 @@
               %7D%0A
-%0A
+//%0A//
                 
@@ -1977,24 +1977,26 @@
 .getName();%0A
+//
             
@@ -2032,24 +2032,26 @@
 ey(name)) %7B%0A
+//
             
@@ -2068,16 +2068,18 @@
 ntinue;%0A
+//
         
@@ -2084,25 +2084,29 @@
           %7D%0A
-%0A
+//%0A//
             
@@ -2146,32 +2146,34 @@
 Property(name);%0A
+//
                 
@@ -2196,24 +2196,26 @@
 is, value);%0A
+//
             
@@ -2208,32 +2208,34 @@
 //            %7D%0A
+//
         %7D catch 
@@ -2242,32 +2242,34 @@
 (Exception e) %7B%0A
+//
             thro
@@ -2287,32 +2287,34 @@
 meException(e);%0A
+//
         %7D%0A    %7D%0A

Remove Introspector