SecurityManagerCallStack.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.pool2.impl;

  18. import java.io.PrintWriter;
  19. import java.lang.ref.WeakReference;
  20. import java.security.AccessController;
  21. import java.security.PrivilegedAction;
  22. import java.text.DateFormat;
  23. import java.text.SimpleDateFormat;
  24. import java.util.List;
  25. import java.util.stream.Collectors;
  26. import java.util.stream.Stream;

  27. /**
  28.  * A {@link CallStack} strategy using a {@link SecurityManager}. Obtaining the current call stack is much faster via a
  29.  * SecurityManger, but access to the underlying method may be restricted by the current SecurityManager. In environments
  30.  * where a SecurityManager cannot be created, {@link ThrowableCallStack} should be used instead.
  31.  *
  32.  * @see RuntimePermission
  33.  * @see SecurityManager#getClassContext()
  34.  * @since 2.4.3
  35.  */
  36. public class SecurityManagerCallStack implements CallStack {

  37.     /**
  38.      * A custom security manager.
  39.      */
  40.     private static final class PrivateSecurityManager extends SecurityManager {

  41.         /**
  42.          * Gets the class stack.
  43.          *
  44.          * @return class stack
  45.          */
  46.         private List<WeakReference<Class<?>>> getCallStack() {
  47.             final Stream<WeakReference<Class<?>>> map = Stream.of(getClassContext()).map(WeakReference::new);
  48.             return map.collect(Collectors.toList());
  49.         }
  50.     }

  51.     /**
  52.      * A snapshot of a class stack.
  53.      */
  54.     private static final class Snapshot {
  55.         private final long timestampMillis = System.currentTimeMillis();
  56.         private final List<WeakReference<Class<?>>> stack;

  57.         /**
  58.          * Constructs a new snapshot with a class stack.
  59.          *
  60.          * @param stack class stack
  61.          */
  62.         private Snapshot(final List<WeakReference<Class<?>>> stack) {
  63.             this.stack = stack;
  64.         }
  65.     }

  66.     private final String messageFormat;

  67.     //@GuardedBy("dateFormat")
  68.     private final DateFormat dateFormat;

  69.     private final PrivateSecurityManager securityManager;

  70.     private volatile Snapshot snapshot;

  71.     /**
  72.      * Creates a new instance.
  73.      *
  74.      * @param messageFormat message format
  75.      * @param useTimestamp whether to format the dates in the output message or not
  76.      */
  77.     public SecurityManagerCallStack(final String messageFormat, final boolean useTimestamp) {
  78.         this.messageFormat = messageFormat;
  79.         this.dateFormat = useTimestamp ? new SimpleDateFormat(messageFormat) : null;
  80.         this.securityManager = AccessController.doPrivileged((PrivilegedAction<PrivateSecurityManager>) PrivateSecurityManager::new);
  81.     }

  82.     @Override
  83.     public void clear() {
  84.         snapshot = null;
  85.     }

  86.     @Override
  87.     public void fillInStackTrace() {
  88.         snapshot = new Snapshot(securityManager.getCallStack());
  89.     }

  90.     @Override
  91.     public boolean printStackTrace(final PrintWriter writer) {
  92.         final Snapshot snapshotRef = this.snapshot;
  93.         if (snapshotRef == null) {
  94.             return false;
  95.         }
  96.         final String message;
  97.         if (dateFormat == null) {
  98.             message = messageFormat;
  99.         } else {
  100.             synchronized (dateFormat) {
  101.                 message = dateFormat.format(Long.valueOf(snapshotRef.timestampMillis));
  102.             }
  103.         }
  104.         writer.println(message);
  105.         snapshotRef.stack.forEach(reference -> writer.println(reference.get()));
  106.         return true;
  107.     }
  108. }