001package ca.uhn.fhir.interceptor.executor; 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 ca.uhn.fhir.interceptor.api.Hook; 024import ca.uhn.fhir.interceptor.api.HookParams; 025import ca.uhn.fhir.interceptor.api.IAnonymousInterceptor; 026import ca.uhn.fhir.interceptor.api.IInterceptorBroadcaster; 027import ca.uhn.fhir.interceptor.api.IInterceptorService; 028import ca.uhn.fhir.interceptor.api.Interceptor; 029import ca.uhn.fhir.interceptor.api.Pointcut; 030import com.google.common.annotations.VisibleForTesting; 031import org.apache.commons.lang3.Validate; 032 033import java.lang.reflect.Method; 034import java.util.Optional; 035 036public class InterceptorService extends BaseInterceptorService<Pointcut> implements IInterceptorService, IInterceptorBroadcaster { 037 038 /** 039 * Constructor which uses a default name of "default" 040 */ 041 public InterceptorService() { 042 this("default"); 043 } 044 045 /** 046 * Constructor 047 * 048 * @param theName The name for this registry (useful for troubleshooting) 049 */ 050 public InterceptorService(String theName) { 051 super(theName); 052 } 053 054 @Override 055 protected Optional<HookDescriptor> scanForHook(Method nextMethod) { 056 return findAnnotation(nextMethod, Hook.class).map(t -> new HookDescriptor(t.value(), t.order())); 057 } 058 059 060 @Override 061 @VisibleForTesting 062 public void registerAnonymousInterceptor(Pointcut thePointcut, IAnonymousInterceptor theInterceptor) { 063 registerAnonymousInterceptor(thePointcut, Interceptor.DEFAULT_ORDER, theInterceptor); 064 } 065 066 @Override 067 public void registerAnonymousInterceptor(Pointcut thePointcut, int theOrder, IAnonymousInterceptor theInterceptor) { 068 Validate.notNull(thePointcut); 069 Validate.notNull(theInterceptor); 070 BaseInvoker invoker = new AnonymousLambdaInvoker(thePointcut, theInterceptor, theOrder); 071 registerAnonymousInterceptor(thePointcut, theInterceptor, invoker); 072 } 073 074 075 private class AnonymousLambdaInvoker extends BaseInvoker { 076 private final IAnonymousInterceptor myHook; 077 private final Pointcut myPointcut; 078 079 public AnonymousLambdaInvoker(Pointcut thePointcut, IAnonymousInterceptor theHook, int theOrder) { 080 super(theHook, theOrder); 081 myHook = theHook; 082 myPointcut = thePointcut; 083 } 084 085 @Override 086 Object invoke(HookParams theParams) { 087 myHook.invoke(myPointcut, theParams); 088 return true; 089 } 090 } 091 092 093}