001    /**
002     * Copyright (c) 2000-2012 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.bean.IdentifiableBean;
018    import com.liferay.portal.kernel.cluster.ClusterExecutorUtil;
019    import com.liferay.portal.kernel.cluster.ClusterRequest;
020    import com.liferay.portal.kernel.cluster.Clusterable;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.spring.aop.Swallowable;
024    import com.liferay.portal.kernel.util.MethodHandler;
025    import com.liferay.portal.spring.aop.AnnotationChainableMethodAdvice;
026    
027    import java.lang.annotation.Annotation;
028    import java.lang.reflect.Method;
029    
030    import org.aopalliance.intercept.MethodInvocation;
031    
032    /**
033     * @author Shuyang Zhou
034     */
035    public class ClusterableAdvice
036            extends AnnotationChainableMethodAdvice<Clusterable> {
037    
038            @Override
039            public void afterReturning(MethodInvocation methodInvocation, Object result)
040                    throws Throwable {
041    
042                    if (!ClusterInvokeThreadLocal.isEnabled()) {
043                            return;
044                    }
045    
046                    Clusterable clusterable = findAnnotation(methodInvocation);
047    
048                    if (clusterable == _nullClusterable) {
049                            return;
050                    }
051    
052                    Object thisObject = methodInvocation.getThis();
053    
054                    if (!(thisObject instanceof IdentifiableBean)) {
055                            _log.error(
056                                    "Not clustering calls for " + thisObject.getClass().getName() +
057                                            " because it does not implement " +
058                                                    IdentifiableBean.class.getName());
059    
060                            return;
061                    }
062    
063                    Method method = methodInvocation.getMethod();
064    
065                    MethodHandler methodHandler = new MethodHandler(
066                            method, methodInvocation.getArguments());
067    
068                    ClusterRequest clusterRequest = ClusterRequest.createMulticastRequest(
069                            methodHandler, true);
070    
071                    IdentifiableBean identifiableBean = (IdentifiableBean)thisObject;
072    
073                    clusterRequest.setBeanIdentifier(identifiableBean.getBeanIdentifier());
074    
075                    clusterRequest.setServletContextName(_servletContextName);
076    
077                    ClusterExecutorUtil.execute(clusterRequest);
078            }
079    
080            @Override
081            public boolean afterThrowing(
082                            MethodInvocation methodInvocation, Throwable throwable)
083                    throws Throwable {
084    
085                    if (!(throwable instanceof Swallowable)) {
086                            return true;
087                    }
088    
089                    Swallowable swallowable = (Swallowable)throwable;
090    
091                    if (swallowable.isSwallowable()) {
092                            return false;
093                    }
094                    else {
095                            return true;
096                    }
097            }
098    
099            @Override
100            public Clusterable getNullAnnotation() {
101                    return _nullClusterable;
102            }
103    
104            public void setServletContextName(String servletContextName) {
105                    _servletContextName = servletContextName;
106            }
107    
108            private static Log _log = LogFactoryUtil.getLog(ClusterableAdvice.class);
109    
110            private static Clusterable _nullClusterable =
111                    new Clusterable() {
112    
113                            public Class<? extends Annotation> annotationType() {
114                                    return Clusterable.class;
115                            }
116    
117                    };
118    
119            private String _servletContextName;
120    
121    }