1 | package dk.deepthought.sidious.util; |
2 | |
3 | import java.io.File; |
4 | import java.io.FileInputStream; |
5 | import java.io.IOException; |
6 | import java.util.Properties; |
7 | |
8 | import org.apache.commons.logging.Log; |
9 | import org.apache.commons.logging.LogFactory; |
10 | |
11 | import dk.deepthought.sidious.supportsystem.SuperLinkID; |
12 | |
13 | /** |
14 | * This class retrieves the appropriate properties for a given <code>Rule</code>. |
15 | * |
16 | * @author Deepthought |
17 | * |
18 | */ |
19 | public class RuleProperty { |
20 | /** |
21 | * Logger for this class |
22 | */ |
23 | private static final Log logger = LogFactory.getLog(RuleProperty.class); |
24 | |
25 | /** |
26 | * Internal properties file. |
27 | */ |
28 | private final Properties properties; |
29 | |
30 | /** |
31 | * The path to the folder where properties files for the <code>Rules</code> |
32 | * are stored. |
33 | */ |
34 | private static final String RULES_PROPERTIES_PATH = "."; |
35 | |
36 | /** |
37 | * Constructs a new RuleProperty from the input class name. |
38 | * |
39 | * @param className |
40 | * the name of the class |
41 | */ |
42 | public RuleProperty(String className) { |
43 | if (className == null || className.equals("")) { |
44 | logger.error("RuleProperty(String className=" + className |
45 | + ") - Not valid input=null"); |
46 | throw new IllegalArgumentException("null not valid input"); |
47 | } |
48 | properties = readProperties(className); |
49 | } |
50 | |
51 | /** |
52 | * Constructs a new RuleProperty from the input <code>Properties</code>. |
53 | * |
54 | * @param properties |
55 | * the properties |
56 | */ |
57 | public RuleProperty(Properties properties) { |
58 | if (properties == null || properties.isEmpty()) { |
59 | logger.error("RuleProperty(String properties=" + properties |
60 | + ") - Not valid input"); |
61 | throw new IllegalArgumentException("not valid input"); |
62 | } |
63 | this.properties = properties; |
64 | } |
65 | |
66 | /** |
67 | * Method returns the <code>Properties</code> object specified by the |
68 | * input <code>name</code>. |
69 | * |
70 | * @param prefixName |
71 | * prefix of the properties file |
72 | * @return the properties of the input object name |
73 | */ |
74 | private Properties readProperties(String prefixName) { |
75 | if (prefixName == null || prefixName.equals("")) { |
76 | logger.error("getProperties(String prefixName=" + prefixName |
77 | + ") - input not valid"); |
78 | throw new IllegalArgumentException("Not valid input=" + prefixName); |
79 | } |
80 | Properties properties = new Properties(); |
81 | try { |
82 | FileInputStream in = new FileInputStream(RULES_PROPERTIES_PATH |
83 | + File.separator + prefixName + ".properties"); |
84 | properties.load(in); |
85 | in.close(); |
86 | } catch (IOException e) { |
87 | logger.error("getProperties(String prefixName=" + prefixName |
88 | + ") - IOException, properties not read", e); |
89 | if (logger.isDebugEnabled()) { |
90 | logger.debug("Empty properties returned"); |
91 | } |
92 | } |
93 | return properties; |
94 | } |
95 | |
96 | /** |
97 | * Method returns the value of the specified key. |
98 | * |
99 | * @param key |
100 | * the key |
101 | * @param defaultValue |
102 | * the default value, if retrieved value was not a |
103 | * <code>double</code> |
104 | * @return the retrieved value |
105 | */ |
106 | public double getFloat(String key, double defaultValue) { |
107 | if (key == null) { |
108 | logger.error("getFloat(String key=null, double defaultValue=" |
109 | + defaultValue + ") - null changed to empty string"); |
110 | key = ""; |
111 | } |
112 | double f = defaultValue; |
113 | try { |
114 | f = Float.parseFloat(properties.getProperty(key, String |
115 | .valueOf(defaultValue))); |
116 | } catch (NumberFormatException e) { |
117 | logger.error("getFloat(String key=" + key + ", double defaultValue=" |
118 | + defaultValue + ") - Value of key was not a double"); |
119 | } |
120 | return f; |
121 | } |
122 | |
123 | /** |
124 | * Method returns the value of the specified key. |
125 | * <p> |
126 | * NOTE: The <code>RuleProperty.getFloat()</code> is utilized for retrieval. |
127 | * |
128 | * @param key |
129 | * the key |
130 | * @param defaultValue |
131 | * the default value, if retrieved value was not an |
132 | * <code>int</code> |
133 | * @return the retrieved value |
134 | */ |
135 | public int getInt(String key, int defaultValue) { |
136 | if (logger.isDebugEnabled()) { |
137 | logger.debug("getInt(String key=" + key + ", int defaultValue=" |
138 | + defaultValue + ") - start - calling getFloat()"); |
139 | } |
140 | int returnint = (int) getFloat(key, (double) defaultValue); |
141 | if (logger.isDebugEnabled()) { |
142 | logger.debug("getInt(String key=" + key + ", int defaultValue=" |
143 | + defaultValue + ") - end - return value=" + returnint); |
144 | } |
145 | return returnint; |
146 | } |
147 | |
148 | /** |
149 | * Method returns the value of the specified key. |
150 | * |
151 | * @param key |
152 | * the key |
153 | * @return the retrieved value |
154 | */ |
155 | public SuperLinkID getID(String key) { |
156 | if (key == null) { |
157 | logger.error("getID(String key=null) - null changed to empty string"); |
158 | key = ""; |
159 | } |
160 | return new SuperLinkID(properties.getProperty(key)); |
161 | } |
162 | |
163 | } |