001package ca.uhn.fhir.interceptor.api; 002 003/*- 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2021 Smile CDR, Inc. 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import javax.annotation.Nullable; 024import java.util.Collection; 025import java.util.List; 026import java.util.function.Predicate; 027 028public interface IBaseInterceptorService<POINTCUT extends IPointcut> extends IBaseInterceptorBroadcaster<POINTCUT> { 029 030 /** 031 * Register an interceptor that will be used in a {@link ThreadLocal} context. 032 * This means that events will only be broadcast to the given interceptor if 033 * they were fired from the current thread. 034 * <p> 035 * Note that it is almost always desirable to call this method with a 036 * try-finally statement that removes the interceptor afterwards, since 037 * this can lead to memory leakage, poor performance due to ever-increasing 038 * numbers of interceptors, etc. 039 * </p> 040 * <p> 041 * Note that most methods such as {@link #getAllRegisteredInterceptors()} and 042 * {@link #unregisterAllInterceptors()} do not affect thread local interceptors 043 * as they are kept in a separate list. 044 * </p> 045 * 046 * @param theInterceptor The interceptor 047 * @return Returns <code>true</code> if at least one valid hook method was found on this interceptor 048 */ 049 boolean registerThreadLocalInterceptor(Object theInterceptor); 050 051 /** 052 * Unregisters a ThreadLocal interceptor 053 * 054 * @param theInterceptor The interceptor 055 * @see #registerThreadLocalInterceptor(Object) 056 */ 057 void unregisterThreadLocalInterceptor(Object theInterceptor); 058 059 /** 060 * Register an interceptor. This method has no effect if the given interceptor is already registered. 061 * 062 * @param theInterceptor The interceptor to register 063 * @return Returns <code>true</code> if at least one valid hook method was found on this interceptor 064 */ 065 boolean registerInterceptor(Object theInterceptor); 066 067 /** 068 * Unregister an interceptor. This method has no effect if the given interceptor is not already registered. 069 * 070 * @param theInterceptor The interceptor to unregister 071 * @return Returns <code>true</code> if the interceptor was found and removed 072 */ 073 boolean unregisterInterceptor(Object theInterceptor); 074 075 /** 076 * Returns all currently registered interceptors (excluding any thread local interceptors). 077 */ 078 List<Object> getAllRegisteredInterceptors(); 079 080 /** 081 * Unregisters all registered interceptors. Note that this method does not unregister 082 * any {@link #registerThreadLocalInterceptor(Object) thread local interceptors}. 083 */ 084 void unregisterAllInterceptors(); 085 086 void unregisterInterceptors(@Nullable Collection<?> theInterceptors); 087 088 void registerInterceptors(@Nullable Collection<?> theInterceptors); 089 090 /** 091 * Unregisters all interceptors that are indicated by the given callback function returning <code>true</code> 092 */ 093 void unregisterInterceptorsIf(Predicate<Object> theShouldUnregisterFunction); 094}