2006-12-14 10:14:18 -05:00
|
|
|
package davmail;
|
|
|
|
|
2007-10-09 08:01:58 -04:00
|
|
|
import davmail.tray.DavGatewayTray;
|
|
|
|
|
2006-12-14 10:14:18 -05:00
|
|
|
import java.util.Properties;
|
2007-10-20 14:33:31 -04:00
|
|
|
import java.io.*;
|
2006-12-14 10:14:18 -05:00
|
|
|
|
2008-11-04 08:48:38 -05:00
|
|
|
import org.apache.log4j.Level;
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
|
2006-12-14 10:14:18 -05:00
|
|
|
/**
|
|
|
|
* Settings facade
|
|
|
|
*/
|
|
|
|
public class Settings {
|
2007-05-09 19:37:24 -04:00
|
|
|
protected Settings() {
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final Properties SETTINGS = new Properties();
|
|
|
|
private static String configFilePath;
|
2007-08-02 09:34:10 -04:00
|
|
|
private static boolean isFirstStart;
|
2006-12-14 10:14:18 -05:00
|
|
|
|
|
|
|
public static synchronized void setConfigFilePath(String value) {
|
|
|
|
configFilePath = value;
|
|
|
|
}
|
2007-11-08 12:12:36 -05:00
|
|
|
|
2007-08-02 09:34:10 -04:00
|
|
|
public static boolean isFirstStart() {
|
|
|
|
return isFirstStart;
|
|
|
|
}
|
2006-12-14 10:14:18 -05:00
|
|
|
|
2008-02-14 10:41:02 -05:00
|
|
|
public static synchronized void load(InputStream inputStream) throws IOException {
|
|
|
|
SETTINGS.load(inputStream);
|
|
|
|
}
|
|
|
|
|
2006-12-14 10:14:18 -05:00
|
|
|
public static synchronized void load() {
|
2007-10-20 14:33:31 -04:00
|
|
|
FileInputStream fileInputStream = null;
|
2006-12-14 10:14:18 -05:00
|
|
|
try {
|
2006-12-15 11:44:31 -05:00
|
|
|
if (configFilePath == null) {
|
2007-05-09 19:37:24 -04:00
|
|
|
//noinspection AccessOfSystemProperties
|
2006-12-15 11:44:31 -05:00
|
|
|
configFilePath = System.getProperty("user.home") + "/.davmail.properties";
|
|
|
|
}
|
2006-12-14 10:14:18 -05:00
|
|
|
File configFile = new File(configFilePath);
|
|
|
|
if (configFile.exists()) {
|
2007-10-20 14:33:31 -04:00
|
|
|
fileInputStream = new FileInputStream(configFile);
|
2008-02-14 10:41:02 -05:00
|
|
|
load(fileInputStream);
|
2006-12-14 10:14:18 -05:00
|
|
|
} else {
|
2007-08-02 09:34:10 -04:00
|
|
|
isFirstStart = true;
|
|
|
|
|
|
|
|
// first start : set default values, ports above 1024 for linux
|
2007-05-09 19:37:24 -04:00
|
|
|
SETTINGS.put("davmail.url", "http://exchangeServer/exchange/");
|
2007-08-02 09:34:10 -04:00
|
|
|
SETTINGS.put("davmail.popPort", "1110");
|
|
|
|
SETTINGS.put("davmail.smtpPort", "1025");
|
2008-11-26 19:56:28 -05:00
|
|
|
SETTINGS.put("davmail.caldavPort", "1080");
|
2008-12-05 06:04:37 -05:00
|
|
|
SETTINGS.put("davmail.ldapPort", "1389");
|
2007-05-09 19:37:24 -04:00
|
|
|
SETTINGS.put("davmail.keepDelay", "30");
|
2008-12-05 05:01:24 -05:00
|
|
|
SETTINGS.put("davmail.sentKeepDelay", "90");
|
2008-12-05 05:45:23 -05:00
|
|
|
SETTINGS.put("davmail.caldavPastDelay", "90");
|
2007-09-25 18:30:11 -04:00
|
|
|
SETTINGS.put("davmail.allowRemote", "false");
|
2008-10-09 19:29:45 -04:00
|
|
|
SETTINGS.put("davmail.bindAddress", "");
|
2007-05-09 19:37:24 -04:00
|
|
|
SETTINGS.put("davmail.enableProxy", "false");
|
|
|
|
SETTINGS.put("davmail.proxyHost", "");
|
|
|
|
SETTINGS.put("davmail.proxyPort", "");
|
|
|
|
SETTINGS.put("davmail.proxyUser", "");
|
|
|
|
SETTINGS.put("davmail.proxyPassword", "");
|
2008-02-14 10:41:02 -05:00
|
|
|
SETTINGS.put("davmail.server", "false");
|
2008-10-31 13:12:30 -04:00
|
|
|
SETTINGS.put("davmail.server.certificate.hash", "");
|
2008-11-04 08:48:38 -05:00
|
|
|
|
|
|
|
// logging
|
|
|
|
SETTINGS.put("log4j.rootLogger", "WARN");
|
|
|
|
SETTINGS.put("log4j.logger.davmail", "DEBUG");
|
|
|
|
SETTINGS.put("log4j.logger.httpclient.wire", "WARN");
|
|
|
|
SETTINGS.put("log4j.logger.org.apache.commons.httpclient", "WARN");
|
2006-12-14 10:14:18 -05:00
|
|
|
save();
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
DavGatewayTray.error("Unable to load settings: ", e);
|
2007-05-09 19:37:24 -04:00
|
|
|
} finally {
|
2007-10-20 14:33:31 -04:00
|
|
|
if (fileInputStream != null) {
|
2007-05-09 19:37:24 -04:00
|
|
|
try {
|
2007-10-20 14:33:31 -04:00
|
|
|
fileInputStream.close();
|
2007-05-09 19:37:24 -04:00
|
|
|
} catch (IOException e) {
|
|
|
|
DavGatewayTray.debug("Error closing configuration file: ", e);
|
|
|
|
}
|
|
|
|
}
|
2006-12-14 10:14:18 -05:00
|
|
|
}
|
2008-11-04 08:48:38 -05:00
|
|
|
// update logging levels
|
|
|
|
Settings.setLoggingLevel("rootLogger", Settings.getLoggingLevel("rootLogger"));
|
|
|
|
Settings.setLoggingLevel("davmail", Settings.getLoggingLevel("davmail"));
|
|
|
|
Settings.setLoggingLevel("httpclient.wire", Settings.getLoggingLevel("httpclient.wire"));
|
|
|
|
Settings.setLoggingLevel("org.apache.commons.httpclient", Settings.getLoggingLevel("org.apache.commons.httpclient"));
|
2006-12-14 10:14:18 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static synchronized void save() {
|
2008-01-09 11:31:21 -05:00
|
|
|
FileOutputStream fileOutputStream = null;
|
2006-12-14 10:14:18 -05:00
|
|
|
try {
|
2008-01-09 11:31:21 -05:00
|
|
|
fileOutputStream = new FileOutputStream(configFilePath);
|
|
|
|
SETTINGS.store(fileOutputStream, "DavMail settings");
|
2006-12-14 10:14:18 -05:00
|
|
|
} catch (IOException e) {
|
|
|
|
DavGatewayTray.error("Unable to store settings: ", e);
|
2007-05-09 19:37:24 -04:00
|
|
|
} finally {
|
2008-01-09 11:31:21 -05:00
|
|
|
if (fileOutputStream != null) {
|
2007-05-09 19:37:24 -04:00
|
|
|
try {
|
2008-01-09 11:31:21 -05:00
|
|
|
fileOutputStream.close();
|
2007-05-09 19:37:24 -04:00
|
|
|
} catch (IOException e) {
|
|
|
|
DavGatewayTray.debug("Error closing configuration file: ", e);
|
|
|
|
}
|
|
|
|
}
|
2006-12-14 10:14:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static synchronized String getProperty(String property) {
|
2007-05-09 19:37:24 -04:00
|
|
|
return SETTINGS.getProperty(property);
|
2006-12-14 10:14:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public static synchronized void setProperty(String property, String value) {
|
2007-05-09 19:37:24 -04:00
|
|
|
SETTINGS.setProperty(property, value);
|
2006-12-14 10:14:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public static synchronized int getIntProperty(String property) {
|
2008-12-05 05:45:23 -05:00
|
|
|
return getIntProperty(property, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static synchronized int getIntProperty(String property, int defaultValue) {
|
|
|
|
int value = defaultValue;
|
2006-12-14 10:14:18 -05:00
|
|
|
try {
|
2007-05-09 19:37:24 -04:00
|
|
|
String propertyValue = SETTINGS.getProperty(property);
|
2006-12-14 10:14:18 -05:00
|
|
|
if (propertyValue != null && propertyValue.length() > 0) {
|
2008-01-31 18:42:04 -05:00
|
|
|
value = Integer.parseInt(propertyValue);
|
2006-12-14 10:14:18 -05:00
|
|
|
}
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
DavGatewayTray.error("Invalid setting value in " + property, e);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2007-09-25 18:30:11 -04:00
|
|
|
|
|
|
|
public static synchronized boolean getBooleanProperty(String property) {
|
2007-11-08 12:12:36 -05:00
|
|
|
String propertyValue = SETTINGS.getProperty(property);
|
|
|
|
return "true".equals(propertyValue);
|
2007-09-25 18:30:11 -04:00
|
|
|
}
|
2008-10-31 13:12:30 -04:00
|
|
|
|
2008-11-04 08:48:38 -05:00
|
|
|
protected static String getLoggingPrefix(String category) {
|
|
|
|
String prefix;
|
|
|
|
if ("rootLogger".equals(category)) {
|
|
|
|
prefix = "log4j.";
|
|
|
|
} else {
|
|
|
|
prefix = "log4j.logger.";
|
|
|
|
}
|
|
|
|
return prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static synchronized Level getLoggingLevel(String category) {
|
|
|
|
String prefix = getLoggingPrefix(category);
|
|
|
|
String currentValue = SETTINGS.getProperty(prefix + category);
|
|
|
|
|
|
|
|
if (currentValue != null && currentValue.length() > 0) {
|
|
|
|
return Level.toLevel(currentValue);
|
|
|
|
} else if ("rootLogger".equals(category)) {
|
|
|
|
return Logger.getRootLogger().getLevel();
|
|
|
|
} else {
|
|
|
|
return Logger.getLogger(category).getLevel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static synchronized void setLoggingLevel(String category, Level level) {
|
|
|
|
String prefix = getLoggingPrefix(category);
|
|
|
|
SETTINGS.setProperty(prefix + category, level.toString());
|
|
|
|
if ("rootLogger".equals(category)) {
|
|
|
|
Logger.getRootLogger().setLevel(level);
|
|
|
|
} else {
|
|
|
|
Logger.getLogger(category).setLevel(level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-31 13:12:30 -04:00
|
|
|
public static synchronized void saveProperty(String property, String value) {
|
|
|
|
Settings.load();
|
|
|
|
Settings.setProperty(property, value);
|
|
|
|
Settings.save();
|
|
|
|
}
|
|
|
|
|
2006-12-14 10:14:18 -05:00
|
|
|
}
|