001
014
015 package com.liferay.portal.service;
016
017 import com.liferay.portal.kernel.util.AutoResetThreadLocal;
018 import com.liferay.portal.spring.aop.ChainableMethodAdvice;
019
020 import java.util.LinkedList;
021
022 import org.aopalliance.intercept.MethodInvocation;
023
024
027 public class ServiceContextAdvice extends ChainableMethodAdvice {
028
029 @Override
030 public Object before(MethodInvocation methodInvocation) {
031 Object[] arguments = methodInvocation.getArguments();
032
033 LinkedList<Boolean> linkedList = _popServiceContext.get();
034
035 if (arguments != null) {
036 for (int i = arguments.length - 1; i >= 0; i--) {
037 if (arguments[i] instanceof ServiceContext) {
038 ServiceContext serviceContext =
039 (ServiceContext)arguments[i];
040
041 if (serviceContext == null) {
042 linkedList.push(false);
043 }
044 else {
045 ServiceContextThreadLocal.pushServiceContext(
046 serviceContext);
047
048 linkedList.push(true);
049 }
050
051 return null;
052 }
053 }
054 }
055
056 linkedList.push(false);
057
058 serviceBeanAopCacheManager.removeMethodInterceptor(
059 methodInvocation, this);
060
061 return null;
062 }
063
064 @Override
065 public void duringFinally(MethodInvocation methodInvocation) {
066 LinkedList<Boolean> linkedList = _popServiceContext.get();
067
068 if (linkedList.pop()) {
069 ServiceContextThreadLocal.popServiceContext();
070 }
071 }
072
073 private static final ThreadLocal<LinkedList<Boolean>> _popServiceContext =
074 new AutoResetThreadLocal<>(
075 ServiceContextAdvice.class.getName() + "._popServiceContext",
076 new LinkedList<Boolean>());
077
078 }