[6785] | 1 | /** |
---|
| 2 | * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface. |
---|
| 3 | * Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior |
---|
| 4 | * |
---|
| 5 | * This program is free software: you can redistribute it and/or modify |
---|
| 6 | * it under the terms of the GNU Affero General Public License as |
---|
| 7 | * published by the Free Software Foundation, either version 3 of the |
---|
| 8 | * License, or (at your option) any later version. |
---|
| 9 | * |
---|
| 10 | * This program is distributed in the hope that it will be useful, |
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU Affero General Public License for more details. |
---|
| 14 | * |
---|
| 15 | * You should have received a copy of the GNU Affero General Public License |
---|
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | /******************************************************************************\ |
---|
| 20 | * |
---|
| 21 | * This product was developed by |
---|
| 22 | * |
---|
| 23 | * SERVIÃO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO), |
---|
| 24 | * |
---|
| 25 | * a government company established under Brazilian law (5.615/70), |
---|
| 26 | * at Department of Development of Porto Alegre. |
---|
| 27 | * |
---|
| 28 | \******************************************************************************/ |
---|
| 29 | |
---|
| 30 | package serpro.mailarchiver.util; |
---|
| 31 | |
---|
| 32 | import java.io.File; |
---|
| 33 | import java.io.FileReader; |
---|
| 34 | import java.io.FilterReader; |
---|
| 35 | import java.io.IOException; |
---|
| 36 | import java.net.URI; |
---|
| 37 | import java.net.URISyntaxException; |
---|
| 38 | import java.nio.file.Files; |
---|
| 39 | import java.nio.file.Path; |
---|
| 40 | import java.nio.file.Paths; |
---|
| 41 | import java.util.HashMap; |
---|
| 42 | import java.util.Map; |
---|
| 43 | |
---|
| 44 | import org.springframework.core.io.ClassPathResource; |
---|
| 45 | |
---|
| 46 | public final class UserAppConfig { |
---|
| 47 | |
---|
| 48 | private UserAppConfig() {} |
---|
| 49 | |
---|
| 50 | private static class UserAppConfigHolder { |
---|
| 51 | public static final UserAppConfig INSTANCE = new UserAppConfig(); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | public static UserAppConfig getInstance() { |
---|
| 55 | return UserAppConfigHolder.INSTANCE; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | private Ini ini = new Ini(); |
---|
| 59 | |
---|
| 60 | private class Ini extends org.ini4j.Ini { |
---|
| 61 | public void load(Path iniPath) throws IOException { |
---|
| 62 | File iniFile = iniPath.toFile(); |
---|
| 63 | setFile(iniFile); |
---|
| 64 | super.load(new FilterReader(new FileReader(iniFile)) { |
---|
| 65 | @Override |
---|
| 66 | public int read(char[] cbuf, int off, int len) throws IOException { |
---|
| 67 | int c = in.read(); |
---|
| 68 | if(c == -1) { |
---|
| 69 | return -1; |
---|
| 70 | } |
---|
| 71 | if(c == '\\') { |
---|
| 72 | cbuf[off] = '\\'; |
---|
| 73 | cbuf[off + 1] = '\\'; |
---|
| 74 | return 2; |
---|
| 75 | } |
---|
| 76 | else { |
---|
| 77 | cbuf[off] = (char) c; |
---|
| 78 | return 1; |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | }); |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | public Path getConfigIniPath() { |
---|
| 86 | if(ini.getFile() != null) { |
---|
| 87 | return ini.getFile().toPath(); |
---|
| 88 | } |
---|
| 89 | return null; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | private String parentResource(String s, int n) { |
---|
| 93 | if(n > 0) { |
---|
| 94 | return parentResource(s.substring(0, s.lastIndexOf('/')), n-1); |
---|
| 95 | } |
---|
| 96 | else if(s.endsWith("/classes")) { |
---|
| 97 | return parentResource(s.substring(0, s.lastIndexOf('/')), n); |
---|
| 98 | } |
---|
| 99 | else if(s.endsWith("/build")) { |
---|
| 100 | return parentResource(s.substring(0, s.lastIndexOf('/')), n); |
---|
| 101 | } |
---|
| 102 | else if(s.endsWith("/bin")) { |
---|
| 103 | return parentResource(s.substring(0, s.lastIndexOf('/')), n); |
---|
| 104 | } |
---|
| 105 | else if(s.endsWith("/out")) { |
---|
| 106 | return parentResource(s.substring(0, s.lastIndexOf('/')), n); |
---|
| 107 | } |
---|
| 108 | else { |
---|
| 109 | return s; |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | //<editor-fold defaultstate="collapsed" desc=" load "> |
---|
| 115 | public void load() throws IOException { |
---|
| 116 | Path configIni = null; |
---|
| 117 | Environment env = Environment.getInstance(); |
---|
| 118 | |
---|
| 119 | switch(env.getOperatingSystem()) { |
---|
| 120 | case Windows7: |
---|
| 121 | case WindowsVista: |
---|
| 122 | case WindowsXP: |
---|
| 123 | |
---|
| 124 | Path localAppDataPath = env.getLocalAppDataPath(); |
---|
| 125 | |
---|
| 126 | if(localAppDataPath != null) { |
---|
| 127 | configIni = localAppDataPath |
---|
| 128 | .resolve("MailArchiver") |
---|
| 129 | .resolve("config.ini"); |
---|
| 130 | } |
---|
| 131 | break; |
---|
| 132 | |
---|
| 133 | case Linux: |
---|
| 134 | |
---|
| 135 | Path homePath = env.getHomePath(); |
---|
| 136 | |
---|
| 137 | if(homePath != null) { |
---|
| 138 | configIni = homePath |
---|
| 139 | .resolve(".MailArchiver") |
---|
| 140 | .resolve("config.ini"); |
---|
| 141 | } |
---|
| 142 | break; |
---|
| 143 | |
---|
| 144 | case MacOSX: |
---|
| 145 | //TODO: pesquisar |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | if(configIni != null) { |
---|
| 149 | |
---|
| 150 | System.out.print("Check for " + configIni.toString() + "... "); |
---|
| 151 | |
---|
| 152 | if(Files.exists(configIni) && Files.isRegularFile(configIni)) { |
---|
| 153 | ini.load(configIni); |
---|
| 154 | |
---|
| 155 | System.out.println("exists!"); |
---|
| 156 | return; |
---|
| 157 | } |
---|
| 158 | else { |
---|
| 159 | System.out.println("not exists"); |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | String s = new ClassPathResource("/serpro/mailarchiver").getURI().toString(); |
---|
| 164 | if(s.startsWith("jar:")) { |
---|
| 165 | s = parentResource(s.substring(4), 3); |
---|
| 166 | } |
---|
| 167 | else { |
---|
| 168 | s = parentResource(s, 2); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | try { |
---|
| 172 | configIni = Paths.get(new URI(s)).resolve("etc").resolve("config.ini"); |
---|
| 173 | } |
---|
| 174 | catch(URISyntaxException e) { |
---|
| 175 | throw new IOException(e); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | System.out.print("Check for " + configIni.toString() + "... "); |
---|
| 179 | |
---|
| 180 | if(Files.exists(configIni) && Files.isRegularFile(configIni)) { |
---|
| 181 | ini.load(configIni); |
---|
| 182 | |
---|
| 183 | System.out.println("exists!"); |
---|
| 184 | } |
---|
| 185 | else { |
---|
| 186 | System.out.println("not exists"); |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | //</editor-fold> |
---|
| 190 | |
---|
| 191 | public final Server SERVER = new Server(); |
---|
| 192 | |
---|
| 193 | public final class Server { |
---|
| 194 | |
---|
| 195 | //<editor-fold defaultstate="collapsed" desc=" port "> |
---|
| 196 | public int getPort() { |
---|
| 197 | |
---|
| 198 | Ini.Section server = ini.get("Server"); |
---|
| 199 | if(server == null) { |
---|
| 200 | return getDefaultPort(); |
---|
| 201 | } |
---|
| 202 | String portStr = server.fetch("port"); |
---|
| 203 | if(portStr == null) { |
---|
| 204 | return getDefaultPort(); |
---|
| 205 | } |
---|
| 206 | return Integer.parseInt(portStr); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | private int getDefaultPort() { |
---|
| 210 | return 4333; |
---|
| 211 | } |
---|
| 212 | //</editor-fold> |
---|
| 213 | |
---|
| 214 | //<editor-fold defaultstate="collapsed" desc=" confidentialPort "> |
---|
| 215 | public int getConfidentialPort() { |
---|
| 216 | |
---|
| 217 | Ini.Section server = ini.get("Server"); |
---|
| 218 | if(server == null) { |
---|
| 219 | return getDefaultConfidentialPort(); |
---|
| 220 | } |
---|
| 221 | String confidentialPortStr = server.fetch("confidentialPort"); |
---|
| 222 | if(confidentialPortStr == null) { |
---|
| 223 | return getDefaultConfidentialPort(); |
---|
| 224 | } |
---|
| 225 | return Integer.parseInt(confidentialPortStr); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | private int getDefaultConfidentialPort() { |
---|
| 229 | return 4334; |
---|
| 230 | } |
---|
| 231 | //</editor-fold> |
---|
| 232 | |
---|
| 233 | //<editor-fold defaultstate="collapsed" desc=" archiveDir "> |
---|
| 234 | public Path getArchiveDir() { |
---|
| 235 | |
---|
| 236 | Ini.Section server = ini.get("Server"); |
---|
| 237 | if(server == null) { |
---|
| 238 | return getDefaultArchiveDir(); |
---|
| 239 | } |
---|
| 240 | String archiveDir = server.fetch("archiveDir"); |
---|
| 241 | if(archiveDir == null) { |
---|
| 242 | return getDefaultArchiveDir(); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | Path p = Paths.get(archiveDir); |
---|
| 246 | if(p.isAbsolute()) { |
---|
| 247 | return p; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | return ini.getFile().toPath().resolveSibling(p); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | private Path getDefaultArchiveDir() { |
---|
| 254 | return ini.getFile().toPath().resolveSibling("archive"); |
---|
| 255 | } |
---|
| 256 | //</editor-fold> |
---|
| 257 | |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | public final MetaArchive META_ARCHIVE = new MetaArchive(); |
---|
| 261 | |
---|
| 262 | public final class MetaArchive { |
---|
| 263 | |
---|
| 264 | //<editor-fold defaultstate="collapsed" desc=" tcpPort "> |
---|
| 265 | public int getTcpPort() { |
---|
| 266 | |
---|
| 267 | Ini.Section metaArchive = ini.get("MetaArchive"); |
---|
| 268 | if(metaArchive == null) { |
---|
| 269 | return getDefaultTcpPort(); |
---|
| 270 | } |
---|
| 271 | String tcpPortStr = metaArchive.fetch("tcpPort"); |
---|
| 272 | if(tcpPortStr == null) { |
---|
| 273 | return getDefaultTcpPort(); |
---|
| 274 | } |
---|
| 275 | return Integer.parseInt(tcpPortStr); |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | private int getDefaultTcpPort() { |
---|
| 279 | return 4339; |
---|
| 280 | } |
---|
| 281 | //</editor-fold> |
---|
| 282 | |
---|
| 283 | //<editor-fold defaultstate="collapsed" desc=" tcpAllowOthers "> |
---|
| 284 | public boolean getTcpAllowOthers() { |
---|
| 285 | |
---|
| 286 | Ini.Section metaArchive = ini.get("MetaArchive"); |
---|
| 287 | if(metaArchive == null) { |
---|
| 288 | return getDefaultTcpAllowOthers(); |
---|
| 289 | } |
---|
| 290 | String tcpAllowOthersStr = metaArchive.fetch("tcpAllowOthers"); |
---|
| 291 | if(tcpAllowOthersStr == null) { |
---|
| 292 | return getDefaultTcpAllowOthers(); |
---|
| 293 | } |
---|
| 294 | return Boolean.parseBoolean(tcpAllowOthersStr); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | private boolean getDefaultTcpAllowOthers() { |
---|
| 298 | return false; |
---|
| 299 | } |
---|
| 300 | //</editor-fold> |
---|
| 301 | |
---|
| 302 | //<editor-fold defaultstate="collapsed" desc=" webPort "> |
---|
| 303 | public int getWebPort() { |
---|
| 304 | |
---|
| 305 | Ini.Section metaArchive = ini.get("MetaArchive"); |
---|
| 306 | if(metaArchive == null) { |
---|
| 307 | return getDefaultWebPort(); |
---|
| 308 | } |
---|
| 309 | String webPortStr = metaArchive.fetch("webPort"); |
---|
| 310 | if(webPortStr == null) { |
---|
| 311 | return getDefaultWebPort(); |
---|
| 312 | } |
---|
| 313 | return Integer.parseInt(webPortStr); |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | private int getDefaultWebPort() { |
---|
| 317 | return 4335; |
---|
| 318 | } |
---|
| 319 | //</editor-fold> |
---|
| 320 | |
---|
| 321 | //<editor-fold defaultstate="collapsed" desc=" webAllowOthers "> |
---|
| 322 | public boolean getWebAllowOthers() { |
---|
| 323 | |
---|
| 324 | Ini.Section metaArchive = ini.get("MetaArchive"); |
---|
| 325 | if(metaArchive == null) { |
---|
| 326 | return getDefaultWebAllowOthers(); |
---|
| 327 | } |
---|
| 328 | String webAllowOthersStr = metaArchive.fetch("webAllowOthers"); |
---|
| 329 | if(webAllowOthersStr == null) { |
---|
| 330 | return getDefaultWebAllowOthers(); |
---|
| 331 | } |
---|
| 332 | return Boolean.parseBoolean(webAllowOthersStr); |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | private boolean getDefaultWebAllowOthers() { |
---|
| 336 | return false; |
---|
| 337 | } |
---|
| 338 | //</editor-fold> |
---|
| 339 | |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | public final Authentication AUTHENTICATION = new Authentication(); |
---|
| 343 | |
---|
| 344 | public final class Authentication { |
---|
| 345 | |
---|
| 346 | //<editor-fold defaultstate="collapsed" desc=" url "> |
---|
| 347 | public String[] getUrl() { |
---|
| 348 | |
---|
| 349 | Ini.Section authentication = ini.get("Authentication"); |
---|
| 350 | if(authentication == null) { |
---|
| 351 | return getDefaultUrl(); |
---|
| 352 | } |
---|
| 353 | return authentication.fetchAll("url", String[].class); |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | private String[] getDefaultUrl() { |
---|
| 357 | return new String[]{}; |
---|
| 358 | } |
---|
| 359 | //</editor-fold> |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | public final Log LOG = new Log(); |
---|
| 363 | |
---|
| 364 | public final class Log { |
---|
| 365 | |
---|
| 366 | //<editor-fold defaultstate="collapsed" desc=" file "> |
---|
| 367 | public Path getFile() { |
---|
| 368 | |
---|
| 369 | Ini.Section log = ini.get("Log"); |
---|
| 370 | if(log == null) { |
---|
| 371 | return getDefaultFile(); |
---|
| 372 | } |
---|
| 373 | String file = log.fetch("file"); |
---|
| 374 | if(file == null) { |
---|
| 375 | return getDefaultFile(); |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | Path f = Paths.get(file); |
---|
| 379 | if(f.isAbsolute()) { |
---|
| 380 | return f; |
---|
| 381 | } |
---|
| 382 | |
---|
| 383 | return ini.getFile().toPath().resolveSibling(f); |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | private Path getDefaultFile() { |
---|
| 387 | |
---|
| 388 | String fileNamePattern = "mail_archiver_%d{yyyy_MM}.log.zip"; |
---|
| 389 | // String fileNamePattern = "mail_archiver_%d{yyyy_MM_dd}.log.zip"; |
---|
| 390 | // String fileNamePattern = "mail_archiver_%d{yyyy_ww}.log.zip"; |
---|
| 391 | |
---|
| 392 | return ini.getFile().toPath().resolveSibling(fileNamePattern); |
---|
| 393 | } |
---|
| 394 | //</editor-fold> |
---|
| 395 | |
---|
| 396 | //<editor-fold defaultstate="collapsed" desc=" layout "> |
---|
| 397 | public String getLayout() { |
---|
| 398 | |
---|
| 399 | Ini.Section log = ini.get("Log"); |
---|
| 400 | if(log == null) { |
---|
| 401 | return getDefaultLayout(); |
---|
| 402 | } |
---|
| 403 | String layout = log.get("layout"); |
---|
| 404 | if(layout == null) { |
---|
| 405 | return getDefaultLayout(); |
---|
| 406 | } |
---|
| 407 | return layout; |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | private String getDefaultLayout() { |
---|
| 411 | return |
---|
| 412 | "****%p**** {%c} %d{dd/MM/yy HH.mm.ss} [%t] %l%n" + //priority, category, timestamp, thread, location |
---|
| 413 | "%m%n%n"; //message; |
---|
| 414 | } |
---|
| 415 | //</editor-fold> |
---|
| 416 | |
---|
| 417 | //<editor-fold defaultstate="collapsed" desc=" socketHost "> |
---|
| 418 | public String getSocketHost() { |
---|
| 419 | |
---|
| 420 | Ini.Section log = ini.get("Log"); |
---|
| 421 | if(log == null) { |
---|
| 422 | return getDefaultSocketHost(); |
---|
| 423 | } |
---|
| 424 | String socketHost = log.fetch("socketHost"); |
---|
| 425 | if(socketHost == null) { |
---|
| 426 | return getDefaultSocketHost(); |
---|
| 427 | } |
---|
| 428 | return socketHost; |
---|
| 429 | } |
---|
| 430 | |
---|
| 431 | private String getDefaultSocketHost() { |
---|
| 432 | //por default, não loga em socket |
---|
| 433 | return ""; |
---|
| 434 | } |
---|
| 435 | //</editor-fold> |
---|
| 436 | |
---|
| 437 | //<editor-fold defaultstate="collapsed" desc=" socketPort "> |
---|
| 438 | public int getSocketPort() { |
---|
| 439 | |
---|
| 440 | Ini.Section log = ini.get("Log"); |
---|
| 441 | if(log == null) { |
---|
| 442 | return getDefaultSocketPort(); |
---|
| 443 | } |
---|
| 444 | String socketPortStr = log.fetch("socketPort"); |
---|
| 445 | if(socketPortStr == null) { |
---|
| 446 | return getDefaultSocketPort(); |
---|
| 447 | } |
---|
| 448 | return Integer.parseInt(socketPortStr); |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | private int getDefaultSocketPort() { |
---|
| 452 | return |
---|
| 453 | 4560; //Chainsaw |
---|
| 454 | // 4447; //LogView4J |
---|
| 455 | } |
---|
| 456 | //</editor-fold> |
---|
| 457 | |
---|
| 458 | //<editor-fold defaultstate="collapsed" desc=" Loggers "> |
---|
| 459 | public Map<String,String> getLoggers() { |
---|
| 460 | |
---|
| 461 | Map<String,String> loggers = ini.get("Loggers"); |
---|
| 462 | if(loggers == null) { |
---|
| 463 | return getDefaultLoggers(); |
---|
| 464 | } |
---|
| 465 | if(!loggers.containsKey("root")) { |
---|
| 466 | loggers.put("root", "INFO"); |
---|
| 467 | } |
---|
| 468 | return loggers; |
---|
| 469 | } |
---|
| 470 | |
---|
| 471 | private Map<String,String> getDefaultLoggers() { |
---|
| 472 | Map<String,String> loggers = new HashMap<String,String>(); |
---|
| 473 | loggers.put("root", "INFO"); |
---|
| 474 | return loggers; |
---|
| 475 | } |
---|
| 476 | //</editor-fold> |
---|
| 477 | |
---|
| 478 | } |
---|
| 479 | } |
---|