Skip to content

Method: getProperty(String)

1: package de.fhdw.wtf.persistence.utils;
2:
3: import java.util.HashMap;
4: import java.util.MissingResourceException;
5:
6: /**
7: * This class is a singleton that is used to create a PropertiesReader class that stores the connection properties to
8: * the oracle database.
9: *
10: * @author HFW413hy
11: *
12: */
13: public class PropertiesReaderFactory {
14:         
15:         private static PropertiesReaderFactory _instance = null;
16:         
17:         /**
18:          * Returns the instance of this singleton.
19:          *
20:          * @return The instance of this singleton.
21:          */
22:         public static synchronized PropertiesReaderFactory getInstance() {
23:                 if (_instance == null) {
24:                         _instance = new PropertiesReaderFactory();
25:                 }
26:                 return _instance;
27:         }
28:         
29:         /**
30:          * Creates a PropertiesReaderFile-Object that will be returned.
31:          *
32:          * @param file
33:          * @return The PropertiesReaderFile-Object
34:          */
35:         public PropertiesReader getPropertiesReaderFile(final String file) {
36:                 final PropertiesReader reader = new PropertiesReaderFile();
37:                 try {
38:                         reader.initialize(file);
39:                         return reader;
40:                 } catch (final MissingResourceException e) {
41:                         return this.getPropertiesReaderOracle();
42:                 }
43:         }
44:         
45:         /**
46:          * Returns a new PropertiesReader that stores the Oracle connection properties. At first, it searches for a file
47:          * "oracle.properties" in the current directory. If there is no file that could be read, it tries to read the
48:          * "oracle.properties" from the current namespace. If there is not file, it will return a PropertiesReader that
49:          * contains default information.
50:          *
51:          * @return The PropertiesReader that contains connection properties to the oracle database.
52:          */
53:         public PropertiesReader getPropertiesReaderOracle() {
54:                 PropertiesReader reader;
55:                 
56:                 // Try to read the local file from the current directory
57:                 // --------------------------------------------------
58:                 reader = new PropertiesReaderFile();
59:                 try {
60:                         reader.initialize("oracle.properties");
61:                         return reader;
62:                 } catch (final MissingResourceException e) {
63:                         // Continue if this happens... file is not here
64:                 }
65:                 
66:                 // Try to read the ressource file from the current package
67:                 // --------------------------------------------------
68:                 reader = new PropertiesReaderRessource();
69:                 try {
70:                         reader.initialize("oracle");
71:                         return reader;
72:                 } catch (final MissingResourceException e) {
73:                         // Continue if this happens... file is not here
74:                 }
75:                 
76:                 // Now, the default reader with default params
77:                 // --------------------------------------------------
78:                 reader = new PropertiesReader() {
79:                         private final HashMap<String, String> propValues = new HashMap<>();
80:                         
81:                         @Override
82:                         public void initialize(final String propFile) throws MissingResourceException {
83:                                 this.propValues.put("sid", "XE");
84:                                 this.propValues.put("schema", "meta");
85:                                 this.propValues.put("username", "meta");
86:                                 this.propValues.put("password", "meta");
87:                                 this.propValues.put("hostname", "localhost");
88:                                 this.propValues.put("port", "1521");
89:                         }
90:                         
91:                         @Override
92:                         public String getProperty(final String key) {
93:•                                if (!this.propValues.containsKey(key)) {
94:                                         return "";
95:                                 }
96:                                 return this.propValues.get(key);
97:                         }
98:                 };
99:                 reader.initialize("");
100:                 return reader;
101:         }
102: }