001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
025     * @author Preston Crary
026     */
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    }