Changed itoopie to write configuration files to ./itoopie/ or AppData/Roaming/itoopie/.
This commit is contained in:
@ -2,6 +2,7 @@ package net.i2p.itoopie.configuration;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
@ -22,8 +23,9 @@ import org.apache.commons.logging.LogFactory;
|
||||
*
|
||||
*/
|
||||
public class ConfigurationManager {
|
||||
private static final String DEFAULT_CONFIG_LOCATION = "itoopie.conf";
|
||||
private static final String DEFAULT_CONFIG_NAME = "itoopie.conf";
|
||||
private static final Log _log = LogFactory.getLog(ConfigurationManager.class);
|
||||
private static final String APP_DIR_NAME = "itoopie";
|
||||
|
||||
|
||||
private static ConfigurationManager instance;
|
||||
@ -64,16 +66,16 @@ public class ConfigurationManager {
|
||||
*/
|
||||
public static void readConfFile(){
|
||||
try {
|
||||
BufferedReader br = new BufferedReader(new FileReader(DEFAULT_CONFIG_LOCATION));
|
||||
BufferedReader br = new BufferedReader(new FileReader(getAppConfDir() + DEFAULT_CONFIG_NAME));
|
||||
String input;
|
||||
while ((input = br.readLine()) != null){
|
||||
parseConfigStr(input);
|
||||
}
|
||||
br.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
_log.info("Unable to find config file, " + DEFAULT_CONFIG_LOCATION);
|
||||
_log.info("Unable to find config file, " + getAppConfDir() + DEFAULT_CONFIG_NAME);
|
||||
} catch (IOException e) {
|
||||
_log.error("Unable to read from config file, " + DEFAULT_CONFIG_LOCATION);
|
||||
_log.error("Unable to read from config file, " + getAppConfDir() + DEFAULT_CONFIG_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
@ -92,13 +94,13 @@ public class ConfigurationManager {
|
||||
tree.put(e.getKey(), e.getValue().toString());
|
||||
}
|
||||
try {
|
||||
BufferedWriter bw = new BufferedWriter(new FileWriter(DEFAULT_CONFIG_LOCATION));
|
||||
BufferedWriter bw = new BufferedWriter(new FileWriter(getAppConfDir() + DEFAULT_CONFIG_NAME));
|
||||
for (Entry<String,String> e : tree.entrySet()){
|
||||
bw.write(e.getKey() + "=" + e.getValue() + "\r\n");
|
||||
}
|
||||
bw.close();
|
||||
} catch (IOException e1) {
|
||||
_log.error("Couldn't open file, " + DEFAULT_CONFIG_LOCATION + " for writing config.");
|
||||
_log.error("Couldn't open file, " + getAppConfDir() + DEFAULT_CONFIG_NAME + " for writing config.");
|
||||
}
|
||||
}
|
||||
|
||||
@ -206,4 +208,22 @@ public class ConfigurationManager {
|
||||
public void setConf(String settingName, boolean bool){
|
||||
booleanConfigurations.put(settingName, bool);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file path to the configuration directory. If the directory does not yet exist, creates it.
|
||||
* @return Application configuration directory.
|
||||
*/
|
||||
public static String getAppConfDir(){
|
||||
String dir;
|
||||
if (System.getenv("APPDATA") != null && !System.getenv("APPDATA").equals("")){
|
||||
dir = System.getenv("APPDATA")+ File.separator + APP_DIR_NAME + File.separator; // Windows path
|
||||
} else {
|
||||
dir = System.getProperties().getProperty("user.home") + File.separator + "." + APP_DIR_NAME + File.separator; // Linux/mac path
|
||||
}
|
||||
File dirFile = new File(dir);
|
||||
if (!dirFile.exists()){
|
||||
dirFile.mkdirs();
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
}
|
@ -22,6 +22,8 @@ import java.security.cert.X509Certificate;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.TrustManagerFactory;
|
||||
|
||||
import net.i2p.itoopie.configuration.ConfigurationManager;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
@ -160,12 +162,12 @@ public class CertificateManager {
|
||||
if (_ks == null){
|
||||
try {
|
||||
_ks = KeyStore.getInstance(DEFAULT_KEYSTORE_TYPE);
|
||||
if ((new File(DEFAULT_KEYSTORE_LOCATION)).exists()){
|
||||
InputStream is = new FileInputStream(DEFAULT_KEYSTORE_LOCATION);
|
||||
if ((new File(ConfigurationManager.getAppConfDir() + DEFAULT_KEYSTORE_LOCATION)).exists()){
|
||||
InputStream is = new FileInputStream(ConfigurationManager.getAppConfDir() + DEFAULT_KEYSTORE_LOCATION);
|
||||
_ks.load(is, DEFAULT_KEYSTORE_PASSWORD.toCharArray());
|
||||
return _ks;
|
||||
} else {
|
||||
throw new IOException("KeyStore file " + DEFAULT_KEYSTORE_LOCATION + "wasn't readable");
|
||||
throw new IOException("KeyStore file " + ConfigurationManager.getAppConfDir() + DEFAULT_KEYSTORE_LOCATION + "wasn't readable");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// Ignore. Not an issue. Let's just create a new keystore instead.
|
||||
@ -186,7 +188,7 @@ public class CertificateManager {
|
||||
|
||||
private static void saveKeyStore(KeyStore ks){
|
||||
try {
|
||||
ks.store(new FileOutputStream(DEFAULT_KEYSTORE_LOCATION), DEFAULT_KEYSTORE_PASSWORD.toCharArray());
|
||||
ks.store(new FileOutputStream(ConfigurationManager.getAppConfDir() + DEFAULT_KEYSTORE_LOCATION), DEFAULT_KEYSTORE_PASSWORD.toCharArray());
|
||||
} catch (KeyStoreException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
|
Reference in New Issue
Block a user