001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.distributable;
022:
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.Set;
026:
027: import org.jgroups.Address;
028: import org.jgroups.Channel;
029: import org.jgroups.MembershipListener;
030: import org.jgroups.View;
031:
032: /**
033: * @author Paul Ferraro
034: */
035: public abstract class AbstractMembershipListener implements
036: MembershipListener {
037: protected Channel channel;
038: private Set<Address> addressSet = new HashSet<Address>();
039:
040: protected AbstractMembershipListener(Channel channel) {
041: this .channel = channel;
042:
043: this .channel.setOpt(Channel.LOCAL, false);
044: }
045:
046: /**
047: * @see org.jgroups.MembershipListener#block()
048: */
049: @Override
050: public final void block() {
051: // Do nothing
052: }
053:
054: /**
055: * @see org.jgroups.MembershipListener#suspect(org.jgroups.Address)
056: */
057: @Override
058: public final void suspect(Address address) {
059: // Do nothing
060: }
061:
062: /**
063: * @see org.jgroups.MembershipListener#viewAccepted(org.jgroups.View)
064: */
065: @Override
066: public final void viewAccepted(View view) {
067: synchronized (this .addressSet) {
068: Iterator<Address> addresses = this .addressSet.iterator();
069:
070: while (addresses.hasNext()) {
071: Address address = addresses.next();
072:
073: if (!view.containsMember(address)) {
074: addresses.remove();
075:
076: this .memberLeft(address);
077: }
078: }
079:
080: Address localAddress = this .channel.getLocalAddress();
081:
082: for (Address address : view.getMembers()) {
083: // Track remote members only
084: if (!address.equals(localAddress)
085: && this .addressSet.add(address)) {
086: this .memberJoined(address);
087: }
088: }
089: }
090: }
091:
092: public int getMembershipSize() {
093: synchronized (this .addressSet) {
094: return this .addressSet.size();
095: }
096: }
097:
098: public boolean isMembershipEmpty() {
099: synchronized (this .addressSet) {
100: return this .addressSet.isEmpty();
101: }
102: }
103:
104: protected abstract void memberJoined(Address address);
105:
106: protected abstract void memberLeft(Address address);
107: }
|