1 17 package org.apache.activemq.broker.view; 18 19 import java.io.PrintWriter ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import javax.management.MBeanServer ; 28 import javax.management.MBeanServerInvocationHandler ; 29 import javax.management.MalformedObjectNameException ; 30 import javax.management.ObjectName ; 31 32 import org.apache.activemq.broker.Broker; 33 import org.apache.activemq.broker.ConnectionContext; 34 import org.apache.activemq.broker.ProducerBrokerExchange; 35 import org.apache.activemq.broker.jmx.BrokerViewMBean; 36 import org.apache.activemq.broker.jmx.ManagementContext; 37 import org.apache.activemq.broker.jmx.SubscriptionViewMBean; 38 import org.apache.activemq.broker.region.Subscription; 39 import org.apache.activemq.command.ActiveMQDestination; 40 import org.apache.activemq.command.ConsumerInfo; 41 import org.apache.activemq.command.Message; 42 import org.apache.activemq.command.ProducerId; 43 import org.apache.activemq.command.ProducerInfo; 44 import org.apache.activemq.filter.DestinationMapNode; 45 46 50 public class ConnectionDotFileInterceptor extends DotFileInterceptorSupport { 51 52 protected static final String ID_SEPARATOR = "_"; 53 54 private final boolean redrawOnRemove; 55 private boolean clearProducerCacheAfterRender = false; 56 private String domain = "org.apache.activemq"; 57 private BrokerViewMBean brokerView; 58 private MBeanServer mbeanServer; 59 60 private Map producers = new HashMap (); 62 private Map producerDestinations = new HashMap (); 63 private Object lock = new Object (); 64 65 public ConnectionDotFileInterceptor(Broker next, String file, boolean redrawOnRemove) 66 throws MalformedObjectNameException { 67 super(next, file); 68 this.redrawOnRemove = redrawOnRemove; 69 70 mbeanServer = new ManagementContext().getMBeanServer(); 71 ObjectName brokerName = new ObjectName (domain + ":Type=Broker,BrokerName=localhost"); 72 brokerView = (BrokerViewMBean) MBeanServerInvocationHandler.newProxyInstance(mbeanServer, brokerName, 73 BrokerViewMBean.class, true); 74 } 75 76 public Subscription addConsumer(ConnectionContext context, ConsumerInfo info) throws Exception { 77 Subscription answer = super.addConsumer(context, info); 78 generateFile(); 79 return answer; 80 } 81 82 public void addProducer(ConnectionContext context, ProducerInfo info) throws Exception { 83 super.addProducer(context, info); 84 ProducerId producerId = info.getProducerId(); 85 synchronized (lock) { 86 producers.put(producerId, info); 87 } 88 generateFile(); 89 } 90 91 public void removeConsumer(ConnectionContext context, ConsumerInfo info) throws Exception { 92 super.removeConsumer(context, info); 93 if (redrawOnRemove) { 94 generateFile(); 95 } 96 } 97 98 public void removeProducer(ConnectionContext context, ProducerInfo info) throws Exception { 99 super.removeProducer(context, info); 100 ProducerId producerId = info.getProducerId(); 101 if (redrawOnRemove) { 102 synchronized (lock) { 103 producerDestinations.remove(producerId); 104 producers.remove(producerId); 105 } 106 generateFile(); 107 } 108 } 109 110 public void send(ProducerBrokerExchange producerExchange, Message messageSend) throws Exception { 111 super.send(producerExchange, messageSend); 112 ProducerId producerId = messageSend.getProducerId(); 113 ActiveMQDestination destination = messageSend.getDestination(); 114 synchronized (lock) { 115 Set destinations = (Set ) producerDestinations.get(producerId); 116 if (destinations == null) { 117 destinations = new HashSet (); 118 } 119 producerDestinations.put(producerId, destinations); 120 destinations.add(destination); 121 } 122 } 123 124 protected void generateFile(PrintWriter writer) throws Exception { 125 126 writer.println("digraph \"ActiveMQ Connections\" {"); 127 writer.println(); 128 writer.println("label=\"ActiveMQ Broker: " + brokerView.getBrokerId() + "\"];"); 129 writer.println(); 130 writer.println("node [style = \"rounded,filled\", fillcolor = yellow, fontname=\"Helvetica-Oblique\"];"); 131 writer.println(); 132 133 Map clients = new HashMap (); 134 Map queues = new HashMap (); 135 Map topics = new HashMap (); 136 137 printSubscribers(writer, clients, queues, "queue_", brokerView.getQueueSubscribers()); 138 writer.println(); 139 140 printSubscribers(writer, clients, topics, "topic_", brokerView.getTopicSubscribers()); 141 writer.println(); 142 143 printProducers(writer, clients, queues, topics); 144 writer.println(); 145 146 writeLabels(writer, "green", "Client: ", clients); 147 writer.println(); 148 149 writeLabels(writer, "red", "Queue: ", queues); 150 writeLabels(writer, "blue", "Topic: ", topics); 151 writer.println("}"); 152 153 if (clearProducerCacheAfterRender) { 154 producerDestinations.clear(); 155 } 156 } 157 158 protected void printProducers(PrintWriter writer, Map clients, Map queues, Map topics) { 159 synchronized(lock) { 160 for (Iterator iter = producerDestinations.entrySet().iterator(); iter.hasNext();) { 161 Map.Entry entry = (Map.Entry ) iter.next(); 162 ProducerId producerId = (ProducerId) entry.getKey(); 163 Set destinationSet = (Set ) entry.getValue(); 164 printProducers(writer, clients, queues, topics, producerId, destinationSet); 165 } 166 } 167 } 168 169 protected void printProducers(PrintWriter writer, Map clients, Map queues, Map topics, ProducerId producerId, Set destinationSet) { 170 for (Iterator iter = destinationSet.iterator(); iter.hasNext();) { 171 ActiveMQDestination destination = (ActiveMQDestination) iter.next(); 172 173 String clientId = producerId.getConnectionId(); 175 String safeClientId = asID(clientId); 176 clients.put(safeClientId, clientId); 177 178 String physicalName = destination.getPhysicalName(); 179 String safeDestinationId = asID(physicalName); 180 if (destination.isTopic()) { 181 safeDestinationId = "topic_" + safeDestinationId; 182 topics.put(safeDestinationId, physicalName); 183 } 184 else { 185 safeDestinationId = "queue_" + safeDestinationId; 186 queues.put(safeDestinationId, physicalName); 187 } 188 189 String safeProducerId = asID(producerId.toString()); 190 191 193 writer.print(safeClientId); 194 writer.print(" -> "); 195 writer.print(safeProducerId); 196 writer.println(";"); 197 198 writer.print(safeProducerId); 199 writer.print(" -> "); 200 writer.print(safeDestinationId); 201 writer.println(";"); 202 203 writer.print(safeProducerId); 205 writer.print(" [label = \""); 206 String label = "Producer: " + producerId.getSessionId() + "-" + producerId.getValue(); 207 writer.print(label); 208 writer.println("\"];"); 209 210 } 211 } 212 213 214 protected void printSubscribers(PrintWriter writer, Map clients, Map destinations, String type, 215 ObjectName [] subscribers) { 216 for (int i = 0; i < subscribers.length; i++) { 217 ObjectName name = subscribers[i]; 218 SubscriptionViewMBean subscriber = (SubscriptionViewMBean) MBeanServerInvocationHandler.newProxyInstance( 219 mbeanServer, name, SubscriptionViewMBean.class, true); 220 221 String clientId = subscriber.getClientId(); 222 String safeClientId = asID(clientId); 223 clients.put(safeClientId, clientId); 224 225 String destination = subscriber.getDestinationName(); 226 String safeDestinationId = type + asID(destination); 227 destinations.put(safeDestinationId, destination); 228 229 String selector = subscriber.getSelector(); 230 231 String subscriberId = safeClientId + "_" + subscriber.getSessionId() + "_" + subscriber.getSubcriptionId(); 233 234 writer.print(subscriberId); 235 writer.print(" -> "); 236 writer.print(safeClientId); 237 writer.println(";"); 238 239 writer.print(safeDestinationId); 240 writer.print(" -> "); 241 writer.print(subscriberId); 242 writer.println(";"); 243 244 writer.print(subscriberId); 246 writer.print(" [label = \""); 247 String label = "Subscription: " + subscriber.getSessionId() + "-" + subscriber.getSubcriptionId(); 248 if (selector != null && selector.length() > 0) { 249 label = label + "\\nSelector: " + selector; 250 } 251 writer.print(label); 252 writer.println("\"];"); 253 } 254 } 255 256 protected void writeLabels(PrintWriter writer, String color, String prefix, Map map) { 257 for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) { 258 Map.Entry entry = (Map.Entry ) iter.next(); 259 String id = (String ) entry.getKey(); 260 String label = (String ) entry.getValue(); 261 262 writer.print(id); 263 writer.print(" [ fillcolor = "); 264 writer.print(color); 265 writer.print(", label = \""); 266 writer.print(prefix); 267 writer.print(label); 268 writer.println("\"];"); 269 } 270 } 271 272 275 protected String asID(String name) { 276 StringBuffer buffer = new StringBuffer (); 277 for (int i = 0, size = name.length(); i < size; i++) { 278 char ch = name.charAt(i); 279 if (Character.isLetterOrDigit(ch) || ch == '_') { 280 buffer.append(ch); 281 } 282 else { 283 buffer.append('_'); 284 } 285 } 286 return buffer.toString(); 287 } 288 289 protected void printNodes(PrintWriter writer, DestinationMapNode node, String prefix) { 290 String path = getPath(node); 291 writer.print(" "); 292 writer.print(prefix); 293 writer.print(ID_SEPARATOR); 294 writer.print(path); 295 String label = path; 296 if (prefix.equals("topic")) { 297 label = "Topics"; 298 } 299 else if (prefix.equals("queue")) { 300 label = "Queues"; 301 } 302 writer.print("[ label = \""); 303 writer.print(label); 304 writer.println("\" ];"); 305 306 Collection children = node.getChildren(); 307 for (Iterator iter = children.iterator(); iter.hasNext();) { 308 DestinationMapNode child = (DestinationMapNode) iter.next(); 309 printNodes(writer, child, prefix + ID_SEPARATOR + path); 310 } 311 } 312 313 protected void printNodeLinks(PrintWriter writer, DestinationMapNode node, String prefix) { 314 String path = getPath(node); 315 Collection children = node.getChildren(); 316 for (Iterator iter = children.iterator(); iter.hasNext();) { 317 DestinationMapNode child = (DestinationMapNode) iter.next(); 318 319 writer.print(" "); 320 writer.print(prefix); 321 writer.print(ID_SEPARATOR); 322 writer.print(path); 323 writer.print(" -> "); 324 writer.print(prefix); 325 writer.print(ID_SEPARATOR); 326 writer.print(path); 327 writer.print(ID_SEPARATOR); 328 writer.print(getPath(child)); 329 writer.println(";"); 330 331 printNodeLinks(writer, child, prefix + ID_SEPARATOR + path); 332 } 333 } 334 335 protected String getPath(DestinationMapNode node) { 336 String path = node.getPath(); 337 if (path.equals("*")) { 338 return "root"; 339 } 340 return path; 341 } 342 } 343 | Popular Tags |