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.web.handler; 017 018import com.jfinal.aop.Interceptor; 019import com.jfinal.core.Action; 020import com.jfinal.core.Controller; 021 022import java.lang.reflect.InvocationTargetException; 023import java.util.ArrayList; 024import java.util.List; 025 026/** 027 * @author michael yang (fuhai999@gmail.com) 028 */ 029public class JbootActionReporterInvocation extends JbootActionInvocation { 030 031 032 protected static ThreadLocal<List<Interceptor>> invokedInterceptors = ThreadLocal.withInitial(ArrayList::new); 033 protected static ThreadLocal<Boolean> controllerInvokeFlag = ThreadLocal.withInitial(() -> Boolean.FALSE); 034 035 public JbootActionReporterInvocation(Action action, Controller controller) { 036 super(action, controller); 037 } 038 039 040 @Override 041 public void invoke() { 042 if (index < inters.length) { 043 Interceptor interceptor = inters[index++]; 044 invokedInterceptors.get().add(interceptor); 045 interceptor.intercept(this); 046 } else if (index++ == inters.length) { // index++ ensure invoke action only one time 047 try { 048 // Invoke the action 049 controllerInvokeFlag.set(true); 050 returnValue = action.getMethod().invoke(target, args); 051 } catch (InvocationTargetException e) { 052 Throwable t = e.getTargetException(); 053 if (t == null) { 054 t = e; 055 } 056 throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t); 057 } catch (RuntimeException e) { 058 throw e; 059 } catch (Throwable t) { 060 throw new RuntimeException(t); 061 } 062 } 063 } 064 065 066 public static List<Interceptor> getInvokedInterceptor() { 067 return invokedInterceptors.get(); 068 } 069 070 public static boolean isControllerInvoked() { 071 return controllerInvokeFlag.get(); 072 } 073 074 075 public static void clear() { 076 invokedInterceptors.get().clear(); 077 invokedInterceptors.remove(); 078 controllerInvokeFlag.remove(); 079 } 080 081 public Interceptor[] getInters() { 082 return inters; 083 } 084 085 086}