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;
022
023/**
024 * CallStack strategy that uses the stack trace from a {@link Throwable}. This strategy, while slower than the
025 * SecurityManager implementation, provides call stack method names and other metadata in addition to the call stack
026 * of classes.
027 *
028 * @see Throwable#fillInStackTrace()
029 * @since 2.4.3
030 */
031public class ThrowableCallStack implements CallStack {
032
033    /**
034     * A snapshot of a throwable.
035     */
036    private static class Snapshot extends Throwable {
037        private static final long serialVersionUID = 1L;
038        private final long timestampMillis = System.currentTimeMillis();
039    }
040    private final String messageFormat;
041
042    //@GuardedBy("dateFormat")
043    private final DateFormat dateFormat;
044
045    private volatile Snapshot snapshot;
046
047    /**
048     * Create a new instance.
049     *
050     * @param messageFormat message format
051     * @param useTimestamp whether to format the dates in the output message or not
052     */
053    public ThrowableCallStack(final String messageFormat, final boolean useTimestamp) {
054        this.messageFormat = messageFormat;
055        this.dateFormat = useTimestamp ? new SimpleDateFormat(messageFormat) : null;
056    }
057
058    @Override
059    public void clear() {
060        snapshot = null;
061    }
062
063    @Override
064    public void fillInStackTrace() {
065        snapshot = new Snapshot();
066    }
067
068    @Override
069    public synchronized boolean printStackTrace(final PrintWriter writer) {
070        final Snapshot snapshotRef = this.snapshot;
071        if (snapshotRef == null) {
072            return false;
073        }
074        final String message;
075        if (dateFormat == null) {
076            message = messageFormat;
077        } else {
078            synchronized (dateFormat) {
079                message = dateFormat.format(Long.valueOf(snapshotRef.timestampMillis));
080            }
081        }
082        writer.println(message);
083        snapshotRef.printStackTrace(writer);
084        return true;
085    }
086}