KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ha > framework > interfaces > SubPartitionInfo


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ha.framework.interfaces;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashSet JavaDoc;
26
27 /**
28  * Holder class that knows about a particular HA(sub)Partition i.e. member nodes,
29  * partition name and some utility functions.
30  *
31  * @see org.jboss.ha.hasessionstate.interfaces.HASessionState
32  * @see org.jboss.ha.hasessionstate.server.HASessionStateImpl
33  *
34  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>
35  * @version $Revision: 37459 $
36  */

37 public class SubPartitionInfo
38    implements Comparable JavaDoc, Cloneable JavaDoc, java.io.Serializable JavaDoc
39 {
40    // Constants -----------------------------------------------------
41
/** The serialVersionUID
42     * @since 1.2
43     */

44    private static final long serialVersionUID = -4116262958129610472L;
45
46    // Attributes ----------------------------------------------------
47

48    /**
49     * Name of the current sub-partition (will be used to create a JGroups group)
50     */

51    public String JavaDoc subPartitionName = null;
52    
53    /**
54     * When sub-partitions are merged, some names will disappear (eg. Merge G1 and G2 in G1)
55     * this structure remembers the removed named so that HAPartition can know which new group
56     * they should join
57     */

58    public HashSet JavaDoc subPartitionMergedNames = new HashSet JavaDoc ();
59    
60    /**
61     * List of nodes part of this sub-partition
62     */

63    public ArrayList JavaDoc memberNodeNames = new ArrayList JavaDoc ();
64
65    private transient boolean newGroup = false;
66
67    // Static --------------------------------------------------------
68

69    // Constructors --------------------------------------------------
70

71    public SubPartitionInfo () {}
72    
73    public SubPartitionInfo (String JavaDoc partitionName, String JavaDoc[] members)
74    {
75       super ();
76       this.subPartitionName = partitionName;
77       if (members != null)
78          for (int i=0; i<members.length; i++)
79             this.memberNodeNames.add (members[i]);
80    }
81    
82    // Public --------------------------------------------------------
83

84    public void setIsNewGroup ()
85    {
86       this.newGroup = true;
87    }
88    
89    public void merge (SubPartitionInfo merged)
90    {
91       this.memberNodeNames.addAll (merged.memberNodeNames);
92       if (this.newGroup && !merged.newGroup)
93          this.subPartitionName = merged.subPartitionName;
94       else if (!merged.newGroup)
95          this.subPartitionMergedNames.add (merged.subPartitionName);
96       
97       
98       if (!merged.newGroup)
99          this.subPartitionMergedNames.add (merged.subPartitionName);
100       this.subPartitionMergedNames.addAll (merged.subPartitionMergedNames); // ? needed ?
101
merged.memberNodeNames.clear ();
102       merged.subPartitionMergedNames.clear ();
103    }
104    
105    public String JavaDoc toString ()
106    {
107       return subPartitionName + ":[" + memberNodeNames + "] aka '" + subPartitionMergedNames + "'";
108    }
109    
110    public boolean actsForSubPartition (String JavaDoc subPartitionName)
111    {
112       return (subPartitionName.equals (subPartitionName) || subPartitionMergedNames.contains (subPartitionName));
113    }
114    
115    public boolean containsNode (String JavaDoc node)
116    {
117       return memberNodeNames.contains (node);
118    }
119
120    // Comparable implementation ----------------------------------------------
121

122    /**
123     * "Note: this class has a natural ordering that is
124     * inconsistent with equals."
125     */

126    public int compareTo (Object JavaDoc o)
127    {
128       int mySize = memberNodeNames.size ();
129       int itsSize = ((SubPartitionInfo)o).memberNodeNames.size ();
130       
131       if (mySize==itsSize)
132          return 0;
133       else if (mySize > itsSize)
134          return 1;
135       else
136          return -1;
137       
138    }
139    
140    // Cloneable implementation ----------------------------------------------
141

142    public Object JavaDoc clone ()
143    {
144       SubPartitionInfo clonedObject = new SubPartitionInfo ();
145       clonedObject.subPartitionName = this.subPartitionName;
146       clonedObject.memberNodeNames = (ArrayList JavaDoc)this.memberNodeNames.clone ();
147       clonedObject.subPartitionMergedNames = (HashSet JavaDoc)this.subPartitionMergedNames.clone ();
148       
149       return clonedObject;
150    }
151    
152 }
153
Popular Tags