Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import com.dougnoel.sentinel.exceptions.IOException;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -39,8 +40,7 @@ public class Configuration {
private static final Properties appProps = new Properties();

private static ConfigurationData sentinelConfigurations = null;

private static final File CONFIGURATION_FILE = new File("conf/sentinel.yml");

private static final String DEFAULT = "default";
private static final String LINUX = "linux";
private static final String MAC = "mac";
Expand All @@ -65,17 +65,18 @@ private static String getOrCreateConfigurationData(String configurationKey) {
if (data != null) {
return data;
}

if(sentinelConfigurations == null) {
try {
File configurationFile = new ConfigurationData().getConfigFile();
ObjectMapper mapper = new ObjectMapper(new YAMLFactory())
.configure(DeserializationFeature
.FAIL_ON_UNKNOWN_PROPERTIES, false);
sentinelConfigurations = mapper.readValue( new ConfigurationData(), ConfigurationData.class );
.FAIL_ON_UNKNOWN_PROPERTIES, false);
sentinelConfigurations = mapper.readValue(configurationFile, ConfigurationData.class );
} catch (Exception e) {
String errorMessage = SentinelStringUtils.format("Could not load the {} property because of the exception: {}." + System.lineSeparator() +
"Please fix the file or pass the property in on the commandline using the -D{}= option.", configurationKey, e.getMessage(), configurationKey, configurationKey);
throw new FileException(errorMessage, e, CONFIGURATION_FILE);
throw new IOException(errorMessage, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,54 @@
import java.io.FileNotFoundException;
import java.util.Map;

import com.dougnoel.sentinel.exceptions.FileException;
import com.dougnoel.sentinel.exceptions.IOException;
import com.dougnoel.sentinel.strings.SentinelStringUtils;
import com.dougnoel.sentinel.system.FileManager;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class ConfigurationData extends File {
public class ConfigurationData {
/**
* Config file for sentinel Configuration Settings. Users can create a sentinel.yml file and we will load
* configs from this file instead of setting them manually on the command line each time. A user can override
* a specific setting by passing in a command line arg or using System.getProperty() in the setup method in the Test java file.
*/
private static final long serialVersionUID = 3930207641065895241L;

private static final String CONFIG_FILE_NAME = "sentinel.yml";
private File configFile;
private String configFilePath;

@JsonProperty("configurations")
private Map<String, Map<String, String>> configurations;

@JsonCreator
public ConfigurationData() throws FileNotFoundException {
super("conf/sentinel.yml");
}
public ConfigurationData() throws FileNotFoundException {
try {
setConfigFile(FileManager.findFilePath(CONFIG_FILE_NAME));
setConfigFilePath(getConfigFile());
configFilePath = configFile.getAbsolutePath();
} catch (FileException e) {
String warningMessage = SentinelStringUtils.format("Could not locate the configuration file {} in the project. Settings present in the file will not be honored.", CONFIG_FILE_NAME);
throw new IOException(warningMessage , e);
}
}

private void setConfigFile(File configurationFile) { configFile = configurationFile; }

private void setConfigFilePath(File configurationFile) { configFilePath = configurationFile.getAbsolutePath(); }

/**
* Returns the configuration file found on instantiation's file object.
* @return File the configuration file object.
*/
public File getConfigFile() { return configFile; }

/**
* Returns the configuration file found on instantiation's absolute path as a string.
* @return String the absolute path of the configuration file loaded.
*/
public String getConfigFilePath() { return configFilePath; }

/**
* Returns the configuration value for the given environment and key .
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/dougnoel/sentinel/system/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static String loadJavascript(String path) throws IOException {
}

/**
* Returns the absolute path to the file searching the root directory of the project
* Returns the file given by searching from the root directory of the project
* and any sub directories.
* If file is not found there, this method searches the java classpath for a file matching the given filename. This check encompasses all resources on the classpath.
* See <a href="https://docs.oracle.com/javase/tutorial/essential/environment/paths.html">official Oracle documentation</a>
Expand All @@ -64,7 +64,7 @@ public static String loadJavascript(String path) throws IOException {
* @return File the file found
*/
public static File findFilePath(String fileName) {
File result = searchDirectory(new File("src" + File.separator), fileName);
File result = searchDirectory(new File(System.getProperty("user.dir") + File.separator), fileName);

if(result == null){
List<URL> resourceNames;
Expand Down