FileDocCategorySizeDatePackage
HASingletonElectionPolicyBase.javaAPI DocJBoss 4.2.13125Fri Jul 13 20:52:36 BST 2007org.jboss.ha.singleton

HASingletonElectionPolicyBase.java

/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This 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 software 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 software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.ha.singleton;


import org.jboss.ha.framework.interfaces.HAPartition;
import org.jboss.ha.framework.server.ClusterPartition;
import org.jboss.logging.Logger;
import org.jboss.system.ServiceMBeanSupport;

/**
 * A base class for policy service that decides which node in the cluster should be 
 * the master node to run certain HASingleton service.
 * 
 * @author <a href="mailto:afu@novell.com">Alex Fu</a>
 * @version $Revision: 46010 $
 */
public abstract class HASingletonElectionPolicyBase 
   extends ServiceMBeanSupport
   implements HASingletonElectionPolicyMBean
{
   private Object mManagedSingleton;
   private HAPartition mPartition;
   
   /**
    * @see HASingletonElectionPolicyMBean#setManagedSingleton(Object)
    */
   public void setManagedSingleton(Object singleton)
   {
      this.mManagedSingleton = singleton;
   }

   /**
    * @see HASingletonElectionPolicyMBean#getManagedSingleton()
    */
   public Object getManagedSingleton()
   {
      return this.mManagedSingleton;
   }

   /**
    * @see HASingletonElectionPolicyMBean#setHAPartition(HAPartition)
    */
   public void setHAPartition(HAPartition partition)
   {
      this.mPartition = partition;
   }

   /**
    * @see HASingletonElectionPolicyMBean#getHAPartition()
    */
   public HAPartition getHAPartition()
   {
      return this.mPartition;
   }
   
   /**
    * @see HASingletonElectionPolicyMBean#isElectedMaster()
    */
   public boolean isElectedMaster()
   {
      if (null == this.mPartition)
         throw new IllegalStateException("HAPartition is not set");
      
      return pickSingleton().equals(this.mPartition.getClusterNode());
   }
    
   /**
    * @see HASingletonElectionPolicyMBean#isElectedMaster(HAPartition)
    */
   public boolean isElectedMaster(HAPartition partition) 
   {
      if (null == partition)
         throw new IllegalStateException("parameter cannot be null");

      return pickSingleton(partition).equals(partition.getClusterNode());
   }
}