java-org.hwo/src/org/hwo/configuration/ConfigurableObjects.java

160 lines
3.9 KiB
Java

package org.hwo.configuration;
import static org.hwo.logging.Logging.log;
import static org.hwo.logging.LogLevel.*;
import java.io.ObjectInputStream.GetField;
import java.lang.reflect.Field;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.StringTokenizer;
import org.hwo.ByteArrayHelper;
import org.hwo.ByteArrayHexlifier;
public class ConfigurableObjects {
public static Field[] getConfigurableAttributes(Class<?> clazz){
LinkedList<Field> cfl = new LinkedList<Field>();
if (clazz.getAnnotation(ConfigurableObject.class)!=null){
Field[] fields = clazz.getDeclaredFields();
for (Field f: fields){
if (f.getAnnotation(ConfigurableAttribute.class)!=null){
f.setAccessible(true);
cfl.add(f);
}
}
if (clazz.getSuperclass()!=null){
for (Field f:getConfigurableAttributes(clazz.getSuperclass()))
cfl.add(f);
}
}
return cfl.toArray(new Field[0]);
}
public static Field[] getConfigurableAttributes(Object o){
return getConfigurableAttributes(o.getClass());
}
public static String getValueAsString(Object value){
String s = value.toString();
return ByteArrayHexlifier.hexlify(s);
}
public static Object getValueFromString(String _value,Class<?> type) throws ClassCastException{
String value = ByteArrayHexlifier.unhexlify(_value);
if (type.equals(String.class))
return new String(value);
if (type.equals(Integer.class))
return Integer.parseInt(value);
if (type.equals(Float.class))
return Float.parseFloat(value);
if (type.equals(Double.class))
return Double.parseDouble(value);
if (type.equals(Boolean.class))
return Boolean.parseBoolean(value);
if (type.isEnum()){
for (Object e:type.getEnumConstants()){
if (e.toString().equals(value))
return e;
}
}
throw new ClassCastException();
}
public static String getConfiguration(Object o){
if (IConfigurableObject.class.isInstance(o))
return ((IConfigurableObject)o).getConfiguration();
Field[] fields = getConfigurableAttributes(o);
Hashtable<String, String> values = new Hashtable<String, String>();
for (Field f: fields){
try {
String sv = getValueAsString(f.get(o));
if (sv != null){
values.put(f.getName(), sv);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
StringBuilder sb = new StringBuilder();
Enumeration<String> keys = values.keys();
while (keys.hasMoreElements()){
String name = keys.nextElement();
sb.append(String.format("%s=%s", name, values.get(name)));
if (keys.hasMoreElements())
sb.append(";");
}
return sb.toString();
}
public static void setConfiguration(Object o,String configuration){
StringTokenizer st = new StringTokenizer(configuration, ";");
LinkedList<String> l = new LinkedList<String>();
while (st.hasMoreTokens()){
String t = st.nextToken();
if ((l.peekLast()!=null)&&(l.peekLast().endsWith("\\"))){
String last = l.removeLast();
l.add(last.substring(0, last.length()-1) + ";" + t);
} else {
l.addFirst(t);
}
}
Hashtable<String,String> hash = new Hashtable<String, String>();
for (int n=0;n<l.size();n++){
log(DEBUG,"l: %s",l.get(n));
String c = l.get(n);
String[] split = c.split("\\=",2);
if (split.length==1){
log(DEBUG,"split: (%s)",split[0]);
hash.put(split[0], "");
} else if (split.length==2){
log(DEBUG,"split: (%s:%s)",split[0],split[1]);
hash.put(split[0], split[1]);
}
}
for (Field f: getConfigurableAttributes(o)){
if (hash.containsKey(f.getName())){
try {
f.set(o, getValueFromString(hash.get(f.getName()), f.getType()));
} catch (IllegalArgumentException e) {
log(e);
} catch (IllegalAccessException e) {
log(e);
} catch (ClassCastException e){
log(e);
}
}
}
}
}