re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / lock / ComponentLockAspect.java
1 package org.openecomp.sdc.be.components.impl.lock;
2
3 import org.aspectj.lang.ProceedingJoinPoint;
4 import org.openecomp.sdc.be.components.impl.ComponentLocker;
5 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
6 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
7 import org.openecomp.sdc.common.log.wrappers.Logger;
8
9 public class ComponentLockAspect {
10
11     private static final Logger log = Logger.getLogger(ComponentLockAspect.class);
12     private final ComponentLocker componentLocker;
13
14     public ComponentLockAspect(ComponentLocker componentLocker) {
15         this.componentLocker = componentLocker;
16     }
17
18     public Object lock(ProceedingJoinPoint proceedingJoinPoint, String componentId, ComponentTypeEnum componentType) throws Throwable {
19         return surroundWithLockUnlock(proceedingJoinPoint, componentId, componentType.getNodeType());
20     }
21
22     private Object surroundWithLockUnlock(ProceedingJoinPoint proceedingJoinPoint, String componentId, NodeTypeEnum componentType) throws Throwable {
23         String methodName = proceedingJoinPoint.getSignature().toShortString();
24         try {
25             log.trace("#{} - before locking component {} of type {}", methodName, componentId, componentType);
26             componentLocker.lock(componentId, componentType);
27             log.trace("#{} - after locking component {} of type {}", methodName, componentId, componentType);
28             return proceedingJoinPoint.proceed();
29         }
30         finally {
31             log.trace("#{} - before unlocking component {} of type {}", methodName, componentId, componentType);
32             componentLocker.unlock(componentId, componentType);
33             log.trace("#{} - after unlocking component {} of type {}", methodName, componentId, componentType);
34         }
35     }
36
37
38 }