1 18 package net.sf.mybatchfwk.utils; 19 20 import java.io.FileNotFoundException ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.lang.reflect.Field ; 24 import java.lang.reflect.Modifier ; 25 import java.net.URL ; 26 import java.text.DateFormat ; 27 import java.text.ParseException ; 28 import java.util.Date ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Properties ; 32 33 import net.sf.mybatchfwk.BatchService; 34 35 41 public class PropertiesLoader { 42 43 private DateFormat dateFormat; 44 private Properties properties; 45 46 49 public PropertiesLoader(Properties properties) { 50 this.properties = properties; 51 } 52 53 58 public static Properties getProperties(String fileName) throws IOException { 59 60 InputStream inputStream = null; 61 URL urlFile = Thread.currentThread().getContextClassLoader().getResource(fileName); 62 if (urlFile != null) { 63 inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); 64 } else { 65 urlFile = BatchService.class.getClassLoader().getResource(fileName); 66 if (urlFile != null) { 67 inputStream = BatchService.class.getClassLoader().getResourceAsStream(fileName); 68 } 69 } 70 71 if (urlFile == null) { 72 throw new FileNotFoundException ("unable to find the configuration file '" + fileName + "'"); 73 } 74 75 Properties properties = new Properties (); 76 properties.load(inputStream); 77 78 return properties; 79 } 80 81 86 public static Field [] getPropertyFields(Class clazz) { 87 List <Field > l = new LinkedList <Field >(); 88 89 Field [] fields = clazz.getDeclaredFields(); 90 for (int i = 0; i < fields.length; i++) { 91 Field field = fields[i]; 92 if (field.isAnnotationPresent(Property.class)) { 93 l.add(field); 94 } 95 } 96 97 return l.toArray(new Field [l.size()]); 98 } 99 100 111 public void assignProperties(Object object) throws IllegalArgumentException , IllegalAccessException , ParseException { 112 113 for (Field field : getPropertyFields(object.getClass())) { 114 Property property = field.getAnnotation(Property.class); 115 116 if ((properties.get(property.value()) != null) && 117 (!(Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers())))) { 118 119 if (!field.isAccessible()) { 120 field.setAccessible(true); 121 } 122 123 Class type = field.getType(); 124 if (Boolean .class.equals(type) || boolean.class.equals(type)) { 125 field.setBoolean(object, getBooleanProperty(property.value())); 126 } else if (Integer .class.equals(type) || int.class.equals(type)) { 127 field.setInt(object, getIntegerProperty(property.value())); 128 } else if (Short .class.equals(type) || short.class.equals(type)) { 129 field.setShort(object, getShortProperty(property.value())); 130 } else if (Long .class.equals(type) || long.class.equals(type)) { 131 field.setLong(object, getLongProperty(property.value())); 132 } else if (Date .class.equals(type)) { 133 field.set(object, getDateProperty(property.value())); 134 } else if (String .class.equals(type)) { 135 field.set(object, properties.get(property.value())); 136 } 137 } 138 } 139 } 140 141 146 public boolean getBooleanProperty(String key) { 147 String param = properties.getProperty(key); 148 return Boolean.valueOf(param); 149 } 150 151 156 public int getIntegerProperty(String key) { 157 String param = properties.getProperty(key); 158 return Integer.parseInt(param); 159 } 160 161 166 public short getShortProperty(String key) { 167 String param = properties.getProperty(key); 168 return Short.parseShort(param); 169 } 170 171 176 public long getLongProperty(String key) { 177 String param = properties.getProperty(key); 178 return Long.parseLong(param); 179 } 180 181 186 public Date getDateProperty(String key) throws ParseException { 187 String param = properties.getProperty(key); 188 return dateFormat.parse(param); 189 } 190 191 196 public String getProperty(String key) { 197 return properties.getProperty(key); 198 } 199 200 205 public void addProperty(String key, String value) { 206 properties.put(key, value); 207 } 208 209 213 216 public DateFormat getDateFormat() { 217 return dateFormat; 218 } 219 220 223 public void setDateFormat(DateFormat dateFormat) { 224 this.dateFormat = dateFormat; 225 } 226 227 230 public Properties getProperties() { 231 return properties; 232 } 233 234 237 public void setProperties(Properties properties) { 238 this.properties = properties; 239 } 240 } 241 | Popular Tags |