1 18 package org.apache.activemq.filter; 19 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Set ; 24 import java.util.SortedSet ; 25 import java.util.TreeSet ; 26 27 import org.apache.activemq.command.ActiveMQDestination; 28 29 42 public class DestinationMap { 43 protected static final String ANY_DESCENDENT = DestinationFilter.ANY_DESCENDENT; 44 protected static final String ANY_CHILD = DestinationFilter.ANY_CHILD; 45 46 private DestinationMapNode queueRootNode = new DestinationMapNode(null); 47 private DestinationMapNode topicRootNode = new DestinationMapNode(null); 48 49 60 public synchronized Set get(ActiveMQDestination key) { 61 if (key.isComposite()) { 62 ActiveMQDestination[] destinations = key.getCompositeDestinations(); 63 Set answer = new HashSet (destinations.length); 64 for (int i = 0; i < destinations.length; i++) { 65 ActiveMQDestination childDestination = destinations[i]; 66 Object value = get(childDestination); 67 if (value instanceof Set ) { 68 answer.addAll((Set ) value); 69 } 70 else if (value != null) { 71 answer.add(value); 72 } 73 } 74 return answer; 75 } 76 return findWildcardMatches(key); 77 } 78 79 public synchronized void put(ActiveMQDestination key, Object value) { 80 if (key.isComposite()) { 81 ActiveMQDestination[] destinations = key.getCompositeDestinations(); 82 for (int i = 0; i < destinations.length; i++) { 83 ActiveMQDestination childDestination = destinations[i]; 84 put(childDestination, value); 85 } 86 return; 87 } 88 String [] paths = key.getDestinationPaths(); 89 getRootNode(key).add(paths, 0, value); 90 } 91 92 95 public synchronized void remove(ActiveMQDestination key, Object value) { 96 if (key.isComposite()) { 97 ActiveMQDestination[] destinations = key.getCompositeDestinations(); 98 for (int i = 0; i < destinations.length; i++) { 99 ActiveMQDestination childDestination = destinations[i]; 100 remove(childDestination, value); 101 } 102 return; 103 } 104 String [] paths = key.getDestinationPaths(); 105 getRootNode(key).remove(paths, 0, value); 106 107 } 108 109 public int getTopicRootChildCount() { 110 return topicRootNode.getChildCount(); 111 } 112 113 public int getQueueRootChildCount() { 114 return queueRootNode.getChildCount(); 115 } 116 117 public DestinationMapNode getQueueRootNode() { 118 return queueRootNode; 119 } 120 121 public DestinationMapNode getTopicRootNode() { 122 return topicRootNode; 123 } 124 125 126 129 133 protected void setEntries(List entries) { 134 for (Iterator iter = entries.iterator(); iter.hasNext();) { 135 Object element = (Object ) iter.next(); 136 Class type = getEntryClass(); 137 if (type.isInstance(element)) { 138 DestinationMapEntry entry = (DestinationMapEntry) element; 139 put(entry.getDestination(), entry.getValue()); 140 } 141 else { 142 throw new IllegalArgumentException ("Each entry must be an instance of type: " + type.getName() + " but was: " + element); 143 } 144 } 145 } 146 147 153 protected Class getEntryClass() { 154 return DestinationMapEntry.class; 155 } 156 157 protected Set findWildcardMatches(ActiveMQDestination key) { 158 String [] paths = key.getDestinationPaths(); 159 Set answer = new HashSet (); 160 getRootNode(key).appendMatchingValues(answer, paths, 0); 161 return answer; 162 } 163 164 168 public Set removeAll(ActiveMQDestination key) { 169 Set rc = new HashSet (); 170 if (key.isComposite()) { 171 ActiveMQDestination[] destinations = key.getCompositeDestinations(); 172 for (int i = 0; i < destinations.length; i++) { 173 rc.add( removeAll(destinations[i]) ); 174 } 175 return rc; 176 } 177 String [] paths = key.getDestinationPaths(); 178 getRootNode(key).removeAll(rc, paths, 0); 179 return rc; 180 } 181 182 191 public Object chooseValue(ActiveMQDestination destination) { 192 Set set = get(destination); 193 if (set == null || set.isEmpty()) { 194 return null; 195 } 196 SortedSet sortedSet = new TreeSet (set); 197 return sortedSet.last(); 198 } 199 200 203 protected DestinationMapNode getRootNode(ActiveMQDestination key) { 204 if (key.isQueue()) { 205 return queueRootNode; 206 } 207 else { 208 return topicRootNode; 209 } 210 } 211 } 212 | Popular Tags |