1 |
package dk.thoerup.webconfig; |
2 |
|
3 |
|
4 |
import java.lang.reflect.Field; |
5 |
import java.lang.reflect.Method; |
6 |
|
7 |
|
8 |
|
9 |
abstract public class ConfigLoader { |
10 |
// TODO: should this class be enhanced with a saveConfig() and a setValue() ?? |
11 |
|
12 |
|
13 |
public abstract String getValue(String paramName); |
14 |
|
15 |
public void loadConfig(Object configObject) { |
16 |
|
17 |
Field fields[] = configObject.getClass().getDeclaredFields(); |
18 |
|
19 |
for(Field field : fields) { |
20 |
field.setAccessible(true); |
21 |
|
22 |
ConfigVariable anno = field.getAnnotation(ConfigVariable.class); |
23 |
if (anno == null) |
24 |
continue; |
25 |
|
26 |
String propertyName = anno.propertyname().equals("") ? field.getName() : anno.propertyname(); |
27 |
|
28 |
String value = getValue( propertyName ); |
29 |
if (value == null) { |
30 |
if (anno.mandatory()) { |
31 |
//TODO: find another exception to throw here |
32 |
throw new RuntimeException("Could not find a value to this field: " + field.getName() ); |
33 |
} else { |
34 |
System.out.println("Could not find a value to this field: " + field.getName() ); |
35 |
continue; |
36 |
} |
37 |
} |
38 |
|
39 |
setFieldValue(field,configObject,value); |
40 |
} |
41 |
|
42 |
} |
43 |
|
44 |
public static void setFieldValue(Field field, Object configObject, String value) { |
45 |
|
46 |
field.setAccessible(true); |
47 |
try { |
48 |
|
49 |
if (field.getType().getName().equals("int") ) { |
50 |
field.setInt(configObject, Integer.parseInt(value) ); |
51 |
} else if (field.getType().getName().equals("boolean") ) { |
52 |
value = value.toLowerCase(); |
53 |
if (! (value.equals("true") || value.equals("false"))) { |
54 |
//TODO: Find another exception to throw here |
55 |
throw new RuntimeException("Only 'true' or 'false' as values for boolean field"); |
56 |
} |
57 |
field.setBoolean(configObject, Boolean.parseBoolean(value) ); |
58 |
} else if (field.getType().getName().equals("long") ) { |
59 |
field.setLong(configObject, Long.parseLong(value) ); |
60 |
} else if (field.getType().getName().equals("short") ) { |
61 |
field.setShort(configObject, Short.parseShort(value) ); |
62 |
} else if (field.getType().getName().equals("double") ) { |
63 |
field.setDouble(configObject, Double.parseDouble(value) ); |
64 |
} else if (field.getType().getName().equals("float") ) { |
65 |
field.setFloat(configObject, Float.parseFloat(value) ); |
66 |
} else if (field.getType().getName().equals("char") ) { |
67 |
field.setFloat(configObject, value.charAt(0) ); |
68 |
} else if (field.getType().isEnum() ) { |
69 |
//throw new RuntimeException("Enums are not supported (yet)"); |
70 |
Object enumval = parseEnumValue(field.getType(), configObject,value); |
71 |
field.set(configObject, enumval ); |
72 |
} else { |
73 |
//System.out.print(" >" + field.getType().getName() ); |
74 |
field.set(configObject, value ); |
75 |
} |
76 |
} catch (IllegalAccessException e) { |
77 |
//this should never happen since we already have called setAccessible(true) |
78 |
throw new RuntimeException("How did we get here?", e); |
79 |
} |
80 |
} |
81 |
|
82 |
protected static Object parseEnumValue(Class<?> cls, Object configObject, String value) { |
83 |
try { |
84 |
Class<?> argTypes[] = {String.class}; |
85 |
Object args[] = {value}; |
86 |
|
87 |
Method valueOf = cls.getMethod("valueOf", argTypes); |
88 |
return valueOf.invoke(configObject, args); |
89 |
|
90 |
} catch (Exception e) { |
91 |
throw new RuntimeException(e); |
92 |
} |
93 |
} |
94 |
|
95 |
} |