View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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   * Abstract base class for monitor targets that implement timeout functionality.
33   */
34  public abstract class MonitorTarget implements TimeoutTarget {
35  
36      /**
37       * Default constructor.
38       */
39      public MonitorTarget() {
40          // Default constructor
41      }
42  
43      /**
44       * Appends a key-value pair to the buffer in JSON format.
45       *
46       * @param buf the string buffer to append to
47       * @param key the key name
48       * @param supplier the value supplier
49       * @return the updated buffer
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       * Appends a timestamp to the buffer.
76       *
77       * @param buf the string buffer to append to
78       * @return the updated buffer
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       * Appends exception information to the buffer.
88       *
89       * @param buf the string buffer to append to
90       * @param exception the exception to append
91       * @return the updated buffer
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 }