001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.exception;
017
018import java.util.LinkedList;
019import java.util.List;
020
021/**
022 * Created by michael on 2017/7/6.
023 */
024public class JbootExceptionHolder {
025
026    static ThreadLocal<List<Throwable>> throwables = ThreadLocal.withInitial(() -> new LinkedList<>());
027    static ThreadLocal<List<String>> messages = ThreadLocal.withInitial(() -> new LinkedList<>());
028
029    public static void release() {
030        if (!throwables.get().isEmpty()){
031            throwables.get().clear();
032        }
033        if (!messages.get().isEmpty()){
034            messages.get().clear();
035        }
036    }
037
038    public static void hold(Throwable ex) {
039        throwables.get().add(ex);
040    }
041    public static void hold(String message,Throwable ex) {
042        if (message != null) {
043            messages.get().add(message);
044        }
045        if (ex != null) {
046            throwables.get().add(ex);
047        }
048    }
049
050    public static List<Throwable> getThrowables() {
051        return throwables.get();
052    }
053
054    public static List<String> getMessages() {
055        return messages.get();
056    }
057
058
059}