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.cluster;
016    
017    import com.liferay.portal.kernel.cluster.ClusterInvokeThreadLocal;
018    import com.liferay.portal.kernel.cluster.ClusterMasterExecutorUtil;
019    import com.liferay.portal.kernel.cluster.Clusterable;
020    import com.liferay.portal.kernel.cluster.ClusterableInvokerUtil;
021    import com.liferay.portal.kernel.cluster.NullClusterable;
022    import com.liferay.portal.spring.aop.AnnotationChainableMethodAdvice;
023    
024    import java.lang.reflect.Method;
025    
026    import org.aopalliance.intercept.MethodInvocation;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class ClusterableAdvice
032            extends AnnotationChainableMethodAdvice<Clusterable> {
033    
034            @Override
035            public void afterReturning(MethodInvocation methodInvocation, Object result)
036                    throws Throwable {
037    
038                    if (!ClusterInvokeThreadLocal.isEnabled()) {
039                            return;
040                    }
041    
042                    Clusterable clusterable = findAnnotation(methodInvocation);
043    
044                    if (clusterable == NullClusterable.NULL_CLUSTERABLE) {
045                            return;
046                    }
047    
048                    ClusterableInvokerUtil.invokeOnCluster(
049                            clusterable.acceptor(), methodInvocation.getThis(),
050                            methodInvocation.getMethod(), methodInvocation.getArguments());
051            }
052    
053            @Override
054            public Object before(MethodInvocation methodInvocation) throws Throwable {
055                    if (!ClusterInvokeThreadLocal.isEnabled()) {
056                            return null;
057                    }
058    
059                    Clusterable clusterable = findAnnotation(methodInvocation);
060    
061                    if (clusterable == NullClusterable.NULL_CLUSTERABLE) {
062                            return null;
063                    }
064    
065                    if (!clusterable.onMaster()) {
066                            return null;
067                    }
068    
069                    Object result = null;
070    
071                    if (ClusterMasterExecutorUtil.isMaster()) {
072                            result = methodInvocation.proceed();
073                    }
074                    else {
075                            result = ClusterableInvokerUtil.invokeOnMaster(
076                                    clusterable.acceptor(), methodInvocation.getThis(),
077                                    methodInvocation.getMethod(), methodInvocation.getArguments());
078                    }
079    
080                    Method method = methodInvocation.getMethod();
081    
082                    Class<?> returnType = method.getReturnType();
083    
084                    if (returnType == void.class) {
085                            result = nullResult;
086                    }
087    
088                    return result;
089            }
090    
091            @Override
092            public Clusterable getNullAnnotation() {
093                    return NullClusterable.NULL_CLUSTERABLE;
094            }
095    
096    }