Skip to content

Package: DciContextWithAutoThreadBindingAspect$1

DciContextWithAutoThreadBindingAspect$1

nameinstructionbranchcomplexitylinemethod
run()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
{...}
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 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.Task;
34: import it.tidalwave.role.ContextManager;
35: import it.tidalwave.dci.annotation.DciContext;
36: import lombok.extern.slf4j.Slf4j;
37: import static it.tidalwave.role.spi.impl.LogUtil.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 Object 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: // It looks like the @Inject approach creates bogus multiple instance of ContextManager
70: final ContextManager contextManager = ContextManager.Locator.find();
71: return contextManager.runWithContext(context, new Task<Object, Throwable>()
72: {
73: @Override
74: public Object run()
75: throws Throwable
76: {
77: return pjp.proceed();
78: }
79: });
80: }
81: }
82: }