    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.lang.ref.WeakReference;
    021import java.security.AccessController;
    022import java.security.PrivilegedAction;
    023import java.text.DateFormat;
    024import java.text.SimpleDateFormat;
    025import java.util.ArrayList;
    026import java.util.List;
    027
    028/**
    029 * CallStack strategy using a {@link SecurityManager}. Obtaining the current call stack is much faster via a
    030 * SecurityManger, but access to the underlying method may be restricted by the current SecurityManager. In environments
    031 * where a SecurityManager cannot be created, {@link ThrowableCallStack} should be used instead.
    032 *
    033 * @see RuntimePermission
    034 * @see SecurityManager#getClassContext()
    035 * @since 2.4.3
    036 */
    037public class SecurityManagerCallStack implements CallStack {
    038
    039    /**
    040     * A custom security manager.
    041     */
    042    private static class PrivateSecurityManager extends SecurityManager {
    043        /**
    044         * Get the class stack.
    045         *
    046         * @return class stack
    047         */
    048        private List<WeakReference<Class<?>>> getCallStack() {
    049            final Class<?>[] classes = getClassContext();
    050            final List<WeakReference<Class<?>>> stack = new ArrayList<>(classes.length);
    051            for (final Class<?> klass : classes) {
    052                stack.add(new WeakReference<>(klass));
    053            }
    054            return stack;
    055        }
    056    }
    057    /**
    058     * A snapshot of a class stack.
    059     */
    060    private static class Snapshot {
    061        private final long timestampMillis = System.currentTimeMillis();
    062        private final List<WeakReference<Class<?>>> stack;
    063
    064        /**
    065         * Create a new snapshot with a class stack.
    066         *
    067         * @param stack class stack
    068         */
    069        private Snapshot(final List<WeakReference<Class<?>>> stack) {
    070            this.stack = stack;
    071        }
    072    }
    073    private final String messageFormat;
    074
    075    //@GuardedBy("dateFormat")
    076    private final DateFormat dateFormat;
    077
    078    private final PrivateSecurityManager securityManager;
    079
    080    private volatile Snapshot snapshot;
    081
    082    /**
    083     * Create a new instance.
    084     *
    085     * @param messageFormat message format
    086     * @param useTimestamp whether to format the dates in the output message or not
    087     */
    088    public SecurityManagerCallStack(final String messageFormat, final boolean useTimestamp) {
    089        this.messageFormat = messageFormat;
    090        this.dateFormat = useTimestamp ? new SimpleDateFormat(messageFormat) : null;
    091        this.securityManager = AccessController.doPrivileged((PrivilegedAction<PrivateSecurityManager>) PrivateSecurityManager::new);
    092    }
    093
    094    @Override
    095    public void clear() {
    096        snapshot = null;
    097    }
    098
    099    @Override
    100    public void fillInStackTrace() {
    101        snapshot = new Snapshot(securityManager.getCallStack());
    102    }
    103
    104    @Override
    105    public boolean printStackTrace(final PrintWriter writer) {
    106        final Snapshot snapshotRef = this.snapshot;
    107        if (snapshotRef == null) {
    108            return false;
    109        }
    110        final String message;
    111        if (dateFormat == null) {
    112            message = messageFormat;
    113        } else {
    114            synchronized (dateFormat) {
    115                message = dateFormat.format(Long.valueOf(snapshotRef.timestampMillis));
    116            }
    117        }
    118        writer.println(message);
    119        for (final WeakReference<Class<?>> reference : snapshotRef.stack) {
    120            writer.println(reference.get());
    121        }
    122        return true;
    123    }
    124}