KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > view > DestinationDotFileInterceptor


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 /**
31  *
32  * @version $Revision: $
33  */

34 public class DestinationDotFileInterceptor extends DotFileInterceptorSupport {
35
36     protected static final String JavaDoc ID_SEPARATOR = "_";
37
38     public DestinationDotFileInterceptor(Broker next, String JavaDoc file) {
39         super(next, file);
40     }
41
42     public Destination addDestination(ConnectionContext context, ActiveMQDestination destination) throws Exception JavaDoc {
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 JavaDoc {
50         super.removeDestination(context, destination, timeout);
51         generateFile();
52     }
53
54
55     protected void generateFile(PrintWriter JavaDoc writer) throws Exception JavaDoc {
56         ActiveMQDestination[] destinations = getDestinations();
57
58         // lets split into a tree
59
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         // now lets navigate the tree
67
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 JavaDoc writer, DestinationMapNode node, String JavaDoc prefix) {
101         String JavaDoc path = getPath(node);
102         writer.print(" ");
103         writer.print(prefix);
104         writer.print(ID_SEPARATOR);
105         writer.print(path);
106         String JavaDoc 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 JavaDoc children = node.getChildren();
118         for (Iterator JavaDoc 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 JavaDoc writer, DestinationMapNode node, String JavaDoc prefix) {
125         String JavaDoc path = getPath(node);
126         Collection JavaDoc children = node.getChildren();
127         for (Iterator JavaDoc 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 JavaDoc getPath(DestinationMapNode node) {
148         String JavaDoc path = node.getPath();
149         if (path.equals("*")) {
150             return "root";
151         }
152         return path;
153     }
154 }
155
Popular Tags