    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 *      https://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.List;
    026import java.util.stream.Collectors;
    027import java.util.stream.Stream;
    028
    029/**
    030 * A {@link CallStack} strategy using a {@link SecurityManager}. Obtaining the current call stack is much faster via a
    031 * SecurityManger, but access to the underlying method may be restricted by the current SecurityManager. In environments
    032 * where a SecurityManager cannot be created, {@link ThrowableCallStack} should be used instead.
    033 *
    034 * @see RuntimePermission
    035 * @see SecurityManager#getClassContext()
    036 * @since 2.4.3
    037 */
    038public class SecurityManagerCallStack implements CallStack {
    039
    040    /**
    041     * A custom security manager.
    042     */
    043    private static final class PrivateSecurityManager extends SecurityManager {
    044
    045        /**
    046         * Gets the class stack.
    047         *
    048         * @return class stack
    049         */
    050        private List<WeakReference<Class<?>>> getCallStack() {
    051            final Stream<WeakReference<Class<?>>> map = Stream.of(getClassContext()).map(WeakReference::new);
    052            return map.collect(Collectors.toList());
    053        }
    054    }
    055
    056    /**
    057     * A snapshot of a class stack.
    058     */
    059    private static final class Snapshot {
    060        private final long timestampMillis = System.currentTimeMillis();
    061        private final List<WeakReference<Class<?>>> stack;
    062
    063        /**
    064         * Constructs a new snapshot with a class stack.
    065         *
    066         * @param stack class stack
    067         */
    068        private Snapshot(final List<WeakReference<Class<?>>> stack) {
    069            this.stack = stack;
    070        }
    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     * Creates 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        snapshotRef.stack.forEach(reference -> writer.println(reference.get()));
    120        return true;
    121    }
    122}