-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Rajendarreddy Jagapathi <rajendarreddyj@gmail.com>
- Loading branch information
1 parent
704c87f
commit 8c9bd27
Showing
5 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
...ts/basics/src/main/java/com/rajendarreddyj/basics/pattern/singleton/PropertiesHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.rajendarreddyj.basics.pattern.singleton; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Serializable; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
public class PropertiesHolder implements Cloneable, Serializable { | ||
private static final long serialVersionUID = 1007236647121448027L; | ||
private static final String CONNECTION_FILE_WITH_PATH = "src/main/resources/dbconnection.properties"; | ||
private static PropertiesHolder PROPERTIES_HOLDER = null; | ||
private final Properties properties = new Properties(); | ||
|
||
private PropertiesHolder() { | ||
} | ||
|
||
public static PropertiesHolder getInstance() throws IOException { | ||
if (PROPERTIES_HOLDER == null) { | ||
synchronized (PropertiesHolder.class) { | ||
if (PROPERTIES_HOLDER == null) { | ||
InputStream file = new FileInputStream(new File(CONNECTION_FILE_WITH_PATH)); | ||
PROPERTIES_HOLDER = new PropertiesHolder(); | ||
PROPERTIES_HOLDER.properties.load(file); | ||
} | ||
} | ||
} | ||
return PROPERTIES_HOLDER; | ||
} | ||
|
||
@Override | ||
protected Object clone() throws CloneNotSupportedException { | ||
return PROPERTIES_HOLDER; | ||
} | ||
|
||
protected Object readResolve() { | ||
return PROPERTIES_HOLDER; | ||
} | ||
|
||
public String getPropertyValue(final String propertyKey) { | ||
if ((propertyKey != null) && !propertyKey.isEmpty()) { | ||
return this.properties.getProperty(propertyKey); | ||
} | ||
return null; | ||
} | ||
|
||
public Map<String, String> getProperties() { | ||
Map<String, String> propertyMap = new HashMap<>(); | ||
for (String propertyKey : this.properties.stringPropertyNames()) { | ||
propertyMap.put(propertyKey, this.properties.getProperty(propertyKey)); | ||
} | ||
return propertyMap; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...asics/src/main/java/com/rajendarreddyj/basics/pattern/singleton/PropertiesHolderMono.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.rajendarreddyj.basics.pattern.singleton; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
/** | ||
* @author rajendarreddy.jagapathi | ||
*/ | ||
public class PropertiesHolderMono { | ||
private static final String CONNECTION_FILE_WITH_PATH = "src/main/resources/dbconnection.properties"; | ||
private static Properties properties = new Properties(); | ||
static { | ||
InputStream file; | ||
try { | ||
file = new FileInputStream(new File(CONNECTION_FILE_WITH_PATH)); | ||
properties.load(file); | ||
} catch (FileNotFoundException exp) { | ||
exp.printStackTrace(); | ||
} catch (IOException exp) { | ||
exp.printStackTrace(); | ||
} | ||
} | ||
|
||
public static String getPropertyValue(final String propertyKey) { | ||
if ((propertyKey != null) && !propertyKey.isEmpty()) { | ||
return properties.getProperty(propertyKey); | ||
} | ||
return null; | ||
} | ||
|
||
public static Map<String, String> getProperties() { | ||
Map<String, String> propertyMap = new HashMap<>(); | ||
for (String propertyKey : properties.stringPropertyNames()) { | ||
propertyMap.put(propertyKey, properties.getProperty(propertyKey)); | ||
} | ||
return propertyMap; | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
java-projects/basics/src/main/resources/dbconnection.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# DB Connection Properties. | ||
# Oracle DB properties | ||
#jdbc.driver=oracle.jdbc.driver.OracleDriver | ||
#jdbc.url=jdbc:oracle:thin:@localhost:1521:test | ||
#jdbc.username=admin | ||
#jdbc.password=admin | ||
# MySQL DB properties | ||
jdbc.driver=com.mysql.jdbc.Driver | ||
jdbc.url=jdbc:mysql://localhost:3306/sample | ||
jdbc.username=root | ||
jdbc.password=admin |
16 changes: 16 additions & 0 deletions
16
...s/src/test/java/com/rajendarreddyj/basics/pattern/singleton/PropertiesHolderMonoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.rajendarreddyj.basics.pattern.singleton; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* @author rajendarreddy.jagapathi | ||
*/ | ||
public class PropertiesHolderMonoTest { | ||
|
||
@Test | ||
public void test() { | ||
Assert.assertNotNull(PropertiesHolderMono.getPropertyValue("jdbc.driver")); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...asics/src/test/java/com/rajendarreddyj/basics/pattern/singleton/PropertiesHolderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.rajendarreddyj.basics.pattern.singleton; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* @author rajendarreddy.jagapathi | ||
*/ | ||
public class PropertiesHolderTest { | ||
|
||
@Test | ||
public void test() throws IOException { | ||
Assert.assertNotNull(PropertiesHolder.getInstance().getPropertyValue("jdbc.driver")); | ||
} | ||
|
||
} |