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