KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collections JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * Default implementation of FamilyClusterInfo
30  *
31  * @see org.jboss.ha.framework.interfaces.FamilyClusterInfo
32  * @see org.jboss.ha.framework.interfaces.ClusteringTargetsRepository
33  *
34  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
35  * @version $Revision: 56227 $
36  */

37 public class FamilyClusterInfoImpl implements FamilyClusterInfo
38 {
39    
40    // Constants -----------------------------------------------------
41

42    // Attributes ----------------------------------------------------
43

44    public String JavaDoc familyName = null;
45    ArrayList JavaDoc targets = null;
46    long currentViewId = 0;
47    boolean isViewMembersInSyncWithViewId = false;
48    
49    int cursor = FamilyClusterInfo.UNINITIALIZED_CURSOR;
50    Object JavaDoc arbitraryObject = null;
51       
52    // Static --------------------------------------------------------
53

54    // Constructors --------------------------------------------------
55

56    private FamilyClusterInfoImpl (){ }
57    
58    protected FamilyClusterInfoImpl (String JavaDoc familyName, List JavaDoc targets, long viewId)
59    {
60       this.familyName = familyName;
61       this.targets = cloneList(targets);
62       this.currentViewId = viewId;
63       
64       this.isViewMembersInSyncWithViewId = false;
65    }
66
67    // Public --------------------------------------------------------
68

69    // FamilyClusterInfo implementation ----------------------------------------------
70

71    public String JavaDoc getFamilyName () { return this.familyName; }
72    
73    /**
74     * Returns an unmodifiable view of the target list.
75     *
76     * @see Collections#unmodifiableList(List)
77     */

78    public synchronized List JavaDoc getTargets ()
79    {
80       return Collections.unmodifiableList(this.targets);
81    }
82    public long getCurrentViewId () { return this.currentViewId; }
83    public int getCursor () { return this.cursor; }
84    public int setCursor (int cursor) { return (this.cursor = cursor);}
85    public Object JavaDoc getObject () { return this.arbitraryObject; }
86    public Object JavaDoc setObject (Object JavaDoc whatever) { this.arbitraryObject = whatever; return this.arbitraryObject; }
87    
88    public List JavaDoc removeDeadTarget(Object JavaDoc target)
89    {
90       synchronized (this)
91       {
92          ArrayList JavaDoc tmp = (ArrayList JavaDoc) targets.clone();
93          tmp.remove (target);
94          this.targets = tmp;
95          this.isViewMembersInSyncWithViewId = false;
96          return Collections.unmodifiableList(this.targets);
97       }
98    }
99    
100    public List JavaDoc updateClusterInfo (List JavaDoc targets, long viewId)
101    {
102       synchronized (this)
103       {
104          this.targets = cloneList(targets);
105          this.currentViewId = viewId;
106          this.isViewMembersInSyncWithViewId = true;
107          return Collections.unmodifiableList(this.targets);
108       }
109    }
110       
111    public boolean currentMembershipInSyncWithViewId ()
112    {
113       return this.isViewMembersInSyncWithViewId;
114    }
115    
116    public void resetView ()
117    {
118       synchronized (this)
119       {
120          this.currentViewId = -1;
121          this.isViewMembersInSyncWithViewId = false;
122       }
123    }
124       
125    // Object overrides ---------------------------------------------------
126

127    public int hashCode()
128    {
129       return this.familyName.hashCode ();
130    }
131    
132    public boolean equals (Object JavaDoc o)
133    {
134       if (o instanceof FamilyClusterInfoImpl)
135       {
136          FamilyClusterInfoImpl fr = (FamilyClusterInfoImpl)o;
137          return fr.familyName == this.familyName;
138       }
139       else
140          return false;
141    }
142
143    public String JavaDoc toString()
144    {
145       StringBuffer JavaDoc tmp = new StringBuffer JavaDoc(super.toString());
146       tmp.append("{familyName=");
147       tmp.append(familyName);
148       tmp.append(",targets=");
149       tmp.append(targets);
150       tmp.append(",currentViewId=");
151       tmp.append(currentViewId);
152       tmp.append(",isViewMembersInSyncWithViewId=");
153       tmp.append(isViewMembersInSyncWithViewId);
154       tmp.append(",cursor=");
155       tmp.append(cursor);
156       tmp.append(",arbitraryObject=");
157       tmp.append(arbitraryObject);
158       tmp.append("}");
159       return tmp.toString();
160    }
161    // Package protected ---------------------------------------------
162

163    // Protected -----------------------------------------------------
164

165    // Private -------------------------------------------------------
166

167    // Inner classes -------------------------------------------------
168

169    private static ArrayList JavaDoc cloneList(List JavaDoc toClone)
170    {
171       if (toClone instanceof ArrayList JavaDoc)
172       {
173          synchronized (toClone)
174          {
175             return (ArrayList JavaDoc) ((ArrayList JavaDoc) toClone).clone();
176          }
177       }
178       
179       ArrayList JavaDoc clone = new ArrayList JavaDoc(toClone.size());
180       synchronized (toClone)
181       {
182         clone.addAll(toClone);
183       }
184       return clone;
185    }
186 }
187
Popular Tags