ClientConfigValidator.java
2.01 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
package com.dianping.cat.configuration;
import java.text.MessageFormat;
import java.util.Date;
import com.dianping.cat.configuration.client.entity.ClientConfig;
import com.dianping.cat.configuration.client.entity.Domain;
import com.dianping.cat.configuration.client.entity.Server;
import com.dianping.cat.configuration.client.transform.DefaultValidator;
public class ClientConfigValidator extends DefaultValidator {
private ClientConfig m_config;
private String getLocalAddress() {
return NetworkInterfaceManager.INSTANCE.getLocalHostAddress();
}
private void log(String severity, String message) {
MessageFormat format = new MessageFormat("[{0,date,MM-dd HH:mm:ss.sss}] [{1}] [{2}] {3}");
System.out.println(format.format(new Object[] { new Date(), severity, "cat", message }));
}
@Override
public void visitConfig(ClientConfig config) {
config.setMode("client");
if (config.getServers().size() == 0) {
config.setEnabled(false);
log("WARN", "CAT client was disabled due to no CAT servers configured!");
} else if (!config.isEnabled()) {
log("WARN", "CAT client was globally disabled!");
}
m_config = config;
super.visitConfig(config);
if (m_config.isEnabled()) {
for (Domain domain : m_config.getDomains().values()) {
if (!domain.isEnabled()) {
m_config.setEnabled(false);
log("WARN", "CAT client was disabled in domain(" + domain.getId() + ") explicitly!");
}
break; // for first domain only
}
}
}
@Override
public void visitDomain(Domain domain) {
super.visitDomain(domain);
// set default values
if (domain.getEnabled() == null) {
domain.setEnabled(true);
}
if (domain.getIp() == null) {
domain.setIp(getLocalAddress());
}
}
@Override
public void visitServer(Server server) {
super.visitServer(server);
// set default values
if (server.getPort() == null) {
server.setPort(2280);
}
if (server.getEnabled() == null) {
server.setEnabled(true);
}
}
}