ThrowableCallStack.java

  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. import java.io.PrintWriter;
  19. import java.text.DateFormat;
  20. import java.text.SimpleDateFormat;
  21. import java.time.Instant;

  22. /**
  23.  * CallStack strategy that uses the stack trace from a {@link Throwable}. This strategy, while slower than the
  24.  * SecurityManager implementation, provides call stack method names and other metadata in addition to the call stack
  25.  * of classes.
  26.  *
  27.  * @see Throwable#fillInStackTrace()
  28.  * @since 2.4.3
  29.  */
  30. public class ThrowableCallStack implements CallStack {

  31.     /**
  32.      * A snapshot of a throwable.
  33.      */
  34.     private static final class Snapshot extends Throwable {

  35.         private static final long serialVersionUID = 1L;
  36.         private final Instant timestamp;

  37.         /**
  38.          * Constructs a new instance with its message set to the now instant.
  39.          */
  40.         public Snapshot() {
  41.             this(Instant.now());
  42.         }

  43.         /**
  44.          * Constructs a new instance and use the timestamp as the message with using {@link DateTimeFormatter#ISO_INSTANT} for more precision.
  45.          *
  46.          * @param timestamp normally the now instant.
  47.          */
  48.         private Snapshot(final Instant timestamp) {
  49.             super(timestamp.toString());
  50.             this.timestamp = timestamp;
  51.         }
  52.     }

  53.     private final String messageFormat;

  54.     // We keep the SimpleDateFormat for backward compatibility instead of a DateTimeFormatter.
  55.     //@GuardedBy("dateFormat")
  56.     private final DateFormat dateFormat;

  57.     private volatile Snapshot snapshot;

  58.     /**
  59.      * Creates a new instance.
  60.      *
  61.      * @param messageFormat message format
  62.      * @param useTimestamp whether to format the dates in the output message or not
  63.      */
  64.     public ThrowableCallStack(final String messageFormat, final boolean useTimestamp) {
  65.         this.messageFormat = messageFormat;
  66.         this.dateFormat = useTimestamp ? new SimpleDateFormat(messageFormat) : null;
  67.     }

  68.     @Override
  69.     public void clear() {
  70.         snapshot = null;
  71.     }

  72.     @Override
  73.     public void fillInStackTrace() {
  74.         snapshot = new Snapshot();
  75.     }

  76.     @Override
  77.     public synchronized boolean printStackTrace(final PrintWriter writer) {
  78.         final Snapshot snapshotRef = this.snapshot;
  79.         if (snapshotRef == null) {
  80.             return false;
  81.         }
  82.         final String message;
  83.         if (dateFormat == null) {
  84.             message = messageFormat;
  85.         } else {
  86.             synchronized (dateFormat) {
  87.                 // The throwable message is in {@link DateTimeFormatter#ISO_INSTANT} format for more precision.
  88.                 message = dateFormat.format(Long.valueOf(snapshotRef.timestamp.toEpochMilli()));
  89.             }
  90.         }
  91.         writer.println(message);
  92.         snapshotRef.printStackTrace(writer);
  93.         return true;
  94.     }
  95. }