1 56 57 package org.objectstyle.cayenne.event; 58 59 import java.util.Collection ; 60 import java.util.HashSet ; 61 import java.util.Iterator ; 62 import java.util.Map ; 63 import java.util.Set ; 64 import java.util.WeakHashMap ; 65 66 import org.apache.log4j.Logger; 67 import org.objectstyle.cayenne.event.EventManager.Dispatch; 68 import org.objectstyle.cayenne.util.Invocation; 69 70 80 class DispatchQueue { 81 private static Logger logObj = Logger.getLogger(DispatchQueue.class); 82 83 85 private Set subjectInvocations = new HashSet (); 86 private Map invocationsBySender = new WeakHashMap (); 87 88 92 synchronized void dispatchEvent(Dispatch dispatch) { 93 dispatchEvent(subjectInvocations, dispatch); 95 96 Object sender = dispatch.getSender(); 98 dispatchEvent(invocationsForSender(sender, false), dispatch); 99 } 100 101 synchronized void addInvocation(Invocation invocation, Object sender) { 102 if (sender == null) { 103 subjectInvocations.add(invocation); 104 } 105 else { 106 invocationsForSender(sender, true).add(invocation); 107 } 108 } 109 110 synchronized boolean removeInvocations(Object listener, Object sender) { 111 112 if (sender != null) { 114 return removeInvocations(invocationsForSender(sender, false), listener); 115 } 116 117 boolean didRemove = false; 118 119 didRemove = removeInvocations(subjectInvocations, listener); 121 122 Iterator sets = invocationsBySender.values().iterator(); 123 while (sets.hasNext()) { 124 Collection senderInvocations = (Collection ) sets.next(); 125 if (senderInvocations == null) { 126 continue; 127 } 128 129 Iterator it = senderInvocations.iterator(); 130 while (it.hasNext()) { 131 Invocation invocation = (Invocation) it.next(); 132 if (invocation.getTarget() == listener) { 133 it.remove(); 134 didRemove = true; 135 } 136 } 137 } 138 139 return didRemove; 140 } 141 142 private Collection invocationsForSender(Object sender, boolean create) { 143 Collection senderInvocations = (Collection ) invocationsBySender.get(sender); 144 if (create && senderInvocations == null) { 145 senderInvocations = new HashSet (); 146 invocationsBySender.put(sender, senderInvocations); 147 } 148 149 return senderInvocations; 150 } 151 152 private boolean removeInvocations(Collection invocations, Object listener) { 154 if (invocations == null || invocations.isEmpty()) { 155 return false; 156 } 157 158 boolean didRemove = false; 159 160 Iterator invocationsIt = invocations.iterator(); 161 while (invocationsIt.hasNext()) { 162 Invocation invocation = (Invocation) invocationsIt.next(); 163 if (invocation.getTarget() == listener) { 164 invocationsIt.remove(); 165 didRemove = true; 166 } 167 } 168 169 return didRemove; 170 } 171 172 private void dispatchEvent(Collection invocations, Dispatch dispatch) { 174 if (invocations == null || invocations.isEmpty()) { 175 return; 176 } 177 178 Iterator it = invocations.iterator(); 179 while (it.hasNext()) { 180 Invocation invocation = (Invocation) it.next(); 181 182 if (!dispatch.fire(invocation)) { 184 it.remove(); 185 logObj.debug( 186 "Failed invocation, removing: " + invocation.getMethod().getName()); 187 } 188 } 189 } 190 } 191 | Popular Tags |