forked from scouter-project/scouter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounterManager.java
More file actions
235 lines (216 loc) · 8.24 KB
/
CounterManager.java
File metadata and controls
235 lines (216 loc) · 8.24 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
/*
* Copyright 2015 the original author or authors.
* @https://github.com/scouter-project/scouter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package scouter.server;
import java.io.File;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import scouter.lang.ObjectType;
import scouter.lang.counters.CounterEngine;
import scouter.lang.pack.MapPack;
import scouter.server.util.XmlUtil;
import scouter.util.FileUtil;
public class CounterManager {
private static final String CUSTOM_FILENAME = "counters.site.xml";
private static volatile CounterManager instance;
CounterEngine engine = new CounterEngine();
File customFile;
byte[] xmlContent;
byte[] xmlCustomContent;
public static CounterManager getInstance() {
if (instance == null) {
synchronized (CounterManager.class) {
if (instance == null) {
instance = new CounterManager();
}
}
}
return instance;
}
private CounterManager() {
init();
}
private void init() {
readAndParseXml("/scouter/lang/counters/counters.xml");
customFile = new File(Configure.CONF_DIR + CUSTOM_FILENAME);
if (customFile.canRead()) {
xmlCustomContent = FileUtil.readAll(customFile);
engine.parse(xmlCustomContent);
}
}
private void readAndParseXml(String path) {
InputStream in = CounterEngine.class.getResourceAsStream(path);
try {
xmlContent = FileUtil.readAll(in);
engine.parse(xmlContent);
} catch (Exception e) {
Logger.println("Failed read " + path);
} finally {
FileUtil.close(in);
}
}
public CounterEngine getCounterEngine() {
return engine;
}
public byte[] getXmlContent() {
return xmlContent;
}
public byte[] getXmlCustomContent() {
return xmlCustomContent;
}
public synchronized boolean addObjectType(MapPack param) {
String name = param.getText(CounterEngine.ATTR_NAME);
if (engine.getObjectType(name) != null) {
return false;
}
String displayName = param.getText(CounterEngine.ATTR_DISPLAY);
String family = param.getText(CounterEngine.ATTR_FAMILY);
String icon = param.getText(CounterEngine.ATTR_ICON);
boolean subobject = param.getBoolean(CounterEngine.ATTR_SUBOBJECT);
ObjectType objType = new ObjectType();
objType.setName(name);
objType.setDisplayName(displayName);
objType.setIcon(icon);
objType.setFamily(engine.getFamily(family));
objType.setSubObject(subobject);
boolean success = appendObjectType(objType);
if (success) {
xmlCustomContent = FileUtil.readAll(customFile);
engine.addObjectType(objType);
}
return success;
}
public synchronized boolean editObjectType(MapPack param) {
String name = param.getText(CounterEngine.ATTR_NAME);
String displayName = param.getText(CounterEngine.ATTR_DISPLAY);
String family = param.getText(CounterEngine.ATTR_FAMILY);
String icon = param.getText(CounterEngine.ATTR_ICON);
boolean subobject = param.getBoolean(CounterEngine.ATTR_SUBOBJECT);
ObjectType objType = new ObjectType();
objType.setName(name);
objType.setDisplayName(displayName);
objType.setIcon(icon);
objType.setFamily(engine.getFamily(family));
objType.setSubObject(subobject);
boolean success = editOrAppendObjectType(objType);
if (success) {
xmlCustomContent = FileUtil.readAll(customFile);
engine.addObjectType(objType);
}
return success;
}
private boolean appendObjectType(ObjectType objType) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = null;
Element parentElement = null;
if (customFile.canRead()) {
doc = builder.parse(customFile);
doc.getDocumentElement().normalize();
parentElement = (Element) doc.getElementsByTagName(CounterEngine.TAG_TYPES).item(0);
} else {
doc = builder.newDocument();
parentElement = doc.createElement(CounterEngine.TAG_TYPES);
doc.appendChild(parentElement);
}
Element objElement = doc.createElement(CounterEngine.TAG_OBJECT_TYPE);
objElement.setAttribute(CounterEngine.ATTR_NAME, objType.getName());
objElement.setAttribute(CounterEngine.ATTR_DISPLAY, objType.getDisplayName());
objElement.setAttribute(CounterEngine.ATTR_FAMILY, objType.getFamily().getName());
objElement.setAttribute(CounterEngine.ATTR_ICON, objType.getIcon());
objElement.setAttribute(CounterEngine.ATTR_SUBOBJECT, objType.isSubObject() ? "true" : "false");
parentElement.appendChild(objElement);
XmlUtil.writeXmlFileWithIndent(doc, customFile, 2);
} catch (Exception e) {
Logger.println(e.getMessage());
return false;
}
return true;
}
private boolean editOrAppendObjectType(ObjectType objType) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = null;
Element parentElement = null;
boolean found = false;
if (customFile.canRead()) {
doc = builder.parse(customFile);
doc.getDocumentElement().normalize();
parentElement = (Element) doc.getElementsByTagName(CounterEngine.TAG_TYPES).item(0);
} else {
doc = builder.newDocument();
parentElement = doc.createElement(CounterEngine.TAG_TYPES);
doc.appendChild(parentElement);
}
NodeList list = doc.getElementsByTagName(CounterEngine.TAG_OBJECT_TYPE);
if (list == null || list.getLength() < 1) {
Element objElement = doc.createElement(CounterEngine.TAG_OBJECT_TYPE);
objElement.setAttribute(CounterEngine.ATTR_NAME, objType.getName());
objElement.setAttribute(CounterEngine.ATTR_DISPLAY, objType.getDisplayName());
objElement.setAttribute(CounterEngine.ATTR_FAMILY, objType.getFamily().getName());
objElement.setAttribute(CounterEngine.ATTR_ICON, objType.getIcon());
objElement.setAttribute(CounterEngine.ATTR_SUBOBJECT, objType.isSubObject() ? "true" : "false");
parentElement.appendChild(objElement);
found = true;
} else {
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element objElement = (Element) node;
String name = objElement.getAttribute(CounterEngine.ATTR_NAME);
if (objType.getName().equals(name)) {
objElement.setAttribute(CounterEngine.ATTR_DISPLAY, objType.getDisplayName());
objElement.setAttribute(CounterEngine.ATTR_FAMILY, objType.getFamily().getName());
objElement.setAttribute(CounterEngine.ATTR_ICON, objType.getIcon());
objElement.setAttribute(CounterEngine.ATTR_SUBOBJECT, objType.isSubObject() ? "true" : "false");
found = true;
break;
}
}
}
if (found == false) {
Element objElement = doc.createElement(CounterEngine.TAG_OBJECT_TYPE);
objElement.setAttribute(CounterEngine.ATTR_NAME, objType.getName());
objElement.setAttribute(CounterEngine.ATTR_DISPLAY, objType.getDisplayName());
objElement.setAttribute(CounterEngine.ATTR_FAMILY, objType.getFamily().getName());
objElement.setAttribute(CounterEngine.ATTR_ICON, objType.getIcon());
objElement.setAttribute(CounterEngine.ATTR_SUBOBJECT, objType.isSubObject() ? "true" : "false");
parentElement.appendChild(objElement);
found = true;
}
}
if (found) {
XmlUtil.writeXmlFileWithIndent(doc, customFile, 2);
return true;
}
} catch (Exception e) {
Logger.println(e.getMessage());
return false;
}
return false;
}
public static void main(String[] args) {
File f = Configure.getInstance().getPropertyFile();
System.out.println(f.getParent());
}
}