forked from LupusNobilis/java-org.hwo
org.hwo.configuration neu
parent
830fb39115
commit
42db0d636f
|
@ -0,0 +1,8 @@
|
|||
package org.hwo.configuration;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ConfigurableAttribute {
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package org.hwo.configuration;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ConfigurableObject {
|
||||
|
||||
}
|
|
@ -0,0 +1,151 @@
|
|||
package org.hwo.configuration;
|
||||
|
||||
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;
|
||||
|
||||
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();
|
||||
|
||||
s = s.replaceAll("\\\\","\\\\\\\\");
|
||||
s = s.replaceAll("\\;", "\\\\;");
|
||||
s = s.replaceAll("\\|", "\\\\|");
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
public static Object getValueFromString(String value,Class<?> type) throws ClassCastException{
|
||||
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++){
|
||||
String c = l.get(n).replaceAll("\\;", ";").replaceAll("\\|", "|").replaceAll("\\\\", "\\");
|
||||
String[] split = c.split("\\=",2);
|
||||
if (split.length==1){
|
||||
hash.put(split[0], "");
|
||||
} else if (split.length==2){
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package org.hwo.configuration;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ConfigurationTrigger {
|
||||
public boolean postConfiguration() default true;
|
||||
public boolean preConfiguration() default false;
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.hwo.configuration;
|
||||
|
||||
public interface IConfigurableObject {
|
||||
|
||||
public String getConfiguration();
|
||||
public void setConfiguration(String configurationText);
|
||||
|
||||
}
|
Loading…
Reference in New Issue