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.bean.IdentifiableBeanInvokerUtil;
018    import com.liferay.portal.kernel.cluster.ClusterInvokeAcceptor;
019    import com.liferay.portal.kernel.util.MethodHandler;
020    import com.liferay.portal.kernel.util.MethodKey;
021    
022    import java.io.Serializable;
023    
024    import java.lang.reflect.Constructor;
025    
026    import java.util.Map;
027    
028    import org.aopalliance.intercept.MethodInvocation;
029    
030    /**
031     * @author Shuyang Zhou
032     */
033    public class ClusterableInvokerUtil {
034    
035            public static MethodHandler createMethodHandler(
036                    Class<? extends ClusterInvokeAcceptor> clusterInvokeAcceptorClass,
037                    MethodInvocation methodInvocation) {
038    
039                    MethodHandler methodHandler =
040                            IdentifiableBeanInvokerUtil.createMethodHandler(methodInvocation);
041    
042                    Map<String, Serializable> context =
043                            ClusterableContextThreadLocal.collectThreadLocalContext();
044    
045                    if (clusterInvokeAcceptorClass == ClusterInvokeAcceptor.class) {
046                            clusterInvokeAcceptorClass = null;
047                    }
048    
049                    return new MethodHandler(
050                            _invokeMethodKey, methodHandler, clusterInvokeAcceptorClass,
051                            context);
052            }
053    
054            @SuppressWarnings("unused")
055            private static Object _invoke(
056                            MethodHandler methodHandler,
057                            Class<? extends ClusterInvokeAcceptor> clusterInvokeAcceptorClass,
058                            Map<String, Serializable> context)
059                    throws Exception {
060    
061                    if (clusterInvokeAcceptorClass != null) {
062                            Constructor<? extends ClusterInvokeAcceptor> constructor =
063                                    clusterInvokeAcceptorClass.getDeclaredConstructor();
064    
065                            if (!constructor.isAccessible()) {
066                                    constructor.setAccessible(true);
067                            }
068    
069                            ClusterInvokeAcceptor clusterInvokeAcceptor =
070                                    constructor.newInstance();
071    
072                            if (!clusterInvokeAcceptor.accept(context)) {
073                                    return null;
074                            }
075                    }
076    
077                    return methodHandler.invoke(false);
078            }
079    
080            private static MethodKey _invokeMethodKey = new MethodKey(
081                    ClusterableInvokerUtil.class, "_invoke", MethodHandler.class,
082                    Class.class, Map.class);
083    
084    }