1 10 package org.jgap.data.config; 11 12 import java.util.*; 13 import org.jgap.*; 14 import java.lang.reflect.*; 15 16 25 public class RootConfigurationHandler 26 implements ConfigurationHandler { 27 28 private final static String CVS_REVISION = "$Revision: 1.11 $"; 29 30 private final static String CONFIG_NAMESPACE = "org.jgap.Configuration"; 32 33 private final static String GENETIC_OPS = "GeneticOperators"; 36 37 private final static String NATURAL_SELS = "NaturalSelectors"; 38 39 private Configurable m_configurable; 40 41 47 public String getName() { 48 return "Configuration"; 49 } 50 51 56 public List getConfigProperties() { 57 return null; 58 } 59 60 68 public String getNS() { 69 return CONFIG_NAMESPACE; 70 } 71 72 82 public void readConfig() 83 throws ConfigException, InvalidConfigurationException { 84 ConfigFileReader.instance().setNS(CONFIG_NAMESPACE); 88 String value = ConfigFileReader.instance().getValue("m_populationSize"); 89 try { 90 if (value != null) { 91 setConfigProperty(m_configurable, "m_populationSize", value); 92 } 93 } catch (IllegalAccessException ex) { 94 ex.printStackTrace(); 95 throw new InvalidConfigurationException(ex.getMessage()); 96 } 97 configureClass(GENETIC_OPS); 99 configureClass(NATURAL_SELS); 101 } 102 103 111 public void setConfigurable(Configurable a_configurable) { 112 m_configurable = a_configurable; 113 } 114 115 128 public void setConfigProperty(Object a_configurable, String a_propertyName, 129 String a_value) 130 throws IllegalAccessException { 131 String configVarName = "m_config"; Field configVar = getPrivateField(a_configurable, configVarName); 143 configVar.setAccessible(true); 144 Object configObj = configVar.get(a_configurable); 145 Field propertyVar = getPrivateField(configObj, a_propertyName); 146 propertyVar.setAccessible(true); 147 Class type = propertyVar.getType(); 148 if (type.equals(boolean.class)) { 149 propertyVar.setBoolean(configObj, Boolean.valueOf(a_value).booleanValue()); 150 } 151 else if (type.equals(byte.class)) { 152 propertyVar.setByte(configObj, Byte.valueOf(a_value).byteValue()); 153 } 154 else if (type.equals(char.class)) { 155 propertyVar.setChar(configObj, a_value.charAt(0)); 156 } 157 else if (type.equals(double.class)) { 158 propertyVar.setDouble(configObj, Double.valueOf(a_value).doubleValue()); 159 } 160 else if (type.equals(float.class)) { 161 propertyVar.setFloat(configObj, Float.valueOf(a_value).floatValue()); 162 } 163 else if (type.equals(int.class)) { 164 propertyVar.setInt(configObj, Integer.valueOf(a_value).intValue()); 165 } 166 else if (type.equals(long.class)) { 167 propertyVar.setLong(configObj, Long.valueOf(a_value).longValue()); 168 } 169 else if (type.equals(short.class)) { 170 propertyVar.setShort(configObj, Short.valueOf(a_value).shortValue()); 171 } 172 else if (type.equals(String .class)) { 173 propertyVar.set(configObj, a_value); 174 } 175 else { 176 throw new RuntimeException ("Unknown field type: " + type.getName()); 177 } 178 } 179 180 189 public static Field getPrivateField(Object a_instance, String a_fieldName) { 190 final Field fields[] = a_instance.getClass().getDeclaredFields(); 191 for (int i = 0; i < fields.length; ++i) { 192 if (a_fieldName.equals(fields[i].getName())) { 193 fields[i].setAccessible(true); 194 return fields[i]; 195 } 196 } 197 return null; 198 } 199 200 209 public static void configureClass(String className) 210 throws ConfigException { 211 List values = ConfigFileReader.instance().getValues(className); 212 if (values != null && values.size() > 0) { 213 String cName = ""; 214 for (Iterator iter = values.iterator(); iter.hasNext(); ) { 217 try { 218 cName = (String ) iter.next(); 219 Class genClass = Class.forName(cName); 220 Configurable conObj = (Configurable) genClass.newInstance(); 221 } catch (Exception ex) { 224 throw new ConfigException("Error while configuring " + className + 225 "." + cName); 226 } 227 } 228 } 229 } 230 } 231 | Popular Tags |