Conf file location override for plugin

Change getAppConfDir() to return File
This commit is contained in:
zzz
2022-01-10 10:13:59 -05:00
parent 8aa7d51813
commit 153922c144
3 changed files with 32 additions and 15 deletions

View File

@ -14,6 +14,7 @@ package net.i2p.itoopie.plugin;
* *
*/ */
import java.io.File;
import javax.swing.UIManager; import javax.swing.UIManager;
import net.i2p.I2PAppContext; import net.i2p.I2PAppContext;
@ -21,6 +22,7 @@ import net.i2p.app.*;
import static net.i2p.app.ClientAppState.*; import static net.i2p.app.ClientAppState.*;
import net.i2p.util.Log; import net.i2p.util.Log;
import net.i2p.itoopie.configuration.ConfigurationManager;
import net.i2p.itoopie.gui.GUIHelper; import net.i2p.itoopie.gui.GUIHelper;
import net.i2p.itoopie.gui.TrayManager; import net.i2p.itoopie.gui.TrayManager;
import net.i2p.itoopie.gui.WindowHandler; import net.i2p.itoopie.gui.WindowHandler;
@ -41,6 +43,10 @@ public class Itoopie implements ClientApp {
_log = ctx.logManager().getLog(Itoopie.class); _log = ctx.logManager().getLog(Itoopie.class);
_mgr = mgr; _mgr = mgr;
_state = INITIALIZED; _state = INITIALIZED;
// Set the conf dir so ConfigurationManager can find it
File d = new File(ctx.getConfigDir(), "plugins");
d = new File(d, "itoopie");
System.setProperty(ConfigurationManager.PROP_CONF_DIR, d.getAbsolutePath());
} }
/** /**

View File

@ -26,6 +26,10 @@ public class ConfigurationManager {
private static final String DEFAULT_CONFIG_NAME = "itoopie.conf"; private static final String DEFAULT_CONFIG_NAME = "itoopie.conf";
private static final Log _log = LogFactory.getLog(ConfigurationManager.class); private static final Log _log = LogFactory.getLog(ConfigurationManager.class);
private static final String APP_DIR_NAME = "itoopie"; private static final String APP_DIR_NAME = "itoopie";
/**
* For plugin
*/
public static final String PROP_CONF_DIR = "itoopie.confdir";
private static ConfigurationManager instance; private static ConfigurationManager instance;
@ -65,17 +69,18 @@ public class ConfigurationManager {
* Reads configuration from file itoopie.conf, every line is parsed as key=value. * Reads configuration from file itoopie.conf, every line is parsed as key=value.
*/ */
public static void readConfFile(){ public static void readConfFile(){
File f = new File(getAppConfDir(), DEFAULT_CONFIG_NAME);
try { try {
BufferedReader br = new BufferedReader(new FileReader(getAppConfDir() + DEFAULT_CONFIG_NAME)); BufferedReader br = new BufferedReader(new FileReader(f));
String input; String input;
while ((input = br.readLine()) != null){ while ((input = br.readLine()) != null){
parseConfigStr(input); parseConfigStr(input);
} }
br.close(); br.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
_log.info("Unable to find config file, " + getAppConfDir() + DEFAULT_CONFIG_NAME); _log.info("Unable to find config file " + f);
} catch (IOException e) { } catch (IOException e) {
_log.error("Unable to read from config file, " + getAppConfDir() + DEFAULT_CONFIG_NAME); _log.error("Unable to read from config file " + f);
} }
} }
@ -93,14 +98,15 @@ public class ConfigurationManager {
for (Entry<String,Boolean> e : booleanConfigurations.entrySet()){ for (Entry<String,Boolean> e : booleanConfigurations.entrySet()){
tree.put(e.getKey(), e.getValue().toString()); tree.put(e.getKey(), e.getValue().toString());
} }
File f = new File(getAppConfDir(), DEFAULT_CONFIG_NAME);
try { try {
BufferedWriter bw = new BufferedWriter(new FileWriter(getAppConfDir() + DEFAULT_CONFIG_NAME)); BufferedWriter bw = new BufferedWriter(new FileWriter(f));
for (Entry<String,String> e : tree.entrySet()){ for (Entry<String,String> e : tree.entrySet()){
bw.write(e.getKey() + "=" + e.getValue() + "\r\n"); bw.write(e.getKey() + "=" + e.getValue() + "\r\n");
} }
bw.close(); bw.close();
} catch (IOException e1) { } catch (IOException e1) {
_log.error("Couldn't open file, " + getAppConfDir() + DEFAULT_CONFIG_NAME + " for writing config."); _log.error("Couldn't open file " + f + " for writing config.");
} }
} }
@ -213,17 +219,21 @@ public class ConfigurationManager {
* Get the file path to the configuration directory. If the directory does not yet exist, creates it. * Get the file path to the configuration directory. If the directory does not yet exist, creates it.
* @return Application configuration directory. * @return Application configuration directory.
*/ */
public static String getAppConfDir(){ public static File getAppConfDir() {
String dir; String dir;
if (System.getenv("APPDATA") != null && !System.getenv("APPDATA").equals("")){ // for plugin
dir = System.getenv("APPDATA")+ File.separator + APP_DIR_NAME + File.separator; // Windows path String override = System.getProperty(PROP_CONF_DIR);
if (override != null) {
dir = override;
} else if (System.getenv("APPDATA") != null && !System.getenv("APPDATA").equals("")) {
dir = System.getenv("APPDATA")+ File.separator + APP_DIR_NAME; // Windows path
} else { } else {
dir = System.getProperties().getProperty("user.home") + File.separator + "." + APP_DIR_NAME + File.separator; // Linux/mac path dir = System.getProperty("user.home") + File.separator + "." + APP_DIR_NAME; // Linux/mac path
} }
File dirFile = new File(dir); File dirFile = new File(dir);
if (!dirFile.exists()){ if (!dirFile.exists()){
dirFile.mkdirs(); dirFile.mkdirs();
} }
return dir; return dirFile;
} }
} }

View File

@ -162,12 +162,13 @@ public class CertificateManager {
if (_ks == null){ if (_ks == null){
try { try {
_ks = KeyStore.getInstance(DEFAULT_KEYSTORE_TYPE); _ks = KeyStore.getInstance(DEFAULT_KEYSTORE_TYPE);
if ((new File(ConfigurationManager.getAppConfDir() + DEFAULT_KEYSTORE_LOCATION)).exists()){ File f = new File(ConfigurationManager.getAppConfDir(), DEFAULT_KEYSTORE_LOCATION);
InputStream is = new FileInputStream(ConfigurationManager.getAppConfDir() + DEFAULT_KEYSTORE_LOCATION); if (f.exists()) {
InputStream is = new FileInputStream(f);
_ks.load(is, DEFAULT_KEYSTORE_PASSWORD.toCharArray()); _ks.load(is, DEFAULT_KEYSTORE_PASSWORD.toCharArray());
return _ks; return _ks;
} else { } else {
throw new IOException("KeyStore file " + ConfigurationManager.getAppConfDir() + DEFAULT_KEYSTORE_LOCATION + "wasn't readable"); throw new IOException("KeyStore file " + f + " wasn't readable");
} }
} catch (Exception e) { } catch (Exception e) {
// Ignore. Not an issue. Let's just create a new keystore instead. // Ignore. Not an issue. Let's just create a new keystore instead.
@ -188,7 +189,7 @@ public class CertificateManager {
private static void saveKeyStore(KeyStore ks){ private static void saveKeyStore(KeyStore ks){
try { try {
ks.store(new FileOutputStream(ConfigurationManager.getAppConfDir() + DEFAULT_KEYSTORE_LOCATION), DEFAULT_KEYSTORE_PASSWORD.toCharArray()); ks.store(new FileOutputStream(new File(ConfigurationManager.getAppConfDir(), DEFAULT_KEYSTORE_LOCATION)), DEFAULT_KEYSTORE_PASSWORD.toCharArray());
} catch (KeyStoreException e) { } catch (KeyStoreException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();