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.io.PrintWriter;
20  import java.lang.ref.WeakReference;
21  import java.security.AccessController;
22  import java.security.PrivilegedAction;
23  import java.text.DateFormat;
24  import java.text.SimpleDateFormat;
25  import java.util.List;
26  import java.util.stream.Collectors;
27  import java.util.stream.Stream;
28  
29  /**
30   * A {@link CallStack} strategy using a {@link SecurityManager}. Obtaining the current call stack is much faster via a
31   * SecurityManger, but access to the underlying method may be restricted by the current SecurityManager. In environments
32   * where a SecurityManager cannot be created, {@link ThrowableCallStack} should be used instead.
33   *
34   * @see RuntimePermission
35   * @see SecurityManager#getClassContext()
36   * @since 2.4.3
37   */
38  public class SecurityManagerCallStack implements CallStack {
39  
40      /**
41       * A custom security manager.
42       */
43      private static class PrivateSecurityManager extends SecurityManager {
44  
45          /**
46           * Gets the class stack.
47           *
48           * @return class stack
49           */
50          private List<WeakReference<Class<?>>> getCallStack() {
51              final Stream<WeakReference<Class<?>>> map = Stream.of(getClassContext()).map(WeakReference::new);
52              return map.collect(Collectors.toList());
53          }
54      }
55  
56      /**
57       * A snapshot of a class stack.
58       */
59      private static class Snapshot {
60          private final long timestampMillis = System.currentTimeMillis();
61          private final List<WeakReference<Class<?>>> stack;
62  
63          /**
64           * Constructs a new snapshot with a class stack.
65           *
66           * @param stack class stack
67           */
68          private Snapshot(final List<WeakReference<Class<?>>> stack) {
69              this.stack = stack;
70          }
71      }
72  
73      private final String messageFormat;
74  
75      //@GuardedBy("dateFormat")
76      private final DateFormat dateFormat;
77  
78      private final PrivateSecurityManager securityManager;
79  
80      private volatile Snapshot snapshot;
81  
82      /**
83       * Creates a new instance.
84       *
85       * @param messageFormat message format
86       * @param useTimestamp whether to format the dates in the output message or not
87       */
88      public SecurityManagerCallStack(final String messageFormat, final boolean useTimestamp) {
89          this.messageFormat = messageFormat;
90          this.dateFormat = useTimestamp ? new SimpleDateFormat(messageFormat) : null;
91          this.securityManager = AccessController.doPrivileged((PrivilegedAction<PrivateSecurityManager>) PrivateSecurityManager::new);
92      }
93  
94      @Override
95      public void clear() {
96          snapshot = null;
97      }
98  
99      @Override
100     public void fillInStackTrace() {
101         snapshot = new Snapshot(securityManager.getCallStack());
102     }
103 
104     @Override
105     public boolean printStackTrace(final PrintWriter writer) {
106         final Snapshot snapshotRef = this.snapshot;
107         if (snapshotRef == null) {
108             return false;
109         }
110         final String message;
111         if (dateFormat == null) {
112             message = messageFormat;
113         } else {
114             synchronized (dateFormat) {
115                 message = dateFormat.format(Long.valueOf(snapshotRef.timestampMillis));
116             }
117         }
118         writer.println(message);
119         snapshotRef.stack.forEach(reference -> writer.println(reference.get()));
120         return true;
121     }
122 }