View Javadoc
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   * Read properties from resources.
17   * 
18   * @author p.baniukiewicz
19   *
20   */
21  public class PropertyReader {
22  
23    /**
24     * The Constant LOGGER.
25     */
26    static final Logger LOGGER = LoggerFactory.getLogger(PropertyReader.class.getName());
27  
28    /**
29     * Default constructor.
30     */
31    public PropertyReader() {
32    }
33  
34    /**
35     * Read property from property file for QuimP package.
36     * 
37     * <p>not static because of getClass().Property file should be in the same package as this class
38     * or
39     * full path should be provided otherwise.
40     * 
41     * @param propFileName property file name
42     * @param propKey name of the key
43     * @return value for propKey
44     * @see <a href=
45     *      "link">http://stackoverflow.com/questions/333363/loading-a-properties-file-from-java-package</a>
46     * @see #readProperty(Class, String, String, String)
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     * Read property from jar file.
80     * 
81     * <p>Used when there is many the same properties in many jars.
82     * 
83     * @param c class object associated with properties (being in jar)
84     * @param partofFilename A part of expected jar name where property is located.
85     * @param propFileName file name in jar with path
86     * @param propKey key
87     * @return Value of property for <tt>propKey</tt>
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 }