1 package pk.lucidxpo.ynami.spring.aspect;
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.aspectj.lang.ProceedingJoinPoint;
5 import org.aspectj.lang.Signature;
6 import org.aspectj.lang.annotation.Around;
7 import org.aspectj.lang.annotation.Aspect;
8 import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;
9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.stereotype.Component;
11 import pk.lucidxpo.ynami.spring.features.FeatureManagerWrappable;
12
13 @Slf4j
14 @Aspect
15 @Component
16 public class FeaturesAspect {
17 private final FeatureManagerWrappable featureManager;
18
19 @Autowired
20 public FeaturesAspect(final FeatureManagerWrappable featureManager) {
21 this.featureManager = featureManager;
22 }
23
24 @Around(
25 "@within(featureAssociation) || @annotation(featureAssociation)"
26 )
27 public Object checkAspect(final ProceedingJoinPoint joinPoint,
28 final FeatureAssociation featureAssociation) throws Throwable {
29 if (featureManager.isActive(featureAssociation.value())) {
30 return joinPoint.proceed();
31 }
32 final Signature signature = ((MethodInvocationProceedingJoinPoint) joinPoint).getSignature();
33 log.info(
34 "Execution of '" + signature.getName() + "' method in '" +
35 signature.getDeclaringTypeName() + "' class is blocked " +
36 "as the feature '" + featureAssociation.value() + "' is not enabled!"
37 );
38 return null;
39 }
40 }