01: package org.claros.commons.configuration;
02:
03: import java.io.File;
04:
05: import org.apache.commons.configuration.Configuration;
06: import org.apache.commons.configuration.ConfigurationException;
07: import org.apache.commons.configuration.PropertiesConfiguration;
08: import org.apache.commons.configuration.XMLConfiguration;
09: import org.apache.commons.logging.Log;
10: import org.apache.commons.logging.LogFactory;
11: import org.claros.commons.exception.SystemException;
12: import org.claros.commons.utility.Utility;
13:
14: /**
15: *
16: * @author Umut Gökbayrak
17: *
18: */
19: public class PropertyFile {
20: private static Log log = LogFactory.getLog(Utility.class);
21:
22: /**
23: * Reads a xml file or properties file and returns it.
24: * You must define the fileName relative to the WEB-INF directory.
25: * For example /config/config.xml means /WEB-INF/config/config.xml
26: * from the web source directory.
27: * @param fileName
28: * @return Configuration
29: * @throws ConfigurationException
30: */
31: public static Configuration getConfiguration(String fileName)
32: throws ConfigurationException, SystemException {
33: Configuration config = null;
34: try {
35: if (fileName != null) {
36: File f = new File(Paths.getPrefix() + "/WEB-INF"
37: + fileName);
38:
39: if (fileName.endsWith(".xml")) {
40: config = new XMLConfiguration(f);
41: } else {
42: config = new PropertiesConfiguration(f);
43: }
44: }
45: } catch (ConfigurationException e) {
46: log
47: .error(
48: "Property file "
49: + fileName
50: + " could not be loaded. Configuration problem.",
51: e);
52: throw e;
53: } catch (Exception e) {
54: log.error("Property file " + fileName
55: + " could not be loaded. System problem.", e);
56: throw new SystemException(e,
57: "configuration.property.generic");
58: }
59: return config;
60: }
61: }
|