DefaultClientConfigManager.java
5.74 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package com.dianping.cat.configuration;
import java.io.File;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.unidal.helper.Files;
import com.dianping.cat.Cat;
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.DefaultSaxParser;
public class DefaultClientConfigManager implements LogEnabled, ClientConfigManager, Initializable {
private static final String CAT_CLIENT_XML = "/META-INF/cat/client.xml";
private static final String PROPERTIES_CLIENT_XML = "/META-INF/app.properties";
private static final String XML = "/data/appdatas/cat/client.xml";
private Logger m_logger;
private ClientConfig m_config;
@Override
public void enableLogging(Logger logger) {
m_logger = logger;
}
@Override
public Domain getDomain() {
Domain domain = null;
if (m_config != null) {
Map<String, Domain> domains = m_config.getDomains();
domain = domains.isEmpty() ? null : domains.values().iterator().next();
}
if (domain != null) {
return domain;
} else {
return new Domain("UNKNOWN").setEnabled(false);
}
}
@Override
public int getMaxMessageLength() {
if (m_config == null) {
return 5000;
} else {
return getDomain().getMaxMessageSize();
}
}
@Override
public String getServerConfigUrl() {
if (m_config == null) {
return null;
} else {
List<Server> servers = m_config.getServers();
for (Server server : servers) {
Integer httpPort = server.getHttpPort();
if (httpPort == null || httpPort == 0) {
httpPort = 8080;
}
return String.format("http://%s:%d/cat/s/router?domain=%s&ip=%s&op=json", server.getIp().trim(), httpPort,
getDomain().getId(), NetworkInterfaceManager.INSTANCE.getLocalHostAddress());
}
}
return null;
}
@Override
public List<Server> getServers() {
if (m_config == null) {
return Collections.emptyList();
} else {
return m_config.getServers();
}
}
@Override
public int getTaggedTransactionCacheSize() {
return 1024;
}
@Override
public boolean isCatEnabled() {
if (m_config == null) {
return false;
} else {
return m_config.isEnabled();
}
}
@Override
public boolean isDumpLocked() {
if (m_config == null) {
return false;
} else {
return m_config.isDumpLocked();
}
}
private ClientConfig loadConfigFromEnviroment() {
String appName = loadProjectName();
if (appName != null) {
ClientConfig config = new ClientConfig();
config.addDomain(new Domain(appName));
return config;
}
return null;
}
private ClientConfig loadConfigFromXml() {
InputStream in = null;
try {
in = Thread.currentThread().getContextClassLoader().getResourceAsStream(CAT_CLIENT_XML);
if (in == null) {
in = Cat.class.getResourceAsStream(CAT_CLIENT_XML);
}
if (in != null) {
String xml = Files.forIO().readFrom(in, "utf-8");
m_logger.info(String.format("Resource file(%s) found.", Cat.class.getResource(CAT_CLIENT_XML)));
return DefaultSaxParser.parse(xml);
}
return null;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
}
}
}
return null;
}
private String loadProjectName() {
String appName = null;
InputStream in = null;
try {
in = Thread.currentThread().getContextClassLoader().getResourceAsStream(PROPERTIES_CLIENT_XML);
if (in == null) {
in = Cat.class.getResourceAsStream(PROPERTIES_CLIENT_XML);
}
if (in != null) {
Properties prop = new Properties();
prop.load(in);
appName = prop.getProperty("app.name");
if (appName != null) {
m_logger.info(String.format("Find domain name %s from app.properties.", appName));
} else {
m_logger.info(String.format("Can't find app.name from app.properties."));
return null;
}
} else {
m_logger.info(String.format("Can't find app.properties in %s", PROPERTIES_CLIENT_XML));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
}
}
}
return appName;
}
@Override
public void initialize() throws InitializationException {
File configFile = new File(XML);
initialize(configFile);
}
@Override
public void initialize(File configFile) throws InitializationException {
try {
ClientConfig globalConfig = null;
ClientConfig clientConfig = null;
if (configFile != null) {
if (configFile.exists()) {
String xml = Files.forIO().readFrom(configFile.getCanonicalFile(), "utf-8");
globalConfig = DefaultSaxParser.parse(xml);
m_logger.info(String.format("Global config file(%s) found.", configFile));
} else {
m_logger.warn(String.format("Global config file(%s) not found, IGNORED.", configFile));
}
}
// load the client configure from Java class-path
clientConfig = loadConfigFromEnviroment();
if (clientConfig == null) {
clientConfig = loadConfigFromXml();
}
// merge the two configures together to make it effected
if (globalConfig != null && clientConfig != null) {
globalConfig.accept(new ClientConfigMerger(clientConfig));
}
if (clientConfig != null) {
clientConfig.accept(new ClientConfigValidator());
}
m_config = clientConfig;
} catch (Exception e) {
throw new InitializationException(e.getMessage(), e);
}
}
}