001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.cluster;
016    
017    import com.liferay.portal.kernel.cluster.Clusterable;
018    import com.liferay.portal.kernel.nio.intraband.rpc.IntrabandRPCUtil;
019    import com.liferay.portal.kernel.resiliency.spi.SPI;
020    import com.liferay.portal.kernel.resiliency.spi.SPIUtil;
021    import com.liferay.portal.spring.aop.AnnotationChainableMethodAdvice;
022    
023    import java.io.Serializable;
024    
025    import java.lang.reflect.Method;
026    
027    import java.util.concurrent.Future;
028    
029    import org.aopalliance.intercept.MethodInvocation;
030    
031    /**
032     * @author Shuyang Zhou
033     */
034    public class SPIClusterableAdvice
035            extends AnnotationChainableMethodAdvice<Clusterable> {
036    
037            @Override
038            public void afterReturning(MethodInvocation methodInvocation, Object result)
039                    throws Throwable {
040    
041                    Clusterable clusterable = findAnnotation(methodInvocation);
042    
043                    if (clusterable == NullClusterable.NULL_CLUSTERABLE) {
044                            return;
045                    }
046    
047                    SPI spi = SPIUtil.getSPI();
048    
049                    IntrabandRPCUtil.execute(
050                            spi.getRegistrationReference(),
051                            new MethodHandlerProcessCallable<Serializable>(
052                                    ClusterableInvokerUtil.createMethodHandler(
053                                            clusterable.acceptor(), methodInvocation)));
054            }
055    
056            @Override
057            public Object before(MethodInvocation methodInvocation) throws Throwable {
058                    Clusterable clusterable = findAnnotation(methodInvocation);
059    
060                    if (clusterable == NullClusterable.NULL_CLUSTERABLE) {
061                            return null;
062                    }
063    
064                    if (!clusterable.onMaster()) {
065                            return null;
066                    }
067    
068                    SPI spi = SPIUtil.getSPI();
069    
070                    Future<Serializable> futureResult = IntrabandRPCUtil.execute(
071                            spi.getRegistrationReference(),
072                            new MethodHandlerProcessCallable<Serializable>(
073                                    ClusterableInvokerUtil.createMethodHandler(
074                                            clusterable.acceptor(), methodInvocation)));
075    
076                    Object result = futureResult.get();
077    
078                    Method method = methodInvocation.getMethod();
079    
080                    Class<?> returnType = method.getReturnType();
081    
082                    if (returnType == void.class) {
083                            result = nullResult;
084                    }
085    
086                    return result;
087            }
088    
089            @Override
090            public Clusterable getNullAnnotation() {
091                    return NullClusterable.NULL_CLUSTERABLE;
092            }
093    
094    }