Skip to content

Commit

Permalink
adding prop holder
Browse files Browse the repository at this point in the history
Signed-off-by: Rajendarreddy Jagapathi <rajendarreddyj@gmail.com>
  • Loading branch information
rajendarreddyj committed Aug 3, 2018
1 parent 704c87f commit 8c9bd27
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
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;
}
}
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 java-projects/basics/src/main/resources/dbconnection.properties
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
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"));
}

}
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"));
}

}

0 comments on commit 8c9bd27

Please sign in to comment.