/**
* MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface.
* Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
/******************************************************************************\
*
* This product was developed by
*
* SERVIÇO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO),
*
* a government company established under Brazilian law (5.615/70),
* at Department of Development of Porto Alegre.
*
\******************************************************************************/
package serpro.mailarchiver.util;
import java.io.File;
import java.io.FileReader;
import java.io.FilterReader;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import org.springframework.core.io.ClassPathResource;
public final class UserAppConfig {
private UserAppConfig() {}
private static class UserAppConfigHolder {
public static final UserAppConfig INSTANCE = new UserAppConfig();
}
public static UserAppConfig getInstance() {
return UserAppConfigHolder.INSTANCE;
}
private Ini ini = new Ini();
private class Ini extends org.ini4j.Ini {
public void load(Path iniPath) throws IOException {
File iniFile = iniPath.toFile();
setFile(iniFile);
super.load(new FilterReader(new FileReader(iniFile)) {
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
int c = in.read();
if(c == -1) {
return -1;
}
if(c == '\\') {
cbuf[off] = '\\';
cbuf[off + 1] = '\\';
return 2;
}
else {
cbuf[off] = (char) c;
return 1;
}
}
});
}
}
public Path getConfigIniPath() {
if(ini.getFile() != null) {
return ini.getFile().toPath();
}
return null;
}
private String parentResource(String s, int n) {
if(n > 0) {
return parentResource(s.substring(0, s.lastIndexOf('/')), n-1);
}
else if(s.endsWith("/classes")) {
return parentResource(s.substring(0, s.lastIndexOf('/')), n);
}
else if(s.endsWith("/build")) {
return parentResource(s.substring(0, s.lastIndexOf('/')), n);
}
else if(s.endsWith("/bin")) {
return parentResource(s.substring(0, s.lastIndexOf('/')), n);
}
else if(s.endsWith("/out")) {
return parentResource(s.substring(0, s.lastIndexOf('/')), n);
}
else {
return s;
}
}
//
public void load() throws IOException {
Path configIni = null;
Environment env = Environment.getInstance();
switch(env.getOperatingSystem()) {
case Windows7:
case WindowsVista:
case WindowsXP:
Path localAppDataPath = env.getLocalAppDataPath();
if(localAppDataPath != null) {
configIni = localAppDataPath
.resolve("MailArchiver")
.resolve("config.ini");
}
break;
case Linux:
Path homePath = env.getHomePath();
if(homePath != null) {
configIni = homePath
.resolve(".MailArchiver")
.resolve("config.ini");
}
break;
case MacOSX:
//TODO: pesquisar
}
if(configIni != null) {
System.out.print("Check for " + configIni.toString() + "... ");
if(Files.exists(configIni) && Files.isRegularFile(configIni)) {
ini.load(configIni);
System.out.println("exists!");
return;
}
else {
System.out.println("not exists");
}
}
String s = new ClassPathResource("/serpro/mailarchiver").getURI().toString();
if(s.startsWith("jar:")) {
s = parentResource(s.substring(4), 3);
}
else {
s = parentResource(s, 2);
}
try {
configIni = Paths.get(new URI(s)).resolve("etc").resolve("config.ini");
}
catch(URISyntaxException e) {
throw new IOException(e);
}
System.out.print("Check for " + configIni.toString() + "... ");
if(Files.exists(configIni) && Files.isRegularFile(configIni)) {
ini.load(configIni);
System.out.println("exists!");
}
else {
System.out.println("not exists");
}
}
//
public final Server SERVER = new Server();
public final class Server {
//
public int getPort() {
Ini.Section server = ini.get("Server");
if(server == null) {
return getDefaultPort();
}
String portStr = server.fetch("port");
if(portStr == null) {
return getDefaultPort();
}
return Integer.parseInt(portStr);
}
private int getDefaultPort() {
return 4333;
}
//
//
public int getConfidentialPort() {
Ini.Section server = ini.get("Server");
if(server == null) {
return getDefaultConfidentialPort();
}
String confidentialPortStr = server.fetch("confidentialPort");
if(confidentialPortStr == null) {
return getDefaultConfidentialPort();
}
return Integer.parseInt(confidentialPortStr);
}
private int getDefaultConfidentialPort() {
return 4334;
}
//
//
public Path getArchiveDir() {
Ini.Section server = ini.get("Server");
if(server == null) {
return getDefaultArchiveDir();
}
String archiveDir = server.fetch("archiveDir");
if(archiveDir == null) {
return getDefaultArchiveDir();
}
Path p = Paths.get(archiveDir);
if(p.isAbsolute()) {
return p;
}
return ini.getFile().toPath().resolveSibling(p);
}
private Path getDefaultArchiveDir() {
return ini.getFile().toPath().resolveSibling("archive");
}
//
}
public final MetaArchive META_ARCHIVE = new MetaArchive();
public final class MetaArchive {
//
public int getTcpPort() {
Ini.Section metaArchive = ini.get("MetaArchive");
if(metaArchive == null) {
return getDefaultTcpPort();
}
String tcpPortStr = metaArchive.fetch("tcpPort");
if(tcpPortStr == null) {
return getDefaultTcpPort();
}
return Integer.parseInt(tcpPortStr);
}
private int getDefaultTcpPort() {
return 4339;
}
//
//
public boolean getTcpAllowOthers() {
Ini.Section metaArchive = ini.get("MetaArchive");
if(metaArchive == null) {
return getDefaultTcpAllowOthers();
}
String tcpAllowOthersStr = metaArchive.fetch("tcpAllowOthers");
if(tcpAllowOthersStr == null) {
return getDefaultTcpAllowOthers();
}
return Boolean.parseBoolean(tcpAllowOthersStr);
}
private boolean getDefaultTcpAllowOthers() {
return false;
}
//
//
public int getWebPort() {
Ini.Section metaArchive = ini.get("MetaArchive");
if(metaArchive == null) {
return getDefaultWebPort();
}
String webPortStr = metaArchive.fetch("webPort");
if(webPortStr == null) {
return getDefaultWebPort();
}
return Integer.parseInt(webPortStr);
}
private int getDefaultWebPort() {
return 4335;
}
//
//
public boolean getWebAllowOthers() {
Ini.Section metaArchive = ini.get("MetaArchive");
if(metaArchive == null) {
return getDefaultWebAllowOthers();
}
String webAllowOthersStr = metaArchive.fetch("webAllowOthers");
if(webAllowOthersStr == null) {
return getDefaultWebAllowOthers();
}
return Boolean.parseBoolean(webAllowOthersStr);
}
private boolean getDefaultWebAllowOthers() {
return false;
}
//
}
public final Authentication AUTHENTICATION = new Authentication();
public final class Authentication {
//
public String[] getUrl() {
Ini.Section authentication = ini.get("Authentication");
if(authentication == null) {
return getDefaultUrl();
}
return authentication.fetchAll("url", String[].class);
}
private String[] getDefaultUrl() {
return new String[]{};
}
//
}
public final Log LOG = new Log();
public final class Log {
//
public Path getFile() {
Ini.Section log = ini.get("Log");
if(log == null) {
return getDefaultFile();
}
String file = log.fetch("file");
if(file == null) {
return getDefaultFile();
}
Path f = Paths.get(file);
if(f.isAbsolute()) {
return f;
}
return ini.getFile().toPath().resolveSibling(f);
}
private Path getDefaultFile() {
String fileNamePattern = "mail_archiver_%d{yyyy_MM}.log.zip";
// String fileNamePattern = "mail_archiver_%d{yyyy_MM_dd}.log.zip";
// String fileNamePattern = "mail_archiver_%d{yyyy_ww}.log.zip";
return ini.getFile().toPath().resolveSibling(fileNamePattern);
}
//
//
public String getLayout() {
Ini.Section log = ini.get("Log");
if(log == null) {
return getDefaultLayout();
}
String layout = log.get("layout");
if(layout == null) {
return getDefaultLayout();
}
return layout;
}
private String getDefaultLayout() {
return
"****%p**** {%c} %d{dd/MM/yy HH.mm.ss} [%t] %l%n" + //priority, category, timestamp, thread, location
"%m%n%n"; //message;
}
//
//
public String getSocketHost() {
Ini.Section log = ini.get("Log");
if(log == null) {
return getDefaultSocketHost();
}
String socketHost = log.fetch("socketHost");
if(socketHost == null) {
return getDefaultSocketHost();
}
return socketHost;
}
private String getDefaultSocketHost() {
//por default, não loga em socket
return "";
}
//
//
public int getSocketPort() {
Ini.Section log = ini.get("Log");
if(log == null) {
return getDefaultSocketPort();
}
String socketPortStr = log.fetch("socketPort");
if(socketPortStr == null) {
return getDefaultSocketPort();
}
return Integer.parseInt(socketPortStr);
}
private int getDefaultSocketPort() {
return
4560; //Chainsaw
// 4447; //LogView4J
}
//
//
public Map getLoggers() {
Map loggers = ini.get("Loggers");
if(loggers == null) {
return getDefaultLoggers();
}
if(!loggers.containsKey("root")) {
loggers.put("root", "INFO");
}
return loggers;
}
private Map getDefaultLoggers() {
Map loggers = new HashMap();
loggers.put("root", "INFO");
return loggers;
}
//
}
}