001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.utils;
014
015import java.util.concurrent.*;
016
017/**
018 * An in-memory cache of stack traces.
019 *
020 * <p>
021 * Used for preventing duplication of stack traces in log files and replacing them with small hashes.
022 */
023public class StackTraceDatabase {
024
025   private final ConcurrentHashMap<Integer,StackTraceInfo> db = new ConcurrentHashMap<>();
026   private final String stopClass;
027
028   /**
029    * Constructor.
030    */
031   public StackTraceDatabase() {
032      this.stopClass = null;
033   }
034
035   /**
036    * Constructor.
037    *
038    * @param stopClass When this class is encountered in a stack trace, stop calculating the hash.
039    */
040   public StackTraceDatabase(Class<?> stopClass) {
041      this.stopClass = stopClass == null ? "" : stopClass.getName();
042   }
043
044   /**
045    * Retrieves the stack trace information for the specified exception.
046    *
047    * @param e The exception.
048    * @param timeout The timeout in milliseconds to cache the hash for this stack trace.
049    * @return The stack trace info, never <jk>null</jk>.
050    */
051   public StackTraceInfo getStackTraceInfo(Throwable e, int timeout) {
052      int hash = hash(e);
053      StackTraceInfo stc = db.get(hash);
054      if (stc != null && stc.timeout > System.currentTimeMillis()) {
055         stc.incrementAndClone();
056         return stc.clone();
057      }
058      synchronized (db) {
059         stc = new StackTraceInfo(timeout, hash);
060         db.put(hash, stc);
061         return stc.clone();
062      }
063   }
064
065   private int hash(Throwable t) {
066      int i = 0;
067      while (t != null) {
068         for (StackTraceElement e : t.getStackTrace()) {
069            if (e.getClassName().equals(stopClass))
070               break;
071            if (e.getClassName().indexOf('$') == -1)
072               i ^= e.hashCode();
073         }
074         t = t.getCause();
075      }
076      return i;
077   }
078
079   /**
080    * Clears out the stack trace cache.
081    */
082   public void reset() {
083      db.clear();
084   }
085}