DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT <http://nagoya.apache.org/bugzilla/show_bug.cgi?id=24272>. ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND INSERTED IN THE BUG DATABASE.
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=24272 Enum/ValueEnum class needs functionality to load values from a properties file Summary: Enum/ValueEnum class needs functionality to load values from a properties file Product: Commons Version: 2.0 Final Platform: All OS/Version: Windows XP Status: NEW Severity: Enhancement Priority: Other Component: Lang AssignedTo: [EMAIL PROTECTED] ReportedBy: [EMAIL PROTECTED] CC: [EMAIL PROTECTED] It would be good to have an implementation of Enum that can load the possible values from a properties file, instead of having to change the code everytime a new value is added. This properties file could be named the same as the Enum class being implemented, e.g., if MyEnum.java extends Enum or ValuedEnum, it should be able to load the values from MyEnum.properties file located in the same directory as the class itself and be packaged in the jar along with the class. I took a crack at solving the problem, as follows: import java.io.Serializable; import java.io.InputStream; import java.io.IOException; import java.util.Properties; import java.util.Iterator; import java.util.Map; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import org.apache.commons.lang.enum.*; public abstract class ConfigEnum extends ValuedEnum { protected static void loadEnumConfig(Class enumClass) throws IOException, InstantiationException, IllegalAccessException, Throwable { Properties p = new Properties(); String propFilename = enumClass.getName().replace('.', '/') + ".properties"; InputStream is = null; ClassLoader loader = enumClass.getClassLoader(); is = ((loader != null) ? loader.getResourceAsStream(propFilename) : ClassLoader.getSystemResourceAsStream(propFilename)); if (is == null) { throw new RuntimeException("No such resource"); } p.load(is); is.close(); for(Iterator pIter = p.entrySet().iterator(); pIter.hasNext(); ) { // Read the entries from the properties file and create Objects of that type Map.Entry entry = (Map.Entry) pIter.next(); int enumValue = Integer.parseInt((String) entry.getKey()); String enumStr = (String) entry.getValue(); Class[] constructorArgTypes = new Class[] { String.class, int.class }; try { Constructor constructor = enumClass.getConstructor (constructorArgTypes); Object[] constructorArgs = new Object [] { enumStr, new Integer (enumValue) }; constructor.newInstance(constructorArgs); } catch (NoSuchMethodException nsme) { System.err.println("Caught Exception in loadEnumConfig: " + nsme); } catch (InvocationTargetException ite) { throw ite.getTargetException(); } } } protected ConfigEnum(String name, int value) { super(name, value); } } The only problem with this implementation is that it requires the MyEnum.java class to have a public constructor with MyEnum(String, int) signature. This defeats the very purpose of protecting creation of new values. Another solution is to make the ConfigEnum class non-abstract, but it doesn't seem elegant or fool-proof. Any suggestions/ideas. Thanks. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
