-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAppConfig.java
More file actions
executable file
·99 lines (83 loc) · 3.37 KB
/
AppConfig.java
File metadata and controls
executable file
·99 lines (83 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright 2018-2021 Yusef Badri - All rights reserved.
* Mailismus is distributed under the terms of the GNU Affero General Public License, Version 3 (AGPLv3).
*/
/*
* This class represents the Mailismus config file as a whole, with a special emphasis on the sections
* that are not defined within the config block of any one task (eg. Queue, Directory, etc).
* Although it is possible for naf.xml Naflets to nominate differing config files, we expect all Mailismus
* Naflets to specify the same application config file.
*/
package com.grey.mailismus;
import com.grey.base.config.XmlConfig;
import com.grey.naf.ApplicationContextNAF;
import com.grey.naf.errors.NAFConfigException;
import com.grey.naf.reactor.Dispatcher;
public final class AppConfig
{
public static final String TOKEN_PRODNAME = "%PRODNAME%";
private static final String TOKEN_HOSTNAME = "%SYSNAME%";
private static final String XPATH_MTA = "mta"; //relative to configRoot
private static final String XPATH_MTA_QUEUE = XPATH_MTA+"/queue";
private static final String XPATH_MTA_RELAYS = XPATH_MTA+"/deliver/relays";
private final String hostName;
private final String announceHost;
private final String productName;
private final DBHandle.Type dbType;
private final XmlConfig configRoot;
public final String getProductName() {return productName;}
public final String getAnnounceHost() {return announceHost;}
public final DBHandle.Type getDatabaseType() {return dbType;}
public static AppConfig get(String cfgpath, Dispatcher d) {
ApplicationContextNAF appctx = d.getApplicationContext();
return appctx.getNamedItem(AppConfig.class.getName(), () -> {
try {
return new AppConfig(cfgpath, d);
} catch (Exception ex) {
throw new NAFConfigException("Failed to create AppConfig with Dispatcher="+d.getName(), ex);
}
});
}
private AppConfig(String cfgpath, Dispatcher dsptch) throws java.io.IOException
{
hostName = java.net.InetAddress.getLocalHost().getCanonicalHostName();
configRoot = (cfgpath == null || cfgpath.isEmpty() ? XmlConfig.BLANKCFG : XmlConfig.getSection(cfgpath, "mailserver"));
XmlConfig cfg = configRoot.getSection("application");
productName = cfg.getValue("prodname", true, "Mailismus");
announceHost = getAnnounceHost(cfg, hostName);
XmlConfig dbcfg = cfg.getSection("database"+XmlConfig.XPATH_ENABLED);
dbType = (dbcfg.exists() ? new DBHandle.Type(dbcfg, dsptch.getApplicationContext().getNafConfig(), dsptch.getLogger()) : null);
}
public XmlConfig getConfigQueue(String name)
{
XmlConfig cfg = null;
if (name != null) {
cfg = configRoot.getSection(XPATH_MTA_QUEUE+"_"+name);
if (!cfg.exists()) cfg = null;
}
if (cfg == null) cfg = configRoot.getSection(XPATH_MTA_QUEUE);
return cfg;
}
public XmlConfig getConfigDirectory()
{
return configRoot.getSection("directory"+XmlConfig.XPATH_ENABLED);
}
public XmlConfig getConfigMS()
{
return configRoot.getSection("message_store"+XmlConfig.XPATH_ENABLED);
}
public XmlConfig getConfigRelays()
{
return configRoot.getSection(XPATH_MTA_RELAYS);
}
public String getAnnounceHost(XmlConfig cfg, String dflt)
{
return parseHost(cfg, "announcehost", false, dflt);
}
public String parseHost(XmlConfig cfg, String xpath, boolean mdty, String dflt)
{
String name = (xpath == null ? dflt : cfg.getValue(xpath, mdty, dflt));
if (name != null) name = name.replace(TOKEN_HOSTNAME, hostName);
return name;
}
}