KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > data > config > RootConfigurationHandler


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.data.config;
11
12 import java.util.*;
13 import org.jgap.*;
14 import java.lang.reflect.*;
15
16 /**
17  * The ConfigurationHandler for the Configuration class itself. This is the
18  * entry point for a Configuration.
19  * In other words this is for dynamically building up a Configuration.
20  *
21  * @author Siddhartha Azad
22  * @author Klaus Meffert
23  * @since 2.3
24  * */

25 public class RootConfigurationHandler
26     implements ConfigurationHandler {
27   /** String containing the CVS revision. Read out via reflection!*/
28   private final static String JavaDoc CVS_REVISION = "$Revision: 1.11 $";
29
30   // Namespace
31
private final static String JavaDoc CONFIG_NAMESPACE = "org.jgap.Configuration";
32
33   // constants to indicate various properties
34
// ----------------------------------------
35
private final static String JavaDoc GENETIC_OPS = "GeneticOperators";
36
37   private final static String JavaDoc NATURAL_SELS = "NaturalSelectors";
38
39   private Configurable m_configurable;
40
41   /**
42    * @return Name of this Configuration Object (name of what you are
43    * configuring) to be used in the properties file.
44    *
45    * @since 2.3
46    * */

47   public String JavaDoc getName() {
48     return "Configuration";
49   }
50
51   /**
52    * Return the information to generate the GUI for configuring this class.
53    * @return a list of ConfigProperty objects
54    * @since 2.3
55    * */

56   public List getConfigProperties() {
57     return null;
58   }
59
60   /**
61    * Get the namespace to be used in the config file for the Configurable
62    * this ConfigurationHandler belongs to.
63    * @return the namespace of the Configurable
64    *
65    * @author Siddhartha Azad
66    * @since 2.3
67    * */

68   public String JavaDoc getNS() {
69     return CONFIG_NAMESPACE;
70   }
71
72   /**
73    * Method that will populate an Configurable with the properties in the
74    * config file.
75    *
76    * @throws ConfigException
77    * @throws InvalidConfigurationException
78    *
79    * @author Siddhartha Azad
80    * @since 2.3
81    * */

82   public void readConfig()
83       throws ConfigException, InvalidConfigurationException {
84     // TODO adapt to new concept of configuration via reflection and marker
85
// interface
86
// set the namespace to get the properties from
87
ConfigFileReader.instance().setNS(CONFIG_NAMESPACE);
88     String JavaDoc value = ConfigFileReader.instance().getValue("m_populationSize");
89     try {
90       if (value != null) {
91         setConfigProperty(m_configurable, "m_populationSize", value);
92       }
93     } catch (IllegalAccessException JavaDoc ex) {
94       ex.printStackTrace();
95       throw new InvalidConfigurationException(ex.getMessage());
96     }
97     // go through all genetic operators and configure them
98
configureClass(GENETIC_OPS);
99     // go through all natural selectors and configure them
100
configureClass(NATURAL_SELS);
101   }
102
103   /**
104    * Set the Configurable to which this ConfigurationHandler belongs.
105    * @param a_configurable the Configurable to which this ConfigurationHandler
106    * belongs
107    *
108    * @author Siddhartha Azad
109    * @since 2.3
110    * */

111   public void setConfigurable(Configurable a_configurable) {
112     m_configurable = a_configurable;
113   }
114
115   /**
116    * Sets the property of a configurable to a given value. Uses reflection to
117    * do so. Queries the method getConfigVarName for the name of the field
118    * hosting the configurable properties.
119    *
120    * @param a_configurable the configurable to use
121    * @param a_propertyName the property to set
122    * @param a_value the value to assign to the property
123    * @throws IllegalAccessException
124    *
125    * @author Klaus Meffert
126    * @since 2.6
127    */

128   public void setConfigProperty(Object JavaDoc a_configurable, String JavaDoc a_propertyName,
129                                 String JavaDoc a_value)
130       throws IllegalAccessException JavaDoc {
131 // Use following in case variable name "m_config" should be determined
132
// dynamically.
133
// Method m = null;
134
// try {
135
// m = a_configurable.getClass().getDeclaredMethod("getConfigVarName",
136
// new Class[0]);
137
// } catch (NoSuchMethodException nex) {
138
// // nothing to set here
139
// return;
140
// }
141
String JavaDoc configVarName = "m_config"; //(String)m.invoke(a_configurable, new Object[0]);
142
Field configVar = getPrivateField(a_configurable, configVarName);
143     configVar.setAccessible(true);
144     Object JavaDoc configObj = configVar.get(a_configurable);
145     Field propertyVar = getPrivateField(configObj, a_propertyName);
146     propertyVar.setAccessible(true);
147     Class JavaDoc 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 JavaDoc.class)) {
173       propertyVar.set(configObj, a_value);
174     }
175     else {
176       throw new RuntimeException JavaDoc("Unknown field type: " + type.getName());
177     }
178   }
179
180   /**
181    * Helper method: Read a private field.
182    * @param a_instance the instance the field is contained with
183    * @param a_fieldName the name of the field to read
184    * @return the Field object or null, if none found
185    *
186    * @author Klaus Meffert
187    * @since 2.6
188    */

189   public static Field getPrivateField(Object JavaDoc a_instance, String JavaDoc 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   /**
201    * Retrieve all instances of a certain property from the config file reader
202    * and configure each of these.
203    * @param className the name of the property to configure
204    * @throws ConfigException
205    *
206    * @author Siddhartha Azad
207    * @since 2.4
208    * */

209   public static void configureClass(String JavaDoc className)
210       throws ConfigException {
211     List values = ConfigFileReader.instance().getValues(className);
212     if (values != null && values.size() > 0) {
213       String JavaDoc cName = "";
214       // iterate through all instances of this property and create Configurables
215
// for them, then configure these
216
for (Iterator iter = values.iterator(); iter.hasNext(); ) {
217         try {
218           cName = (String JavaDoc) iter.next();
219           Class JavaDoc genClass = Class.forName(cName);
220           Configurable conObj = (Configurable) genClass.newInstance();
221 //TODO ConfigurationHandler cHandler = conObj.getConfigurationHandler();
222
//TODO cHandler.readConfig();
223
} catch (Exception JavaDoc ex) {
224           throw new ConfigException("Error while configuring " + className +
225                                     "." + cName);
226         }
227       }
228     }
229   }
230 }
231
Popular Tags