All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.cmi.lb.policy.AbstractPolicy Maven / Gradle / Ivy

There is a newer version: 2.2.6
Show newest version
/**
 * CMI : Cluster Method Invocation
 * Copyright (C) 2008 Bull S.A.S.
 * Contact: [email protected]
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 * --------------------------------------------------------------------------
 * $Id: AbstractPolicy.java 2122 2008-09-27 09:41:35Z loris $
 * --------------------------------------------------------------------------
 */

package org.ow2.cmi.lb.policy;

import java.lang.reflect.Method;
import java.util.Collection;

import org.ow2.cmi.controller.common.ClusterViewManager;
import org.ow2.cmi.lb.LoadBalanceable;
import org.ow2.cmi.lb.NoLoadBalanceableException;
import org.ow2.cmi.lb.decision.BasicDecisionManager;
import org.ow2.cmi.lb.decision.DecisionManager;
import org.ow2.cmi.lb.decision.DecisionUtil;
import org.ow2.cmi.lb.strategy.IStrategy;
import org.ow2.util.log.Log;
import org.ow2.util.log.LogFactory;

/**
 * Abstract implementation of a {@link IPolicy}, using the class {@link DecisionUtil} to take a decision and the class {@link BasicDecisionManager} to execute the decision.
 * @author Loris Bouzonnet
 * @param  The type of objects that are load-balanced
 * @see DecisionUtil
 * @see BasicDecisionManager
 */
public abstract class AbstractPolicy implements IPolicy {

    /**
     * Logger.
     */
    private static final Log LOGGER = LogFactory.getLog(AbstractPolicy.class);

    /**
     * The manager of the cluster view.
     */
    private ClusterViewManager clusterViewManager;

    /**
     * A strategy to modify the behavior of this policy.
     */
    private IStrategy strategy = null;

    /**
     * Default constructor.
     */
    public AbstractPolicy() {
        clusterViewManager = null;
    }

    /**
     * @param clusterViewManager the manager of the cluster view
     */
    public AbstractPolicy(final ClusterViewManager clusterViewManager) {
        this.clusterViewManager = clusterViewManager;
    }

    /**
     * Chooses a load-balanceable among the list of load-balanceables.
     * @param loadBalanceables a list of load-balanceables
     * @throws NoLoadBalanceableException if no server is available
     * @return the chosen load-balanceable
     */
    public abstract T choose(Collection loadBalanceables) throws NoLoadBalanceableException;

    /**
     * Returns a decision when an exception is thrown during an invocation for a given load-balanceable.
     * @param method the method that was invoked
     * @param parameters the parameters of the method
     * @param loadBalanceable the load-balanceable that have caused the exception
     * @param thr the exception that is thrown
     * @return the decision when an exception is thrown during an invocation for a given load-balanceable
     */
    public BasicDecisionManager onInvokeException(final Method method, final Object[] parameters,
            final T loadBalanceable, final Throwable thr) {
        if (DecisionUtil.mustFailoverOnInvoke(thr, clusterViewManager, loadBalanceable)) {
            return BasicDecisionManager.doRetry();
        }
        return BasicDecisionManager.doThrow(thr);
    }

    /**
     * Return a decision when a server is chosen and its delegate retrieved.
     * @param  the type of delegate
     * @param method the method that was invoked
     * @param parameters the parameters of the method
     * @param chosenValue the delegate of chosen server
     * @return the decision when the server is chosen and its delegate retrieved
     */
    public  DecisionManager onChoose(final Method method,
            final Object[] parameters, final ReturnType chosenValue) {
        return BasicDecisionManager.doReturn(chosenValue);
    }

    /**
     * Returns a decision when the invocation of a remote method ends.
     * @param  the type of the returned value
     * @param method the method that was invoked
     * @param parameters the parameters of the method
     * @param loadBalanceable the load-balanceable used for the invocation
     * @param retVal the returned value
     * @return the decision when the invocation of a remote method ends
     */
    public  BasicDecisionManager onReturn(final Method method, final Object[] parameters,
            final T loadBalanceable, final ReturnType retVal) {
        LOGGER.debug("onReturn: do nothing !");
        return BasicDecisionManager.doReturn(retVal);
    }

    /**
     * Returns a decision when an exception is thrown during an access to a registry for a given load-balanceable.
     * @param loadBalanceable the load-balanceable that have caused the exception
     * @param thr the exception that is thrown
     * @return the decision when an exception is thrown during an access to a registry for a given load-balanceable
     */
    public BasicDecisionManager onLookupException(final T loadBalanceable, final Throwable thr) {
        if (DecisionUtil.mustFailoverOnLookup(thr, clusterViewManager, loadBalanceable)) {
            return BasicDecisionManager.doRetry();
        }
        return BasicDecisionManager.doThrow(thr.getCause());
    }

    /**
     * Sets a strategy to modify the behavior of this policy.
     * @param strategy a strategy of load-balancing
     */
    public synchronized void setStrategy(final IStrategy strategy) {
        this.strategy = strategy;
    }

    /**
     * Return a strategy to modify the behavior of this policy.
     * @return a strategy to modify the behavior of this policy
     */
    public IStrategy getStrategy() {
        return strategy;
    }

    /**
     * Set the manager of the cluster view.
     * @param clusterViewManager the manager of the cluster view
     */
    public void setClusterViewManager(final ClusterViewManager clusterViewManager) {
        this.clusterViewManager = clusterViewManager;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy