1 19 20 package za.org.coefficient.core; 21 22 import org.apache.commons.beanutils.ConvertUtils; 23 24 import za.org.coefficient.util.common.InvokerFactory; 25 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.List ; 29 30 35 public abstract class BaseConfigurationBackedConstants { 36 38 41 public static void init(String propertiesFileName, String className) { 42 try { 43 CoefficientConfiguration cc = 44 (CoefficientConfiguration)InvokerFactory.getInvoker() 45 .invokeMethodOnModule("Configuration", "getConfiguration", 46 new Object []{className}); 47 48 java.util.Properties props = new java.util.Properties (); 50 java.io.InputStream propsStream = null; 51 52 if(propertiesFileName != null) { 53 try { 54 propsStream = 56 BaseConfigurationBackedConstants 57 .class.getResourceAsStream("/WEB-INF/" 58 + propertiesFileName); 59 if (null == propsStream) { 61 propsStream = 62 BaseConfigurationBackedConstants.class 63 .getResourceAsStream("/" + propertiesFileName); 64 if (null == propsStream) { 65 propsStream = 66 BaseConfigurationBackedConstants.class 67 .getResourceAsStream(propertiesFileName); 68 } 69 } 70 props.load(propsStream); 71 } catch (Throwable t) { 72 } 73 } 74 75 java.lang.reflect.Field [] fields = Class.forName(className) 77 .getFields(); 78 for (int i = 0; i < fields.length; i++) { 79 String name = fields[i].getName(); 80 String value = props.getProperty(name); 81 if (value == null) { 82 value = cc.getProperty(name); 83 } 84 if (value != null) { 85 value = value.trim(); 86 Object oVal = ConvertUtils.convert(value, fields[i].getType()); 87 fields[i].set(null, oVal); 88 cc.addProperty(name, oVal.toString()); 89 } else { 90 cc.addProperty(name, fields[i].get(null).toString()); 91 } 92 } 93 94 InvokerFactory.getInvoker() 96 .invokeMethodOnModule("Configuration", "saveConfiguration", 97 new Object [] {cc}); 98 99 } catch (Throwable t) { 100 t.printStackTrace(); 101 System.err.println("## BaseConfigurationBackedConstants init error: " 102 + t); 103 } 104 } 105 106 public static void initFromConfig(CoefficientConfiguration cc, 107 String className) { 108 try { 109 BaseConfigurationBackedConstants conf = 110 (BaseConfigurationBackedConstants)Class.forName(className) 111 .newInstance(); 112 java.lang.reflect.Field [] fields = Class.forName(className).getFields(); 114 for (int i = 0; i < fields.length; i++) { 116 String name = fields[i].getName(); 117 String value = cc.getProperty(name); 118 if (value != null) { 119 value = value.trim(); 120 Object oVal = ConvertUtils.convert(value, fields[i].getType()); 121 fields[i].set(null, oVal); 122 } 123 } 124 BaseConfigurationBackedConstants.hasRequiredFields(); 125 } catch (Throwable t) { 126 System.err.println("## BaseConfigurationBackedConstants config init error: " + t); 127 } 128 } 129 130 public static boolean hasRequiredFields() { 131 return true; 132 } 133 134 public static List getPropertyFields(String className) { 135 try { 136 return Arrays.asList(Class.forName(className).getFields()); 137 } catch (Exception ex) { 138 ex.printStackTrace(); 139 return new ArrayList (); 140 } 141 } 142 } 143 | Popular Tags |