Skip to content

Method: advice(ProceedingJoinPoint)

1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.role.spring.spi;
28:
29: import javax.annotation.Nonnull;
30: import org.aspectj.lang.ProceedingJoinPoint;
31: import org.aspectj.lang.annotation.Around;
32: import org.aspectj.lang.annotation.Aspect;
33: import it.tidalwave.util.ContextManager;
34: import it.tidalwave.util.Task;
35: import it.tidalwave.dci.annotation.DciContext;
36: import lombok.extern.slf4j.Slf4j;
37: import static it.tidalwave.util.ShortNames.shortId;
38:
39: /***********************************************************************************************************************
40: *
41: * An aspect which implements {@link DciContext} with {@code autoThreadBinding=true}. This class must not be used
42: * directly. Instead, add the library which contains it as a dependency of your project and make it visible to the
43: * AspectJ compiler.
44: *
45: * @author Fabrizio Giudici
46: * @since 3.0
47: *
48: **********************************************************************************************************************/
49: @Aspect @Slf4j
50: public class DciContextWithAutoThreadBindingAspect
51: {
52: @Around("within(@it.tidalwave.dci.annotation.DciContext *) && execution(* *(..))")
53: public Object advice (@Nonnull final ProceedingJoinPoint pjp)
54: throws Throwable
55: {
56: final var context = pjp.getTarget();
57:
58:• if (!context.getClass().getAnnotation(DciContext.class).autoThreadBinding())
59: {
60: return pjp.proceed();
61: }
62: else
63: {
64:• if (log.isTraceEnabled())
65: {
66: log.trace("executing {}.{}() with context thread binding", shortId(context), pjp.getSignature().getName());
67: }
68:
69: return ContextManager.getInstance().runWithContext(context, new Task<Object, Throwable>()
70: {
71: @Override
72: public Object run()
73: throws Throwable
74: {
75: return pjp.proceed();
76: }
77: });
78: }
79: }
80: }