KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.PrintWriter JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.MBeanServerInvocationHandler JavaDoc;
29 import javax.management.MalformedObjectNameException JavaDoc;
30 import javax.management.ObjectName JavaDoc;
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 /**
47  *
48  * @version $Revision: $
49  */

50 public class ConnectionDotFileInterceptor extends DotFileInterceptorSupport {
51
52     protected static final String JavaDoc ID_SEPARATOR = "_";
53
54     private final boolean redrawOnRemove;
55     private boolean clearProducerCacheAfterRender = false;
56     private String JavaDoc domain = "org.apache.activemq";
57     private BrokerViewMBean brokerView;
58     private MBeanServer JavaDoc mbeanServer;
59
60     // until we have some MBeans for producers, lets do it all ourselves
61
private Map JavaDoc producers = new HashMap JavaDoc();
62     private Map JavaDoc producerDestinations = new HashMap JavaDoc();
63     private Object JavaDoc lock = new Object JavaDoc();
64
65     public ConnectionDotFileInterceptor(Broker next, String JavaDoc file, boolean redrawOnRemove)
66             throws MalformedObjectNameException JavaDoc {
67         super(next, file);
68         this.redrawOnRemove = redrawOnRemove;
69
70         mbeanServer = new ManagementContext().getMBeanServer();
71         ObjectName JavaDoc brokerName = new ObjectName JavaDoc(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 JavaDoc {
77         Subscription answer = super.addConsumer(context, info);
78         generateFile();
79         return answer;
80     }
81
82     public void addProducer(ConnectionContext context, ProducerInfo info) throws Exception JavaDoc {
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 JavaDoc {
92         super.removeConsumer(context, info);
93         if (redrawOnRemove) {
94             generateFile();
95         }
96     }
97
98     public void removeProducer(ConnectionContext context, ProducerInfo info) throws Exception JavaDoc {
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 JavaDoc {
111         super.send(producerExchange, messageSend);
112         ProducerId producerId = messageSend.getProducerId();
113         ActiveMQDestination destination = messageSend.getDestination();
114         synchronized (lock) {
115             Set JavaDoc destinations = (Set JavaDoc) producerDestinations.get(producerId);
116             if (destinations == null) {
117                 destinations = new HashSet JavaDoc();
118             }
119             producerDestinations.put(producerId, destinations);
120             destinations.add(destination);
121         }
122     }
123
124     protected void generateFile(PrintWriter JavaDoc writer) throws Exception JavaDoc {
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 JavaDoc clients = new HashMap JavaDoc();
134         Map JavaDoc queues = new HashMap JavaDoc();
135         Map JavaDoc topics = new HashMap JavaDoc();
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 JavaDoc writer, Map JavaDoc clients, Map JavaDoc queues, Map JavaDoc topics) {
159         synchronized(lock) {
160             for (Iterator JavaDoc iter = producerDestinations.entrySet().iterator(); iter.hasNext();) {
161                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
162                 ProducerId producerId = (ProducerId) entry.getKey();
163                 Set JavaDoc destinationSet = (Set JavaDoc) entry.getValue();
164                 printProducers(writer, clients, queues, topics, producerId, destinationSet);
165             }
166         }
167     }
168
169     protected void printProducers(PrintWriter JavaDoc writer, Map JavaDoc clients, Map JavaDoc queues, Map JavaDoc topics, ProducerId producerId, Set JavaDoc destinationSet) {
170         for (Iterator JavaDoc iter = destinationSet.iterator(); iter.hasNext();) {
171             ActiveMQDestination destination = (ActiveMQDestination) iter.next();
172
173             // TODO use clientId one day
174
String JavaDoc clientId = producerId.getConnectionId();
175             String JavaDoc safeClientId = asID(clientId);
176             clients.put(safeClientId, clientId);
177
178             String JavaDoc physicalName = destination.getPhysicalName();
179             String JavaDoc 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 JavaDoc safeProducerId = asID(producerId.toString());
190             
191             // lets write out the links
192

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             // now lets write out the label
204
writer.print(safeProducerId);
205             writer.print(" [label = \"");
206             String JavaDoc label = "Producer: " + producerId.getSessionId() + "-" + producerId.getValue();
207             writer.print(label);
208             writer.println("\"];");
209
210         }
211     }
212
213     
214     protected void printSubscribers(PrintWriter JavaDoc writer, Map JavaDoc clients, Map JavaDoc destinations, String JavaDoc type,
215             ObjectName JavaDoc[] subscribers) {
216         for (int i = 0; i < subscribers.length; i++) {
217             ObjectName JavaDoc name = subscribers[i];
218             SubscriptionViewMBean subscriber = (SubscriptionViewMBean) MBeanServerInvocationHandler.newProxyInstance(
219                     mbeanServer, name, SubscriptionViewMBean.class, true);
220
221             String JavaDoc clientId = subscriber.getClientId();
222             String JavaDoc safeClientId = asID(clientId);
223             clients.put(safeClientId, clientId);
224             
225             String JavaDoc destination = subscriber.getDestinationName();
226             String JavaDoc safeDestinationId = type + asID(destination);
227             destinations.put(safeDestinationId, destination);
228             
229             String JavaDoc selector = subscriber.getSelector();
230
231             // lets write out the links
232
String JavaDoc 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             // now lets write out the label
245
writer.print(subscriberId);
246             writer.print(" [label = \"");
247             String JavaDoc 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 JavaDoc writer, String JavaDoc color, String JavaDoc prefix, Map JavaDoc map) {
257         for (Iterator JavaDoc iter = map.entrySet().iterator(); iter.hasNext();) {
258             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
259             String JavaDoc id = (String JavaDoc) entry.getKey();
260             String JavaDoc label = (String JavaDoc) 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     /**
273      * Lets strip out any non supported characters
274      */

275     protected String JavaDoc asID(String JavaDoc name) {
276         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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 JavaDoc writer, DestinationMapNode node, String JavaDoc prefix) {
290         String JavaDoc path = getPath(node);
291         writer.print(" ");
292         writer.print(prefix);
293         writer.print(ID_SEPARATOR);
294         writer.print(path);
295         String JavaDoc 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 JavaDoc children = node.getChildren();
307         for (Iterator JavaDoc 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 JavaDoc writer, DestinationMapNode node, String JavaDoc prefix) {
314         String JavaDoc path = getPath(node);
315         Collection JavaDoc children = node.getChildren();
316         for (Iterator JavaDoc 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 JavaDoc getPath(DestinationMapNode node) {
336         String JavaDoc path = node.getPath();
337         if (path.equals("*")) {
338             return "root";
339         }
340         return path;
341     }
342 }
343
Popular Tags