1 package org.jgroups.blocks; 2 3 import org.jgroups.ChannelException; 4 5 14 public class TwoPhaseVotingAdapter { 15 16 private final VotingAdapter voteChannel; 17 18 22 public TwoPhaseVotingAdapter(VotingAdapter voteChannel) { 23 this.voteChannel = voteChannel; 24 } 25 26 30 public void addListener(TwoPhaseVotingListener listener) { 31 voteChannel.addVoteListener(new TwoPhaseVoteWrapper(listener)); 32 } 33 34 37 public void removeListener(TwoPhaseVotingListener listener) { 38 voteChannel.removeVoteListener(new TwoPhaseVoteWrapper(listener)); 39 } 40 41 45 public boolean vote(Object decree, long timeout) throws ChannelException { 46 TwoPhaseWrapper wrappedDecree = new TwoPhaseWrapper(decree); 48 49 try { 51 if (voteChannel.vote(wrappedDecree, timeout / 3)) { 52 wrappedDecree.commit(); 53 54 if (!voteChannel.vote(wrappedDecree, timeout / 3)) { 56 wrappedDecree.abort(); 58 voteChannel.vote(wrappedDecree, timeout / 3); 59 return false; 60 } else 61 return true; 62 63 } else { 64 wrappedDecree.abort(); 66 voteChannel.vote(wrappedDecree, timeout / 3); 67 return false; 68 } 69 } catch(ChannelException chex) { 70 wrappedDecree.abort(); 71 voteChannel.vote(wrappedDecree, timeout / 3 ); 72 throw chex; 73 } 74 } 75 76 public static class TwoPhaseVoteWrapper implements VotingListener { 77 78 private final TwoPhaseVotingListener listener; 79 80 public TwoPhaseVoteWrapper(TwoPhaseVotingListener listener) { 81 this.listener = listener; 82 } 83 84 public boolean vote(Object decree) throws VoteException { 85 if (!(decree instanceof TwoPhaseWrapper)) 86 throw new VoteException("Not my type of decree. Ignore me."); 87 88 TwoPhaseWrapper wrapper = (TwoPhaseWrapper)decree; 89 90 if (wrapper.isPrepare()) 92 return listener.prepare(wrapper.getDecree()); 93 else 94 if (wrapper.isCommit()) 95 return listener.commit(wrapper.getDecree()); 96 else { 97 listener.abort(wrapper.getDecree()); 98 return false; 99 } 100 } 101 102 110 111 public int hashCode() { return listener.hashCode(); } 112 public boolean equals(Object other) { return listener.equals(other); } 113 } 114 115 118 public static class TwoPhaseWrapper implements java.io.Serializable { 119 private static final int PREPARE = 0; 120 private static final int COMMIT = 1; 121 private static final int ABORT = 2; 122 123 public TwoPhaseWrapper(Object decree) { 124 setDecree(decree); 125 setType(PREPARE); 126 } 127 128 private Object decree; 129 private int type; 130 131 public Object getDecree(){ return decree; } 132 public void setDecree(Object decree){ this.decree = decree; } 133 134 private int getType() { return type; } 135 private void setType(int type) { this.type = type; } 136 private boolean isType(int type) { return this.type == type; } 137 138 public boolean isPrepare() { return isType(PREPARE); } 139 public boolean isCommit() { return isType(COMMIT); } 140 public boolean isAbort() { return isType(ABORT); } 141 142 public void commit() { setType(COMMIT); } 143 public void abort() { setType(ABORT); } 144 145 public String toString() { return decree.toString(); } 146 } 147 } | Popular Tags |