Compare commits
4 Commits
master
...
feature-ti
| Author | SHA1 | Date |
|---|---|---|
|
|
b2e6f020b0 | 3 years ago |
|
|
f866ca44f9 | 3 years ago |
|
|
0d1bed4265 | 3 years ago |
|
|
9dc80d90f2 | 3 years ago |
9 changed files with 314 additions and 101 deletions
@ -1,79 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<project version="4"> |
||||
<component name="AutoImportSettings"> |
||||
<option name="autoReloadType" value="SELECTIVE" /> |
||||
</component> |
||||
<component name="ChangeListManager"> |
||||
<list default="true" id="b9554e7b-3332-419c-960c-8bfbee0d5311" name="Changes" comment=""> |
||||
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" /> |
||||
<change afterPath="$PROJECT_DIR$/.idea/aws.xml" afterDir="false" /> |
||||
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" /> |
||||
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" /> |
||||
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> |
||||
<change afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" /> |
||||
<change afterPath="$PROJECT_DIR$/src/main/java/org/leolo/nrdatad/App.java" afterDir="false" /> |
||||
<change afterPath="$PROJECT_DIR$/src/test/java/org/leolo/AppTest.java" afterDir="false" /> |
||||
</list> |
||||
<option name="SHOW_DIALOG" value="false" /> |
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" /> |
||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> |
||||
<option name="LAST_RESOLUTION" value="IGNORE" /> |
||||
</component> |
||||
<component name="Git.Settings"> |
||||
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" /> |
||||
</component> |
||||
<component name="ProjectId" id="2GCtKiOdhwqUrJrlolmjYzZXLwa" /> |
||||
<component name="ProjectLevelVcsManager" settingsEditedManually="true" /> |
||||
<component name="ProjectViewState"> |
||||
<option name="flattenPackages" value="true" /> |
||||
<option name="hideEmptyMiddlePackages" value="true" /> |
||||
<option name="showLibraryContents" value="true" /> |
||||
</component> |
||||
<component name="PropertiesComponent"><![CDATA[{ |
||||
"keyToString": { |
||||
"RunOnceActivity.OpenProjectViewOnStart": "true", |
||||
"RunOnceActivity.ShowReadmeOnStart": "true", |
||||
"WebServerToolWindowFactoryState": "false" |
||||
} |
||||
}]]></component> |
||||
<component name="RunManager"> |
||||
<configuration name="Main" type="Application" factoryName="Application"> |
||||
<option name="MAIN_CLASS_NAME" value="org.leolo.nrdatad.App" /> |
||||
<module name="nr-data-damon" /> |
||||
<extension name="software.aws.toolkits.jetbrains.core.execution.JavaAwsConnectionExtension"> |
||||
<option name="credential" /> |
||||
<option name="region" /> |
||||
<option name="useCurrentConnection" value="false" /> |
||||
</extension> |
||||
<method v="2"> |
||||
<option name="Make" enabled="true" /> |
||||
</method> |
||||
</configuration> |
||||
</component> |
||||
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" /> |
||||
<component name="TaskManager"> |
||||
<task active="true" id="Default" summary="Default task"> |
||||
<changelist id="b9554e7b-3332-419c-960c-8bfbee0d5311" name="Changes" comment="" /> |
||||
<created>1665904334835</created> |
||||
<option name="number" value="Default" /> |
||||
<option name="presentableId" value="Default" /> |
||||
<updated>1665904334835</updated> |
||||
<workItem from="1665904336661" duration="319000" /> |
||||
</task> |
||||
<servers /> |
||||
</component> |
||||
<component name="TypeScriptGeneratedFilesManager"> |
||||
<option name="version" value="3" /> |
||||
</component> |
||||
<component name="Vcs.Log.Tabs.Properties"> |
||||
<option name="TAB_STATES"> |
||||
<map> |
||||
<entry key="MAIN"> |
||||
<value> |
||||
<State /> |
||||
</value> |
||||
</entry> |
||||
</map> |
||||
</option> |
||||
</component> |
||||
</project> |
||||
@ -1,11 +1,60 @@
|
||||
package org.leolo.nrdatad; |
||||
|
||||
|
||||
import org.apache.commons.cli.*; |
||||
import org.apache.logging.log4j.LogManager; |
||||
import org.apache.logging.log4j.Logger; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
/** |
||||
* Hello world! |
||||
* |
||||
*/ |
||||
public class App { |
||||
|
||||
Logger log = LogManager.getLogger(); |
||||
public static void main( String[] args ) { |
||||
System.out.println( "Hello World!" ); |
||||
Option confFile = Option.builder() |
||||
.required(false) |
||||
.hasArg(true) |
||||
.argName("fileName") |
||||
.option("c") |
||||
.longOpt("config-file") |
||||
.build(); |
||||
Options opts = new Options(); |
||||
opts.addOption(confFile); |
||||
String confPath = "nrdatad.conf"; |
||||
try { |
||||
CommandLine cli = new DefaultParser().parse(opts, args); |
||||
if(cli.hasOption(confFile)){ |
||||
confPath = cli.getOptionValue(confFile); |
||||
} |
||||
} catch (ParseException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
new App().run(confPath); |
||||
} |
||||
|
||||
private void run(String configPath){ |
||||
log.always().log("System Started."); |
||||
try { |
||||
ConfigurationManager.getInstance().loadConfiguration(configPath); |
||||
} catch (IOException e) { |
||||
log.atError().withThrowable(e).log("Unable to load confog file"); |
||||
return; |
||||
} |
||||
// log.atDebug()
|
||||
if(ConfigurationManager.getInstance().getProperty("general.debug","false").equals("true")){ |
||||
for(String key:ConfigurationManager.getInstance().stringPropertyNames()){ |
||||
if(key.endsWith("pwd")){ |
||||
log.atDebug().log("Config: {} : ****({} chars)", key, ConfigurationManager.getInstance().getProperty(key).length()); |
||||
|
||||
}else{ |
||||
log.atDebug().log("Config: {} : {}", key, ConfigurationManager.getInstance().getProperty(key)); |
||||
|
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,108 @@
|
||||
package org.leolo.nrdatad; |
||||
|
||||
import org.apache.logging.log4j.LogManager; |
||||
import org.apache.logging.log4j.Logger; |
||||
|
||||
import java.io.FileReader; |
||||
import java.io.IOException; |
||||
import java.io.PrintStream; |
||||
import java.io.PrintWriter; |
||||
import java.util.*; |
||||
|
||||
public final class ConfigurationManager { |
||||
private static ConfigurationManager instance; |
||||
Logger logger = LogManager.getLogger(); |
||||
Properties prop = new Properties(); |
||||
|
||||
public synchronized static ConfigurationManager getInstance(){ |
||||
if(instance==null){ |
||||
instance = new ConfigurationManager(); |
||||
} |
||||
return instance; |
||||
} |
||||
|
||||
private ConfigurationManager(){ |
||||
|
||||
} |
||||
|
||||
public void loadConfiguration(String configPath) throws IOException{ |
||||
logger.atInfo().log("Loading configuration file {}.", configPath); |
||||
try(FileReader reader = new FileReader(configPath)) { |
||||
prop.load(reader); |
||||
} |
||||
logger.atInfo().log("Loaded {} setting(s)", prop.size()); |
||||
} |
||||
|
||||
public Object setProperty(String key, String value) { |
||||
return prop.setProperty(key, value); |
||||
} |
||||
|
||||
public String getProperty(String key) { |
||||
return prop.getProperty(key); |
||||
} |
||||
|
||||
public String getProperty(String key, String defaultValue) { |
||||
return prop.getProperty(key, defaultValue); |
||||
} |
||||
|
||||
public Enumeration<?> propertyNames() { |
||||
return prop.propertyNames(); |
||||
} |
||||
|
||||
public Set<String> stringPropertyNames() { |
||||
return prop.stringPropertyNames(); |
||||
} |
||||
|
||||
public void list(PrintStream out) { |
||||
prop.list(out); |
||||
} |
||||
|
||||
public void list(PrintWriter out) { |
||||
prop.list(out); |
||||
} |
||||
|
||||
public int size() { |
||||
return prop.size(); |
||||
} |
||||
|
||||
public boolean isEmpty() { |
||||
return prop.isEmpty(); |
||||
} |
||||
|
||||
public Enumeration<Object> keys() { |
||||
return prop.keys(); |
||||
} |
||||
|
||||
public Enumeration<Object> elements() { |
||||
return prop.elements(); |
||||
} |
||||
|
||||
public boolean contains(Object value) { |
||||
return prop.contains(value); |
||||
} |
||||
|
||||
public boolean containsValue(Object value) { |
||||
return prop.containsValue(value); |
||||
} |
||||
|
||||
public boolean containsKey(Object key) { |
||||
return prop.containsKey(key); |
||||
} |
||||
|
||||
public Set<Object> keySet() { |
||||
return prop.keySet(); |
||||
} |
||||
|
||||
public Collection<Object> values() { |
||||
return prop.values(); |
||||
} |
||||
|
||||
public Set<Map.Entry<Object, Object>> entrySet() { |
||||
return prop.entrySet(); |
||||
} |
||||
|
||||
protected void clear(){ |
||||
prop.clear(); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<Configuration status="warn"> |
||||
<Appenders> |
||||
<Console name="Console" target="SYSTEM_OUT"> |
||||
<PatternLayout |
||||
pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> |
||||
</Console> |
||||
<File name="RIlog" fileName="ri_log"> |
||||
<PatternLayout |
||||
pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> |
||||
</File> |
||||
</Appenders> |
||||
<Loggers> |
||||
<Root level="debug"> |
||||
<AppenderRef ref="Console" /> |
||||
</Root> |
||||
<Logger name="org.leolo.nrdd"> |
||||
<AppenderRef ref="Console" /> |
||||
<AppenderRef ref="RIlog" /> |
||||
</Logger> |
||||
</Loggers> |
||||
</Configuration> |
||||
@ -0,0 +1,92 @@
|
||||
package org.leolo.nrdatad; |
||||
|
||||
import org.apache.logging.log4j.LogBuilder; |
||||
import org.apache.logging.log4j.LogManager; |
||||
import org.apache.logging.log4j.Logger; |
||||
import org.junit.*; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileNotFoundException; |
||||
import java.io.IOException; |
||||
import java.io.PrintWriter; |
||||
import java.nio.file.Files; |
||||
import java.util.Random; |
||||
import static org.junit.Assert.*; |
||||
|
||||
public class ConfigurationTest { |
||||
|
||||
public static final char[] randomChars = "1234567890qwertyuiopasdfghjklzxcvbnm".toCharArray(); |
||||
private static Logger log = LogManager.getLogger(); |
||||
|
||||
private static String fileName; |
||||
|
||||
@Before public void setUpClass(){ |
||||
int round = 0; |
||||
while(true) { |
||||
fileName = "test-"+getRandomFileName()+".tmp"; |
||||
log.always().log("File name is {}", fileName); |
||||
if(!new File(fileName).exists()){ |
||||
break; |
||||
} |
||||
if(++round>48){ |
||||
assertTrue("Cannot get a file name", false); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private static String getRandomFileName(){ |
||||
StringBuilder sb = new StringBuilder(); |
||||
Random r = new Random(); |
||||
for(int i=0;i<16;i++){ |
||||
sb.append(randomChars[r.nextInt(randomChars.length)]); |
||||
} |
||||
return sb.toString(); |
||||
} |
||||
|
||||
@Test public void basicLoad() throws Exception{ |
||||
assertEquals("Dirty configs", 0, ConfigurationManager.getInstance().size()); |
||||
//Step 1: Create a simple one
|
||||
try(PrintWriter out = new PrintWriter(new File(fileName))){ |
||||
out.println("key1=value1"); |
||||
out.println("key2=value2"); |
||||
out.println("key3=value3"); |
||||
out.println("key4=value4"); |
||||
out.println("#key5=value5"); |
||||
}catch(IOException e){ |
||||
log.always().withThrowable(e).log("Unable to test"); |
||||
assert false; |
||||
} |
||||
ConfigurationManager.getInstance().loadConfiguration(fileName); |
||||
assertEquals("Config key count mismatch", 4, ConfigurationManager.getInstance().size()); |
||||
assertEquals("Config key count mismatch", "value1", ConfigurationManager.getInstance().getProperty("key1")); |
||||
assertEquals("Config key count mismatch", "value3", ConfigurationManager.getInstance().getProperty("key3")); |
||||
assertEquals("Config key count mismatch", "value4", ConfigurationManager.getInstance().getProperty("key4")); |
||||
assertEquals("Config key count mismatch", "value2", ConfigurationManager.getInstance().getProperty("key2")); |
||||
|
||||
assertEquals("Config key count mismatch", null, ConfigurationManager.getInstance().getProperty("key5")); |
||||
} |
||||
|
||||
@Test public void notExistFile(){ |
||||
assertEquals("Dirty configs", 0, ConfigurationManager.getInstance().size()); |
||||
try { |
||||
ConfigurationManager.getInstance().loadConfiguration(fileName); |
||||
}catch(IOException e){ |
||||
return; |
||||
} |
||||
assertFalse("Exception not thrown", true); |
||||
} |
||||
|
||||
@After public void cleanUpEach(){ |
||||
try { |
||||
Files.deleteIfExists(new File(fileName).toPath()); |
||||
log.atInfo().log("Removed file {}", fileName); |
||||
} catch (IOException e) { |
||||
log.always().withThrowable(e).log("Unable to remove file"); |
||||
} |
||||
ConfigurationManager.getInstance().clear(); |
||||
} |
||||
|
||||
@AfterClass public static void cleanup(){ |
||||
new File(fileName).deleteOnExit(); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue