Skip to content

Method: {...}

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:                 reader.initialize(file);
38:                 return reader;
39:         }
40:         
41:         /**
42:          * Returns a new PropertiesReader that stores the Oracle connection properties. At first, it searches for a file
43:          * "oracle.properties" in the current directory. If there is no file that could be read, it tries to read the
44:          * "oracle.properties" from the current namespace. If there is not file, it will return a PropertiesReader that
45:          * contains default information.
46:          *
47:          * @return The PropertiesReader that contains connection properties to the oracle database.
48:          */
49:         public PropertiesReader getPropertiesReaderOracle() {
50:                 PropertiesReader reader;
51:                 
52:                 // Try to read the local file from the current directory
53:                 // --------------------------------------------------
54:                 reader = new PropertiesReaderFile();
55:                 try {
56:                         reader.initialize("oracle.properties");
57:                         return reader;
58:                 } catch (final MissingResourceException e) {
59:                         // Continue if this happens... file is not here
60:                 }
61:                 
62:                 // Try to read the ressource file from the current package
63:                 // --------------------------------------------------
64:                 reader = new PropertiesReaderRessource();
65:                 try {
66:                         reader.initialize("oracle");
67:                         return reader;
68:                 } catch (final MissingResourceException e) {
69:                         // Continue if this happens... file is not here
70:                 }
71:                 
72:                 // Now, the default reader with default params
73:                 // --------------------------------------------------
74:                 reader = new PropertiesReader() {
75:                         private final HashMap<String, String> propValues = new HashMap<>();
76:                         
77:                         @Override
78:                         public void initialize(final String propFile) throws MissingResourceException {
79:                                 this.propValues.put("sid", "XE");
80:                                 this.propValues.put("schema", "meta");
81:                                 this.propValues.put("username", "meta");
82:                                 this.propValues.put("password", "meta");
83:                                 this.propValues.put("hostname", "localhost");
84:                                 this.propValues.put("port", "1521");
85:                         }
86:                         
87:                         @Override
88:                         public String getProperty(final String key) {
89:                                 if (!this.propValues.containsKey(key)) {
90:                                         return "";
91:                                 }
92:                                 return this.propValues.get(key);
93:                         }
94:                 };
95:                 reader.initialize("");
96:                 return reader;
97:         }
98: }