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