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