KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > mybatchfwk > utils > PropertiesLoader


1 /*
2  * MyBatchFramework - Open-source batch framework.
3  * Copyright (C) 2006 Jérôme Bertèche cyberteche@users.sourceforge.net
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * Jérôme Bertèche
16  * Email: cyberteche@users.sourceforge.net
17  */

18 package net.sf.mybatchfwk.utils;
19
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.lang.reflect.Field JavaDoc;
24 import java.lang.reflect.Modifier JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.text.DateFormat JavaDoc;
27 import java.text.ParseException JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import net.sf.mybatchfwk.BatchService;
34
35 /**
36  * The PropertiesLoader class is used to load a configuration from a file,
37  * and initialize all the fields of an object using the annotations.
38  *
39  * @author Jérôme Bertèche (cyberteche@users.sourceforge.net)
40  */

41 public class PropertiesLoader {
42     
43     private DateFormat JavaDoc dateFormat;
44     private Properties JavaDoc properties;
45     
46     /**
47      * @param properties
48      */

49     public PropertiesLoader(Properties JavaDoc properties) {
50         this.properties = properties;
51     }
52     
53     /**
54      * Load properties stored into a properties configuration file
55      * @return the properties of the configuration file
56      * @throws IOException if the configuration file cannot be find into the classpath or if it cannot be parsed (not a properties file)
57      */

58     public static Properties JavaDoc getProperties(String JavaDoc fileName) throws IOException JavaDoc {
59         
60         InputStream JavaDoc inputStream = null;
61         URL JavaDoc 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 JavaDoc("unable to find the configuration file '" + fileName + "'");
73         }
74         
75         Properties JavaDoc properties = new Properties JavaDoc();
76         properties.load(inputStream);
77
78         return properties;
79     }
80     
81     /**
82      * Return the fields that owns an annotation of type Property
83      * @param clazz
84      * @return
85      */

86     public static Field JavaDoc[] getPropertyFields(Class JavaDoc clazz) {
87         List JavaDoc<Field JavaDoc> l = new LinkedList JavaDoc<Field JavaDoc>();
88         
89         Field JavaDoc[] fields = clazz.getDeclaredFields();
90         for (int i = 0; i < fields.length; i++) {
91             Field JavaDoc field = fields[i];
92             if (field.isAnnotationPresent(Property.class)) {
93                 l.add(field);
94             }
95         }
96         
97         return l.toArray(new Field JavaDoc[l.size()]);
98     }
99     
100     /**
101      * Assign the configuration properties to an object using the annotations.<br>
102      * For each field that own an annotation of type Property, we retrieve the key of this annotation.
103      * Next we retrieve the value into the configuration that corresponds to this key.
104      * Finally, this value is assigned to the field of the object.
105      *
106      * @param object
107      * @throws IllegalArgumentException
108      * @throws IllegalAccessException
109      * @throws ParseException
110      */

111     public void assignProperties(Object JavaDoc object) throws IllegalArgumentException JavaDoc, IllegalAccessException JavaDoc, ParseException JavaDoc {
112         
113         for (Field JavaDoc 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 JavaDoc type = field.getType();
124                 if (Boolean JavaDoc.class.equals(type) || boolean.class.equals(type)) {
125                     field.setBoolean(object, getBooleanProperty(property.value()));
126                 } else if (Integer JavaDoc.class.equals(type) || int.class.equals(type)) {
127                     field.setInt(object, getIntegerProperty(property.value()));
128                 } else if (Short JavaDoc.class.equals(type) || short.class.equals(type)) {
129                     field.setShort(object, getShortProperty(property.value()));
130                 } else if (Long JavaDoc.class.equals(type) || long.class.equals(type)) {
131                     field.setLong(object, getLongProperty(property.value()));
132                 } else if (Date JavaDoc.class.equals(type)) {
133                     field.set(object, getDateProperty(property.value()));
134                 } else if (String JavaDoc.class.equals(type)) {
135                     field.set(object, properties.get(property.value()));
136                 }
137             }
138         }
139     }
140     
141     /**
142      * Retrieve a boolean from a string property
143      * @param key
144      * @return
145      */

146     public boolean getBooleanProperty(String JavaDoc key) {
147         String JavaDoc param = properties.getProperty(key);
148         return Boolean.valueOf(param);
149     }
150     
151     /**
152      * Retrieve an integer from a string property
153      * @param key
154      * @return
155      */

156     public int getIntegerProperty(String JavaDoc key) {
157         String JavaDoc param = properties.getProperty(key);
158         return Integer.parseInt(param);
159     }
160     
161     /**
162      * Retrieve a short from a string property
163      * @param key
164      * @return
165      */

166     public short getShortProperty(String JavaDoc key) {
167         String JavaDoc param = properties.getProperty(key);
168         return Short.parseShort(param);
169     }
170     
171     /**
172      * Retrieve a long from a string property
173      * @param key
174      * @return
175      */

176     public long getLongProperty(String JavaDoc key) {
177         String JavaDoc param = properties.getProperty(key);
178         return Long.parseLong(param);
179     }
180     
181     /**
182      * Retrieve a date from a string property
183      * @param key
184      * @return
185      */

186     public Date JavaDoc getDateProperty(String JavaDoc key) throws ParseException JavaDoc {
187         String JavaDoc param = properties.getProperty(key);
188         return dateFormat.parse(param);
189     }
190     
191     /**
192      * Return the property designed by the given key
193      * @param key
194      * @return
195      */

196     public String JavaDoc getProperty(String JavaDoc key) {
197         return properties.getProperty(key);
198     }
199     
200     /**
201      * Add the given property
202      * @param key
203      * @param value
204      */

205     public void addProperty(String JavaDoc key, String JavaDoc value) {
206         properties.put(key, value);
207     }
208     
209     // ######################################################
210
// ################ GETTERS and SETTERS #################
211
// ######################################################
212

213     /**
214      * @return the dateFormat
215      */

216     public DateFormat JavaDoc getDateFormat() {
217         return dateFormat;
218     }
219
220     /**
221      * @param dateFormat the dateFormat to set
222      */

223     public void setDateFormat(DateFormat JavaDoc dateFormat) {
224         this.dateFormat = dateFormat;
225     }
226
227     /**
228      * @return the properties
229      */

230     public Properties JavaDoc getProperties() {
231         return properties;
232     }
233
234     /**
235      * @param properties the properties to set
236      */

237     public void setProperties(Properties JavaDoc properties) {
238         this.properties = properties;
239     }
240 }
241
Popular Tags