View Javadoc
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  
19  import java.security.AccessControlException;
20  
21  /**
22   * Utility methods for {@link CallStack}.
23   *
24   * @since 2.4.3
25   */
26  public final class CallStackUtils {
27  
28      /**
29       * Tests whether the caller can create a security manager in the current environment.
30       *
31       * @return {@code true} if it is able to create a security manager in the current environment, {@code false}
32       *         otherwise.
33       */
34      private static boolean canCreateSecurityManager() {
35          final SecurityManager manager = System.getSecurityManager();
36          if (manager == null) {
37              return true;
38          }
39          try {
40              manager.checkPermission(new RuntimePermission("createSecurityManager"));
41              return true;
42          } catch (final AccessControlException ignored) {
43              return false;
44          }
45      }
46  
47      /**
48       * Constructs a new {@link CallStack} using the fastest allowed strategy.
49       *
50       * @param messageFormat message (or format) to print first in stack traces
51       * @param useTimestamp  if true, interpret message as a SimpleDateFormat and print the created timestamp; otherwise,
52       *                      print message format literally
53       * @return a new CallStack
54       * @deprecated use {@link #newCallStack(String, boolean, boolean)}
55       */
56      @Deprecated
57      public static CallStack newCallStack(final String messageFormat, final boolean useTimestamp) {
58          return newCallStack(messageFormat, useTimestamp, false);
59      }
60  
61      /**
62       * Constructs a new {@link CallStack} using the fasted allowed strategy.
63       *
64       * @param messageFormat         message (or format) to print first in stack traces
65       * @param useTimestamp          if true, interpret message as a SimpleDateFormat and print the created timestamp;
66       *                              otherwise, print message format literally
67       * @param requireFullStackTrace if true, forces the use of a stack walking mechanism that includes full stack trace
68       *                              information; otherwise, uses a faster implementation if possible
69       * @return a new CallStack
70       * @since 2.5
71       */
72      public static CallStack newCallStack(final String messageFormat,
73                                           final boolean useTimestamp,
74                                           final boolean requireFullStackTrace) {
75          return canCreateSecurityManager() && !requireFullStackTrace ?
76              new SecurityManagerCallStack(messageFormat, useTimestamp) :
77              new ThrowableCallStack(messageFormat, useTimestamp);
78      }
79  
80      /**
81       * Hidden constructor.
82       */
83      private CallStackUtils() {
84      }
85  }