1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.timer;
17
18 import java.io.IOException;
19 import java.io.PrintWriter;
20 import java.util.Arrays;
21 import java.util.function.Supplier;
22
23 import org.apache.commons.io.output.ByteArrayOutputStream;
24 import org.apache.commons.text.StringEscapeUtils;
25 import org.codelibs.core.timer.TimeoutTarget;
26 import org.codelibs.fess.Constants;
27 import org.codelibs.fess.helper.SystemHelper;
28 import org.codelibs.fess.taglib.FessFunctions;
29 import org.codelibs.fess.util.ComponentUtil;
30
31
32
33
34 public abstract class MonitorTarget implements TimeoutTarget {
35
36
37
38
39 public MonitorTarget() {
40
41 }
42
43
44
45
46
47
48
49
50
51 protected StringBuilder append(final StringBuilder buf, final String key, final Supplier<Object> supplier) {
52 final StringBuilder tempBuf = new StringBuilder();
53 tempBuf.append('"').append(key).append("\":");
54 try {
55 final Object value = supplier.get();
56 if (value == null) {
57 tempBuf.append("null");
58 } else if (value instanceof Integer || value instanceof Long) {
59 tempBuf.append(value);
60 } else if (value instanceof Short) {
61 tempBuf.append(((Short) value).shortValue());
62 } else if (value instanceof double[]) {
63 tempBuf.append(Arrays.toString((double[]) value));
64 } else {
65 tempBuf.append('"').append(StringEscapeUtils.escapeJson(value.toString())).append('"');
66 }
67 } catch (final Exception e) {
68 tempBuf.append("null");
69 }
70 buf.append(tempBuf.toString());
71 return buf;
72 }
73
74
75
76
77
78
79
80 protected StringBuilder appendTimestamp(final StringBuilder buf) {
81 final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
82 append(buf, "timestamp", () -> FessFunctions.formatDate(systemHelper.getCurrentTime()));
83 return buf;
84 }
85
86
87
88
89
90
91
92
93 protected StringBuilder appendException(final StringBuilder buf, final Exception exception) {
94 try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
95 PrintWriter writer = new PrintWriter(baos, false, Constants.CHARSET_UTF_8)) {
96 exception.printStackTrace(writer);
97 writer.flush();
98 append(buf, "exception", () -> StringEscapeUtils.escapeJson(new String(baos.toByteArray(), Constants.CHARSET_UTF_8)));
99 } catch (final IOException e) {
100 append(buf, "exception", () -> StringEscapeUtils.escapeJson(e.getMessage()));
101 }
102 return buf;
103 }
104 }