1 package com.github.celldynamics.quimp;
2
3 import java.io.BufferedReader;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.InputStreamReader;
8 import java.net.URL;
9 import java.util.Enumeration;
10 import java.util.Properties;
11
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15
16
17
18
19
20
21 public class PropertyReader {
22
23
24
25
26 static final Logger LOGGER = LoggerFactory.getLogger(PropertyReader.class.getName());
27
28
29
30
31 public PropertyReader() {
32 }
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public String readProperty(String propFileName, String propKey) {
49 InputStream inputStream;
50 String result = "";
51 Properties prop;
52
53 prop = new Properties();
54 inputStream = getClass().getResourceAsStream(propFileName);
55 try {
56 if (inputStream != null) {
57 prop.load(inputStream);
58 } else {
59 throw new FileNotFoundException(
60 "property file '" + propFileName + "' not found in the classpath");
61 }
62 result = prop.getProperty(propKey);
63 } catch (Exception e) {
64 LOGGER.error(e.getMessage(), e);
65 } finally {
66 try {
67 if (inputStream != null) {
68 inputStream.close();
69 }
70 } catch (IOException e) {
71 LOGGER.error(e.getMessage(), e);
72 }
73 }
74
75 return result;
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89 public static String readProperty(Class<?> c, String partofFilename, String propFileName,
90 String propKey) {
91 String result = "";
92 Properties prop;
93 BufferedReader in = null;
94 prop = new Properties();
95
96 try {
97 Enumeration<URL> resources = c.getClassLoader().getResources(propFileName);
98 while (resources.hasMoreElements()) {
99 URL reselement = resources.nextElement();
100 LOGGER.trace("res " + reselement.toString() + " class " + c.getSimpleName());
101 if (reselement.toString().contains(partofFilename)) {
102 in = new BufferedReader(new InputStreamReader(reselement.openStream()));
103 }
104 if (in != null) {
105 prop.load(in);
106 result = prop.getProperty(propKey);
107 LOGGER.trace("result " + result);
108 }
109 }
110
111 } catch (Exception e) {
112 LOGGER.error(e.getMessage(), e);
113 } finally {
114 try {
115 if (in != null) {
116 in.close();
117 }
118 } catch (IOException e) {
119 LOGGER.error(e.getMessage(), e);
120 }
121 }
122
123 return result;
124 }
125
126 }