001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ha.framework.interfaces;
023:
024: import java.util.ArrayList;
025: import java.util.HashSet;
026:
027: /**
028: * Holder class that knows about a particular HA(sub)Partition i.e. member nodes,
029: * partition name and some utility functions.
030: *
031: * @see org.jboss.ha.hasessionstate.interfaces.HASessionState
032: * @see org.jboss.ha.hasessionstate.server.HASessionStateImpl
033: *
034: * @author <a href="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>
035: * @version $Revision: 57188 $
036: */
037: public class SubPartitionInfo implements Comparable, Cloneable,
038: java.io.Serializable {
039: // Constants -----------------------------------------------------
040: /** The serialVersionUID
041: * @since 1.2
042: */
043: private static final long serialVersionUID = -4116262958129610472L;
044:
045: // Attributes ----------------------------------------------------
046:
047: /**
048: * Name of the current sub-partition (will be used to create a JGroups group)
049: */
050: public String subPartitionName = null;
051:
052: /**
053: * When sub-partitions are merged, some names will disappear (eg. Merge G1 and G2 in G1)
054: * this structure remembers the removed named so that HAPartition can know which new group
055: * they should join
056: */
057: public HashSet subPartitionMergedNames = new HashSet();
058:
059: /**
060: * List of nodes part of this sub-partition
061: */
062: public ArrayList memberNodeNames = new ArrayList();
063:
064: private transient boolean newGroup = false;
065:
066: // Static --------------------------------------------------------
067:
068: // Constructors --------------------------------------------------
069:
070: public SubPartitionInfo() {
071: }
072:
073: public SubPartitionInfo(String partitionName, String[] members) {
074: super ();
075: this .subPartitionName = partitionName;
076: if (members != null)
077: for (int i = 0; i < members.length; i++)
078: this .memberNodeNames.add(members[i]);
079: }
080:
081: // Public --------------------------------------------------------
082:
083: public void setIsNewGroup() {
084: this .newGroup = true;
085: }
086:
087: public void merge(SubPartitionInfo merged) {
088: this .memberNodeNames.addAll(merged.memberNodeNames);
089: if (this .newGroup && !merged.newGroup)
090: this .subPartitionName = merged.subPartitionName;
091: else if (!merged.newGroup)
092: this .subPartitionMergedNames.add(merged.subPartitionName);
093:
094: if (!merged.newGroup)
095: this .subPartitionMergedNames.add(merged.subPartitionName);
096: this .subPartitionMergedNames
097: .addAll(merged.subPartitionMergedNames); // ? needed ?
098: merged.memberNodeNames.clear();
099: merged.subPartitionMergedNames.clear();
100: }
101:
102: public String toString() {
103: return subPartitionName + ":[" + memberNodeNames + "] aka '"
104: + subPartitionMergedNames + "'";
105: }
106:
107: public boolean actsForSubPartition(String subPartitionName) {
108: return (subPartitionName.equals(subPartitionName) || subPartitionMergedNames
109: .contains(subPartitionName));
110: }
111:
112: public boolean containsNode(String node) {
113: return memberNodeNames.contains(node);
114: }
115:
116: // Comparable implementation ----------------------------------------------
117:
118: /**
119: * "Note: this class has a natural ordering that is
120: * inconsistent with equals."
121: */
122: public int compareTo(Object o) {
123: int mySize = memberNodeNames.size();
124: int itsSize = ((SubPartitionInfo) o).memberNodeNames.size();
125:
126: if (mySize == itsSize)
127: return 0;
128: else if (mySize > itsSize)
129: return 1;
130: else
131: return -1;
132:
133: }
134:
135: // Cloneable implementation ----------------------------------------------
136:
137: public Object clone() {
138: SubPartitionInfo clonedObject = new SubPartitionInfo();
139: clonedObject.subPartitionName = this .subPartitionName;
140: clonedObject.memberNodeNames = (ArrayList) this .memberNodeNames
141: .clone();
142: clonedObject.subPartitionMergedNames = (HashSet) this.subPartitionMergedNames
143: .clone();
144:
145: return clonedObject;
146: }
147:
148: }
|