001    /**
002     * Copyright (c) 2000-2013 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.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    }