1 17 package org.apache.activemq.broker.view; 18 19 import org.apache.activemq.broker.Broker; 20 import org.apache.activemq.broker.ConnectionContext; 21 import org.apache.activemq.broker.region.Destination; 22 import org.apache.activemq.command.ActiveMQDestination; 23 import org.apache.activemq.filter.DestinationMap; 24 import org.apache.activemq.filter.DestinationMapNode; 25 26 import java.io.PrintWriter ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 30 34 public class DestinationDotFileInterceptor extends DotFileInterceptorSupport { 35 36 protected static final String ID_SEPARATOR = "_"; 37 38 public DestinationDotFileInterceptor(Broker next, String file) { 39 super(next, file); 40 } 41 42 public Destination addDestination(ConnectionContext context, ActiveMQDestination destination) throws Exception { 43 Destination answer = super.addDestination(context, destination); 44 generateFile(); 45 return answer; 46 } 47 48 public void removeDestination(ConnectionContext context, ActiveMQDestination destination, long timeout) 49 throws Exception { 50 super.removeDestination(context, destination, timeout); 51 generateFile(); 52 } 53 54 55 protected void generateFile(PrintWriter writer) throws Exception { 56 ActiveMQDestination[] destinations = getDestinations(); 57 58 DestinationMap map = new DestinationMap(); 60 61 for (int i = 0; i < destinations.length; i++) { 62 ActiveMQDestination destination = destinations[i]; 63 map.put(destination, destination); 64 } 65 66 writer.println("digraph \"ActiveMQ Destinations\" {"); 68 writer.println(); 69 writer.println("node [style = \"rounded,filled\", fontname=\"Helvetica-Oblique\"];"); 70 writer.println(); 71 writer.println("topic_root [fillcolor = deepskyblue, label = \"Topics\" ];"); 72 writer.println("queue_root [fillcolor = deepskyblue, label = \"Queues\" ];"); 73 writer.println(); 74 75 writer.println("subgraph queues {"); 76 writer.println(" node [fillcolor=red]; "); 77 writer.println(" label = \"Queues\""); 78 writer.println(); 79 printNodeLinks(writer, map.getQueueRootNode(), "queue"); 80 writer.println("}"); 81 writer.println(); 82 83 writer.println("subgraph topics {"); 84 writer.println(" node [fillcolor=green]; "); 85 writer.println(" label = \"Topics\""); 86 writer.println(); 87 printNodeLinks(writer, map.getTopicRootNode(), "topic"); 88 writer.println("}"); 89 writer.println(); 90 91 printNodes(writer, map.getQueueRootNode(), "queue"); 92 writer.println(); 93 94 printNodes(writer, map.getTopicRootNode(), "topic"); 95 writer.println(); 96 97 writer.println("}"); 98 } 99 100 protected void printNodes(PrintWriter writer, DestinationMapNode node, String prefix) { 101 String path = getPath(node); 102 writer.print(" "); 103 writer.print(prefix); 104 writer.print(ID_SEPARATOR); 105 writer.print(path); 106 String label = path; 107 if (prefix.equals("topic")) { 108 label = "Topics"; 109 } 110 else if (prefix.equals("queue")) { 111 label = "Queues"; 112 } 113 writer.print("[ label = \""); 114 writer.print(label); 115 writer.println("\" ];"); 116 117 Collection children = node.getChildren(); 118 for (Iterator iter = children.iterator(); iter.hasNext();) { 119 DestinationMapNode child = (DestinationMapNode) iter.next(); 120 printNodes(writer, child, prefix + ID_SEPARATOR + path); 121 } 122 } 123 124 protected void printNodeLinks(PrintWriter writer, DestinationMapNode node, String prefix) { 125 String path = getPath(node); 126 Collection children = node.getChildren(); 127 for (Iterator iter = children.iterator(); iter.hasNext();) { 128 DestinationMapNode child = (DestinationMapNode) iter.next(); 129 130 writer.print(" "); 131 writer.print(prefix); 132 writer.print(ID_SEPARATOR); 133 writer.print(path); 134 writer.print(" -> "); 135 writer.print(prefix); 136 writer.print(ID_SEPARATOR); 137 writer.print(path); 138 writer.print(ID_SEPARATOR); 139 writer.print(getPath(child)); 140 writer.println(";"); 141 142 printNodeLinks(writer, child, prefix + ID_SEPARATOR + path); 143 } 144 } 145 146 147 protected String getPath(DestinationMapNode node) { 148 String path = node.getPath(); 149 if (path.equals("*")) { 150 return "root"; 151 } 152 return path; 153 } 154 } 155 | Popular Tags |