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.security.AccessControlException; 020 021/** 022 * Utility methods for {@link CallStack}. 023 * 024 * @since 2.4.3 025 */ 026public final class CallStackUtils { 027 028 /** 029 * @return {@code true} if it is able to create a security manager in the current environment, {@code false} 030 * otherwise. 031 */ 032 private static boolean canCreateSecurityManager() { 033 final SecurityManager manager = System.getSecurityManager(); 034 if (manager == null) { 035 return true; 036 } 037 try { 038 manager.checkPermission(new RuntimePermission("createSecurityManager")); 039 return true; 040 } catch (final AccessControlException ignored) { 041 return false; 042 } 043 } 044 045 /** 046 * Constructs a new {@link CallStack} using the fastest allowed strategy. 047 * 048 * @param messageFormat message (or format) to print first in stack traces 049 * @param useTimestamp if true, interpret message as a SimpleDateFormat and print the created timestamp; otherwise, 050 * print message format literally 051 * @return a new CallStack 052 * @deprecated use {@link #newCallStack(String, boolean, boolean)} 053 */ 054 @Deprecated 055 public static CallStack newCallStack(final String messageFormat, final boolean useTimestamp) { 056 return newCallStack(messageFormat, useTimestamp, false); 057 } 058 059 /** 060 * Constructs a new {@link CallStack} using the fasted allowed strategy. 061 * 062 * @param messageFormat message (or format) to print first in stack traces 063 * @param useTimestamp if true, interpret message as a SimpleDateFormat and print the created timestamp; 064 * otherwise, print message format literally 065 * @param requireFullStackTrace if true, forces the use of a stack walking mechanism that includes full stack trace 066 * information; otherwise, uses a faster implementation if possible 067 * @return a new CallStack 068 * @since 2.5 069 */ 070 public static CallStack newCallStack(final String messageFormat, 071 final boolean useTimestamp, 072 final boolean requireFullStackTrace) { 073 return canCreateSecurityManager() && !requireFullStackTrace 074 ? new SecurityManagerCallStack(messageFormat, useTimestamp) 075 : new ThrowableCallStack(messageFormat, useTimestamp); 076 } 077 078 /** 079 * Hidden constructor. 080 */ 081 private CallStackUtils() { 082 } 083}