Skip to content

Commit

Permalink
local config: read properties from resource
Browse files Browse the repository at this point in the history
  • Loading branch information
short000 committed Mar 19, 2018
1 parent 6a151db commit b0ebad1
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 122 deletions.
180 changes: 92 additions & 88 deletions src/main/java/org/null0/edi/debatcher/Config.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.null0.edi.debatcher;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.NotDirectoryException;
Expand All @@ -13,76 +13,102 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jdk.internal.jline.internal.Log;

/***
*
*
*
*
*/
// TODO: define an interface for Config so that for unit testing we can use it instead without any file I/O dependencies
public class Config {
/** If this environment variable exists it must contain full valid path to properties file */
private static final String ENVAR_NAME = "edi_debatch_config_file";

/** Default expected local properties file name if config_env_name environment variable does not exist */
private static final String FILE_NAME = "debatcher.properties";

/** Expected name for output directory variable in java properties file */
private static final String OUTDIR_PROP_NAME = "outputDirectory";

private static final String ENVAR_NAME = "edi_debatch_config_file"; // If this environment variable exists it must contain full valid path to properties file
private static final String FILE_NAME = "debatcher.properties"; // Default expected local properties file name if config_env_name environment variable does not exist
private static final Logger LOG = LoggerFactory.getLogger(Config.class); // Logger

private Properties properties;
private ConfigurationSource mode;
private Path outDir; // configuration
private int bufferSize; // configuration

// Configuration state
private Path outDir;
private int bufferSize;

public Config() {
// Determine path to configuration file.
// First try to use environment variable override
this.mode = ConfigurationSource.ENVIRONMENT_VARIABLE;
String path = System.getenv(ENVAR_NAME);

boolean ok = true;
try {
// ...if environment variable not defined then default to local
if (path == null || path.isEmpty()) {
path = getLocalDir();
if (!path.endsWith("/")) {
path += "//";
}
path += FILE_NAME;
this.mode = ConfigurationSource.PROPERTIES;
public Config() {
if (!initFromEnvVar()) {
if (!initFromLocal()) {
initFromFailover();
}

// Initialize properties from path
this.properties = new Properties();
this.properties.load(new FileInputStream(path)); // Load it
} catch (FileNotFoundException e) {
LOG.warn("Bad or missing java properties file '{}'", path);
ok = false;
} catch (IOException e) {
LOG.warn("I/O error reading java properties file '{}'", path);
ok = false;
}

// If for any reason we couldn't fetch a configuration file, use defaults
if (!ok) {
setToFailover();

try {
setProperties();
} catch (Exception e) {
Log.error("Exception in CTOR", e);
}
}

public enum ConfigurationSource { ENVIRONMENT_VARIABLE, PROPERTIES, OVERRIDE, FAILOVER }
public Config (String file) throws Exception {
this.mode = ConfigurationSource.EXTERNAL_PROPERTIES;
initFromFile(file);
setProperties();
}

public enum ConfigurationSource { ENVIRONMENT_VARIABLE, LOCAL_PROPERTIES, EXTERNAL_PROPERTIES, OVERRIDE, FAILOVER }
public ConfigurationSource getConfigurationSource() {
return this.mode;
}

private void setToFailover() {
/** @param the output directory */
public Path getOutputDirectory() {
return this.outDir;
}

/** @return data chunk buffer size */
public int getBufferSize() {
return bufferSize;
}

private void setProperties() throws Exception {
this.bufferSize = Integer.parseInt(this.properties.getProperty("buffer_size"));
this.outDir = toPath(this.properties.getProperty("output_directory"));
}

private boolean initFromEnvVar() {
this.mode = ConfigurationSource.ENVIRONMENT_VARIABLE;
String envVarValue = System.getenv(ENVAR_NAME);
return envVarValue == null ? false : initFromFile(envVarValue);
}

private boolean initFromLocal() {
this.mode = ConfigurationSource.LOCAL_PROPERTIES;
try {
this.properties = this.getPropertiesFromResource();
} catch (IOException e) {
return false;
}
return true;
}

private boolean initFromFile(String path) {
this.mode = ConfigurationSource.EXTERNAL_PROPERTIES;
try {
Properties p = getPropertiesFromFile(path);
if (p == null) {
return false;
}
this.properties = p;
} catch (IOException e) {
return false;
}
return true;
}

private void initFromFailover() {
this.mode = ConfigurationSource.FAILOVER;

final int DEFAULT_BUFFER_SIZE = 1024;
this.bufferSize = DEFAULT_BUFFER_SIZE;

try {
this.outDir = toPath(getLocalDir());
this.bufferSize = DEFAULT_BUFFER_SIZE;
this.mode = ConfigurationSource.FAILOVER;
} catch (NotDirectoryException e) {
LOG.error("Unable to determine java resource path", e); // This should never happen
}
Expand All @@ -99,6 +125,23 @@ private String getLocalDir() {
return path;
}

private Properties getPropertiesFromFile(String path) throws IOException {
Properties p = new Properties();
p.load(new FileInputStream(path));
return p;
}

private Properties getPropertiesFromResource() throws IOException {
Properties p = null;
try(InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(FILE_NAME)) {
p = new Properties();
p.load(stream);
} catch (IOException e) {
throw e;
}
return p;
}

private Path toPath (final String path) throws NotDirectoryException {
if (path == null || path.isEmpty()) {
throw new NotDirectoryException("Path was empty or null");
Expand All @@ -109,43 +152,4 @@ private Path toPath (final String path) throws NotDirectoryException {
}
return p;
}

/**
* @return output directory
*/
public void setOutputDir(String path) throws NotDirectoryException {
this.outDir = toPath(path);
this.mode = ConfigurationSource.OVERRIDE;
}

/**
* @param outputDirectory the output directory
*/
public Path getOutputDir() {
if (this.outDir == null ) {
String dirName = null;
try {
dirName = this.properties.getProperty(Config.OUTDIR_PROP_NAME);
this.outDir = toPath(dirName);
} catch (NotDirectoryException e) {
LOG.warn("Bad output directory name '{}'", dirName);
setToFailover();
}
}
return this.outDir;
}

/**
* @return data chunk buffer size
*/
public int getBufferSize() {
return bufferSize;
}

/**
* @param bufferSize data chunk buffer size
*/
public void setBufferSize(int bufferSize) {
this.bufferSize = bufferSize;
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/null0/edi/debatcher/Debatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void debatch(String transactionId, InputStream is) throws Exception {
logger.info("debatching started..." + transactionId);
this.transactionId = transactionId;
this.inputStream = is;
this.outputLocation = this.config.getOutputDir().toString();
this.outputLocation = this.config.getOutputDirectory().toString();
this.batchIdMetadata = metadataLogger.logBatchSubmissionData(transactionId);

readInterchangeControls();
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/debatcher.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
buffer_size=8096
output_directory=./
41 changes: 10 additions & 31 deletions src/test/java/org/null0/edi/debatcher/test/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,25 @@

public class ConfigTest {

@Test
public void testSetOutputDir() {
String dir = Paths.get(".").toAbsolutePath().normalize().toString();
try {
Config config = new Config();
config.setOutputDir(dir);
assertEquals(Config.ConfigurationSource.OVERRIDE, config.getConfigurationSource());
} catch (NotDirectoryException e) {
fail("NotDirectoryException");
}
}

@Test
public void testGetBufferSize_Failover() {
assertEquals(1024, new Config().getBufferSize());
}

@Test
public void testGetOutputDir_Failover() {
Config config = testOutputDir();
assertEquals(Config.ConfigurationSource.FAILOVER, config.getConfigurationSource());
public void testLocalPropertiesFile() throws Exception {
Config config = testOutputDirectory();
assertEquals(Config.ConfigurationSource.LOCAL_PROPERTIES, config.getConfigurationSource());
assertEquals(8096, config.getBufferSize());
assertEquals(".", config.getOutputDirectory().toString());
}

@Test
public void testGetOutputDir_PropertiesFile() {
public void testExternalPropertiesFile() throws Exception {
// TODO: we need to set up the local file "debatcher.properties" for this to pass
Config config = testOutputDir();
assertEquals(Config.ConfigurationSource.PROPERTIES, config.getConfigurationSource());
Config config = testOutputDirectory();
assertEquals(Config.ConfigurationSource.EXTERNAL_PROPERTIES, config.getConfigurationSource());
}

@Test
public void testGetOutputDir_EnvironmentVariable() {
// TODO: we need to set up the a "debatcher.properties" files in a subdirectory and point an environment variable to it for this to pass
Config config =testOutputDir();
assertEquals(Config.ConfigurationSource.ENVIRONMENT_VARIABLE, config.getConfigurationSource());
}

private Config testOutputDir() {
private Config testOutputDirectory() throws Exception {
Config config = new Config();
Path path = config.getOutputDir();
Path path = config.getOutputDirectory();
assertTrue(Files.exists(path));
return config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class DebatcherTest {

@Before
public void setUp() throws Exception {
directory = new Config().getOutputDir().toString();
directory = new Config().getOutputDirectory().toString();
/*
* logger.info("deleting the old messages from the output folder..."); File dir
* = new File(DIRECTORY+"/output"); for(File file: dir.listFiles())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class EdiValidatorTest {

@org.junit.Before
public void setup() throws Exception {
directory = new Config().getOutputDir().toString();
directory = new Config().getOutputDirectory().toString();
}

@Ignore
Expand Down

0 comments on commit b0ebad1

Please sign in to comment.