    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.pool2.impl;
    018
    019import java.io.PrintWriter;
    020import java.text.DateFormat;
    021import java.text.SimpleDateFormat;
    022import java.time.Instant;
    023
    024/**
    025 * CallStack strategy that uses the stack trace from a {@link Throwable}. This strategy, while slower than the
    026 * SecurityManager implementation, provides call stack method names and other metadata in addition to the call stack
    027 * of classes.
    028 *
    029 * @see Throwable#fillInStackTrace()
    030 * @since 2.4.3
    031 */
    032public class ThrowableCallStack implements CallStack {
    033
    034    /**
    035     * A snapshot of a throwable.
    036     */
    037    private static final class Snapshot extends Throwable {
    038
    039        private static final long serialVersionUID = 1L;
    040        private final Instant timestamp;
    041
    042        /**
    043         * Constructs a new instance with its message set to the now instant.
    044         */
    045        public Snapshot() {
    046            this(Instant.now());
    047        }
    048
    049        /**
    050         * Constructs a new instance and use the timestamp as the message with using {@link DateTimeFormatter#ISO_INSTANT} for more precision.
    051         *
    052         * @param timestamp normally the now instant.
    053         */
    054        private Snapshot(final Instant timestamp) {
    055            super(timestamp.toString());
    056            this.timestamp = timestamp;
    057        }
    058    }
    059
    060    private final String messageFormat;
    061
    062    // We keep the SimpleDateFormat for backward compatibility instead of a DateTimeFormatter.
    063    //@GuardedBy("dateFormat")
    064    private final DateFormat dateFormat;
    065
    066    private volatile Snapshot snapshot;
    067
    068    /**
    069     * Creates a new instance.
    070     *
    071     * @param messageFormat message format
    072     * @param useTimestamp whether to format the dates in the output message or not
    073     */
    074    public ThrowableCallStack(final String messageFormat, final boolean useTimestamp) {
    075        this.messageFormat = messageFormat;
    076        this.dateFormat = useTimestamp ? new SimpleDateFormat(messageFormat) : null;
    077    }
    078
    079    @Override
    080    public void clear() {
    081        snapshot = null;
    082    }
    083
    084    @Override
    085    public void fillInStackTrace() {
    086        snapshot = new Snapshot();
    087    }
    088
    089    @Override
    090    public synchronized boolean printStackTrace(final PrintWriter writer) {
    091        final Snapshot snapshotRef = this.snapshot;
    092        if (snapshotRef == null) {
    093            return false;
    094        }
    095        final String message;
    096        if (dateFormat == null) {
    097            message = messageFormat;
    098        } else {
    099            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}