001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.catalina.tribes.group.interceptors;
018:
019: import org.apache.catalina.tribes.group.ChannelInterceptorBase;
020: import org.apache.catalina.tribes.Member;
021: import java.util.ArrayList;
022: import org.apache.catalina.tribes.group.AbsoluteOrder;
023: import org.apache.catalina.tribes.ChannelException;
024: import org.apache.catalina.tribes.Channel;
025:
026: public class StaticMembershipInterceptor extends ChannelInterceptorBase {
027: protected ArrayList members = new ArrayList();
028: protected Member localMember = null;
029:
030: public StaticMembershipInterceptor() {
031: super ();
032: }
033:
034: public void addStaticMember(Member member) {
035: synchronized (members) {
036: if (!members.contains(member))
037: members.add(member);
038: }
039: }
040:
041: public void removeStaticMember(Member member) {
042: synchronized (members) {
043: if (members.contains(member))
044: members.remove(member);
045: }
046: }
047:
048: public void setLocalMember(Member member) {
049: this .localMember = member;
050: }
051:
052: /**
053: * has members
054: */
055: public boolean hasMembers() {
056: return super .hasMembers() || (members.size() > 0);
057: }
058:
059: /**
060: * Get all current cluster members
061: * @return all members or empty array
062: */
063: public Member[] getMembers() {
064: if (members.size() == 0)
065: return super .getMembers();
066: else {
067: synchronized (members) {
068: Member[] others = super .getMembers();
069: Member[] result = new Member[members.size()
070: + others.length];
071: for (int i = 0; i < others.length; i++)
072: result[i] = others[i];
073: for (int i = 0; i < members.size(); i++)
074: result[i + others.length] = (Member) members.get(i);
075: AbsoluteOrder.absoluteOrder(result);
076: return result;
077: }//sync
078: }//end if
079: }
080:
081: /**
082: *
083: * @param mbr Member
084: * @return Member
085: */
086: public Member getMember(Member mbr) {
087: if (members.contains(mbr))
088: return (Member) members.get(members.indexOf(mbr));
089: else
090: return super .getMember(mbr);
091: }
092:
093: /**
094: * Return the member that represents this node.
095: *
096: * @return Member
097: */
098: public Member getLocalMember(boolean incAlive) {
099: if (this .localMember != null)
100: return localMember;
101: else
102: return super .getLocalMember(incAlive);
103: }
104:
105: /**
106: * Send notifications upwards
107: * @param svc int
108: * @throws ChannelException
109: */
110: public void start(int svc) throws ChannelException {
111: if ((Channel.SND_RX_SEQ & svc) == Channel.SND_RX_SEQ)
112: super .start(Channel.SND_RX_SEQ);
113: if ((Channel.SND_TX_SEQ & svc) == Channel.SND_TX_SEQ)
114: super .start(Channel.SND_TX_SEQ);
115: final Member[] mbrs = (Member[]) members
116: .toArray(new Member[members.size()]);
117: final ChannelInterceptorBase base = this ;
118: Thread t = new Thread() {
119: public void run() {
120: for (int i = 0; i < mbrs.length; i++) {
121: base.memberAdded(mbrs[i]);
122: }
123: }
124: };
125: t.start();
126: super
127: .start(svc & (~Channel.SND_RX_SEQ)
128: & (~Channel.SND_TX_SEQ));
129: }
130:
131: }
|