    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    private final String messageFormat;
    034    //@GuardedBy("dateFormat")
    035    private final DateFormat dateFormat;
    036
    037    private volatile Snapshot snapshot;
    038
    039    /**
    040     * Create a new instance.
    041     *
    042     * @param messageFormat message format
    043     * @param useTimestamp whether to format the dates in the output message or not
    044     */
    045    public ThrowableCallStack(final String messageFormat, final boolean useTimestamp) {
    046        this.messageFormat = messageFormat;
    047        this.dateFormat = useTimestamp ? new SimpleDateFormat(messageFormat) : null;
    048    }
    049
    050    @Override
    051    public synchronized boolean printStackTrace(final PrintWriter writer) {
    052        final Snapshot snapshotRef = this.snapshot;
    053        if (snapshotRef == null) {
    054            return false;
    055        }
    056        final String message;
    057        if (dateFormat == null) {
    058            message = messageFormat;
    059        } else {
    060            synchronized (dateFormat) {
    061                message = dateFormat.format(Long.valueOf(snapshotRef.timestamp));
    062            }
    063        }
    064        writer.println(message);
    065        snapshotRef.printStackTrace(writer);
    066        return true;
    067    }
    068
    069    @Override
    070    public void fillInStackTrace() {
    071        snapshot = new Snapshot();
    072    }
    073
    074    @Override
    075    public void clear() {
    076        snapshot = null;
    077    }
    078
    079    /**
    080     * A snapshot of a throwable.
    081     */
    082    private static class Snapshot extends Throwable {
    083        private static final long serialVersionUID = 1L;
    084        private final long timestamp = System.currentTimeMillis();
    085    }
    086}