CallStackUtils.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.security.AccessControlException;

  19. /**
  20.  * Utility methods for {@link CallStack}.
  21.  *
  22.  * @since 2.4.3
  23.  */
  24. public final class CallStackUtils {

  25.     /**
  26.      * Tests whether the caller can create a security manager in the current environment.
  27.      *
  28.      * @return {@code true} if it is able to create a security manager in the current environment, {@code false}
  29.      *         otherwise.
  30.      */
  31.     private static boolean canCreateSecurityManager() {
  32.         final SecurityManager manager = System.getSecurityManager();
  33.         if (manager == null) {
  34.             return true;
  35.         }
  36.         try {
  37.             manager.checkPermission(new RuntimePermission("createSecurityManager"));
  38.             return true;
  39.         } catch (final AccessControlException ignored) {
  40.             return false;
  41.         }
  42.     }

  43.     /**
  44.      * Constructs a new {@link CallStack} using the fastest allowed strategy.
  45.      *
  46.      * @param messageFormat message (or format) to print first in stack traces
  47.      * @param useTimestamp  if true, interpret message as a SimpleDateFormat and print the created timestamp; otherwise,
  48.      *                      print message format literally
  49.      * @return a new CallStack
  50.      * @deprecated use {@link #newCallStack(String, boolean, boolean)}
  51.      */
  52.     @Deprecated
  53.     public static CallStack newCallStack(final String messageFormat, final boolean useTimestamp) {
  54.         return newCallStack(messageFormat, useTimestamp, false);
  55.     }

  56.     /**
  57.      * Constructs a new {@link CallStack} using the fasted allowed strategy.
  58.      *
  59.      * @param messageFormat         message (or format) to print first in stack traces
  60.      * @param useTimestamp          if true, interpret message as a SimpleDateFormat and print the created timestamp;
  61.      *                              otherwise, print message format literally
  62.      * @param requireFullStackTrace if true, forces the use of a stack walking mechanism that includes full stack trace
  63.      *                              information; otherwise, uses a faster implementation if possible
  64.      * @return a new CallStack
  65.      * @since 2.5
  66.      */
  67.     public static CallStack newCallStack(final String messageFormat,
  68.                                          final boolean useTimestamp,
  69.                                          final boolean requireFullStackTrace) {
  70.         return canCreateSecurityManager() && !requireFullStackTrace ?
  71.             new SecurityManagerCallStack(messageFormat, useTimestamp) :
  72.             new ThrowableCallStack(messageFormat, useTimestamp);
  73.     }

  74.     /**
  75.      * Hidden constructor.
  76.      */
  77.     private CallStackUtils() {
  78.     }
  79. }