View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.pool2.impl;
18  
19  import java.io.PrintWriter;
20  import java.text.DateFormat;
21  import java.text.SimpleDateFormat;
22  import java.time.Instant;
23  
24  /**
25   * CallStack strategy that uses the stack trace from a {@link Throwable}. This strategy, while slower than the
26   * SecurityManager implementation, provides call stack method names and other metadata in addition to the call stack
27   * of classes.
28   *
29   * @see Throwable#fillInStackTrace()
30   * @since 2.4.3
31   */
32  public class ThrowableCallStack implements CallStack {
33  
34      /**
35       * A snapshot of a throwable.
36       */
37      private static final class Snapshot extends Throwable {
38  
39          private static final long serialVersionUID = 1L;
40          private final Instant timestamp;
41  
42          /**
43           * Constructs a new instance with its message set to the now instant.
44           */
45          public Snapshot() {
46              this(Instant.now());
47          }
48  
49          /**
50           * Constructs a new instance and use the timestamp as the message with using {@link DateTimeFormatter#ISO_INSTANT} for more precision.
51           *
52           * @param timestamp normally the now instant.
53           */
54          private Snapshot(final Instant timestamp) {
55              super(timestamp.toString());
56              this.timestamp = timestamp;
57          }
58      }
59  
60      private final String messageFormat;
61  
62      // We keep the SimpleDateFormat for backward compatibility instead of a DateTimeFormatter.
63      //@GuardedBy("dateFormat")
64      private final DateFormat dateFormat;
65  
66      private volatile Snapshot snapshot;
67  
68      /**
69       * Creates a new instance.
70       *
71       * @param messageFormat message format
72       * @param useTimestamp whether to format the dates in the output message or not
73       */
74      public ThrowableCallStack(final String messageFormat, final boolean useTimestamp) {
75          this.messageFormat = messageFormat;
76          this.dateFormat = useTimestamp ? new SimpleDateFormat(messageFormat) : null;
77      }
78  
79      @Override
80      public void clear() {
81          snapshot = null;
82      }
83  
84      @Override
85      public void fillInStackTrace() {
86          snapshot = new Snapshot();
87      }
88  
89      @Override
90      public synchronized boolean printStackTrace(final PrintWriter writer) {
91          final Snapshot snapshotRef = this.snapshot;
92          if (snapshotRef == null) {
93              return false;
94          }
95          final String message;
96          if (dateFormat == null) {
97              message = messageFormat;
98          } else {
99              synchronized (dateFormat) {
100                 // The throwable message is in {@link DateTimeFormatter#ISO_INSTANT} format for more precision.
101                 message = dateFormat.format(Long.valueOf(snapshotRef.timestamp.toEpochMilli()));
102             }
103         }
104         writer.println(message);
105         snapshotRef.printStackTrace(writer);
106         return true;
107     }
108 }