-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85299a0
commit 8fea4ee
Showing
10 changed files
with
243 additions
and
82 deletions.
There are no files selected for viewing
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
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
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
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 |
---|---|---|
@@ -1,6 +1,16 @@ | ||
import db.DatabaseHelper; | ||
import env.SharedEnvVars; | ||
|
||
public class PatchFixMain { | ||
public static void main(String[] args) { | ||
new PatchFinderMain().start(); | ||
new FixFinderMain().start(); | ||
SharedEnvVars.initializeEnvVars(false); | ||
final DatabaseHelper dbh = new DatabaseHelper( | ||
SharedEnvVars.getDatabaseType(), | ||
SharedEnvVars.getHikariUrl(), | ||
SharedEnvVars.getHikariUser(), | ||
SharedEnvVars.getHikariPassword() | ||
); | ||
new PatchFinderMain(dbh).start(); | ||
new FixFinderMain(dbh).start(); | ||
} | ||
} |
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,112 @@ | ||
package env; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static env.EnvVarLoader.loadEnvVarsFromFile; | ||
|
||
public class SharedEnvVars { | ||
private static final Logger logger = LogManager.getLogger(PatchFinderEnvVars.class); | ||
private static final String envVarPath = "env.list"; | ||
|
||
// Default values for database environment variables | ||
private static String databaseType = "mysql"; | ||
private static String hikariUrl = "jdbc:mysql://localhost:3306/nvip?useSSL=false&allowPublicKeyRetrieval=true"; | ||
private static String hikariUser = "root"; | ||
private static String hikariPassword = "root"; | ||
|
||
public static String getDatabaseType() { return databaseType; } | ||
public static String getHikariUrl() { return hikariUrl; } | ||
public static String getHikariUser() { return hikariUser; } | ||
public static String getHikariPassword() { return hikariPassword; } | ||
|
||
/** | ||
* Loads environment variables from both env.list file and System.getenv(). If both of these fail, resorts to | ||
* default values defined above. Prioritizes System.getenv() first and then from file second. | ||
*/ | ||
public static void initializeEnvVars(boolean testMode) { | ||
logger.info("CURRENT PATH --> " + System.getProperty("user.dir")); | ||
if(testMode) logger.info("Initializing Test Environment Variables..."); | ||
else logger.info("Initializing Environment Variables..."); | ||
|
||
Map<String, String> fileProps = null; | ||
Map<String, String> systemProps = System.getenv(); | ||
String filePath = envVarPath; | ||
if(testMode) filePath = "src/test/" + filePath; | ||
|
||
try { | ||
// Assumes in `nvip-crawler/patchfinder` working directory | ||
fileProps = loadEnvVarsFromFile(filePath); | ||
} catch (FileNotFoundException e){ | ||
// If that path doesn't work, assumes we are in `nvip-crawler` directory and tries new path with `patchfinder` appended to it | ||
try{ | ||
String possiblePath = "patchfinder\\" + filePath; | ||
fileProps = loadEnvVarsFromFile(possiblePath); | ||
} catch (Exception ignored) {} | ||
} | ||
|
||
// If env vars couldn't be loaded from file, pass in empty map | ||
if(fileProps == null) fileProps = new HashMap<>(); | ||
fetchEnvVars(systemProps, fileProps); | ||
} | ||
|
||
/** | ||
* Attempts to fetch all required environment variables from props map safely, logging any | ||
* missing or incorrect variables. | ||
* | ||
* If environment variable is not found from System.getenv(), it will attempt to fetch it from the loaded file. If it | ||
* is still not found, it will resort to default value. Priority: System.getenv() <- env.list file <- default values | ||
* | ||
* @param systemProps map of environment variables from System.getenv() | ||
* @param fileProps map of environment variables read from file | ||
*/ | ||
private static void fetchEnvVars(Map<String, String> systemProps, Map<String, String> fileProps) { | ||
fetchHikariEnvVars(systemProps, fileProps); | ||
} | ||
|
||
/** | ||
* Initialize database env vars | ||
* | ||
* @param systemProps map of environment variables from System.getenv() | ||
* @param fileProps map of environment variables read from file | ||
*/ | ||
private static void fetchHikariEnvVars(Map<String, String> systemProps, Map<String, String> fileProps) { | ||
|
||
if(systemProps.containsKey("DB_TYPE")) { | ||
databaseType = systemProps.get("DB_TYPE"); | ||
logger.info("Setting DB_TYPE to {}", databaseType); | ||
} else if (fileProps.containsKey("DB_TYPE")) { | ||
databaseType = fileProps.get("DB_TYPE"); | ||
logger.info("Setting DB_TYPE to {}", databaseType); | ||
} else logger.warn("Could not fetch DB_TYPE from env vars, defaulting to {}", databaseType); | ||
|
||
if(systemProps.containsKey("HIKARI_URL")) { | ||
hikariUrl = systemProps.get("HIKARI_URL"); | ||
logger.info("Setting HIKARI_URL to {}", hikariUrl); | ||
} else if (fileProps.containsKey("HIKARI_URL")) { | ||
hikariUrl = fileProps.get("HIKARI_URL"); | ||
logger.info("Setting HIKARI_URL to {}", hikariUrl); | ||
} else logger.warn("Could not fetch HIKARI_URL from env vars, defaulting to {}", hikariUrl); | ||
|
||
if(systemProps.containsKey("HIKARI_USER")) { | ||
hikariUser = systemProps.get("HIKARI_USER"); | ||
logger.info("Setting HIKARI_USER to {}", hikariUser); | ||
} else if (fileProps.containsKey("HIKARI_USER")) { | ||
hikariUser = fileProps.get("HIKARI_USER"); | ||
logger.info("Setting HIKARI_USER to {}", hikariUser); | ||
} else logger.warn("Could not fetch HIKARI_USER from env vars, defaulting to {}", hikariUser); | ||
|
||
if(systemProps.containsKey("HIKARI_PASSWORD")) { | ||
hikariPassword = systemProps.get("HIKARI_PASSWORD"); | ||
logger.info("Setting HIKARI_PASSWORD to {}", hikariPassword); | ||
} else if (fileProps.containsKey("HIKARI_PASSWORD")) { | ||
hikariPassword = fileProps.get("HIKARI_PASSWORD"); | ||
logger.info("Setting HIKARI_PASSWORD to {}", hikariPassword); | ||
} else logger.warn("Could not fetch HIKARI_PASSWORD from env vars, defaulting to {}", hikariPassword); | ||
|
||
} | ||
} |
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
Oops, something went wrong.