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.aop; 017 018import com.jfinal.aop.Interceptor; 019import io.jboot.aop.annotation.AutoLoad; 020import io.jboot.core.weight.WeightUtil; 021import io.jboot.utils.ClassScanner; 022import io.jboot.utils.ClassUtil; 023 024import java.lang.reflect.Method; 025import java.util.Collection; 026import java.util.List; 027import java.util.concurrent.CopyOnWriteArrayList; 028import java.util.function.Predicate; 029 030public class InterceptorBuilderManager{ 031 032 033 private static InterceptorBuilderManager me = new InterceptorBuilderManager(); 034 035 036 public static InterceptorBuilderManager me() { 037 return me; 038 } 039 040 private InterceptorBuilderManager() { 041 List<Class<InterceptorBuilder>> builderClasses = ClassScanner.scanSubClass(InterceptorBuilder.class,true); 042 for (Class<InterceptorBuilder> builderClass : builderClasses){ 043 if (builderClass.getAnnotation(AutoLoad.class) != null){ 044 addInterceptorBuilder(ClassUtil.newInstance(builderClass,false)); 045 } 046 } 047 048 InterceptorCache.clear(); 049 } 050 051 private List<InterceptorBuilder> interceptorBuilders = new CopyOnWriteArrayList(); 052 053 054 public List<InterceptorBuilder> getInterceptorBuilders() { 055 return interceptorBuilders; 056 } 057 058 059 060 public void addInterceptorBuilder(InterceptorBuilder interceptorBuilder) { 061 if (interceptorBuilder == null) { 062 throw new NullPointerException("interceptorBuilder must not be null."); 063 } 064 this.interceptorBuilders.add(interceptorBuilder); 065 WeightUtil.sort(this.interceptorBuilders); 066 067 InterceptorCache.clear(); 068 } 069 070 071 072 073 public void addInterceptorBuilder(Collection<InterceptorBuilder> interceptorBuilders) { 074 if (interceptorBuilders == null) { 075 throw new NullPointerException("interceptorBuilder must not be null."); 076 } 077 this.interceptorBuilders.addAll(interceptorBuilders); 078 WeightUtil.sort(this.interceptorBuilders); 079 080 InterceptorCache.clear(); 081 } 082 083 084 085 public void removeInterceptorBuilder(Predicate<? super InterceptorBuilder> filter){ 086 if (interceptorBuilders != null && !interceptorBuilders.isEmpty()){ 087 if (interceptorBuilders.removeIf(filter)) { 088 InterceptorCache.clear(); 089 } 090 } 091 } 092 093 094 095 public Interceptor[] build(Class<?> targetClass, Method method, Interceptor[] inters) { 096 if (interceptorBuilders != null && interceptorBuilders.size() > 0) { 097 Interceptors interceptors = new Interceptors(inters); 098 for (InterceptorBuilder builder : interceptorBuilders) { 099 builder.build(targetClass, method, interceptors); 100 } 101 return interceptors.toArray(); 102 } 103 return inters; 104 } 105 106 107 108}