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