KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > view > DotViewFlowListener


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.servicemix.jbi.view;
18
19 import java.io.PrintWriter JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import javax.jbi.messaging.MessageExchange;
28 import javax.jbi.servicedesc.ServiceEndpoint;
29
30 import org.apache.servicemix.JbiConstants;
31 import org.apache.servicemix.jbi.event.ComponentEvent;
32 import org.apache.servicemix.jbi.event.ComponentListener;
33 import org.apache.servicemix.jbi.event.EndpointEvent;
34 import org.apache.servicemix.jbi.event.ExchangeEvent;
35 import org.apache.servicemix.jbi.event.ExchangeListener;
36 import org.apache.servicemix.jbi.framework.ComponentMBeanImpl;
37 import org.apache.servicemix.jbi.framework.Registry;
38 import org.apache.servicemix.jbi.messaging.MessageExchangeImpl;
39 import org.apache.servicemix.jbi.servicedesc.EndpointSupport;
40
41 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
42 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArraySet;
43
44 /**
45  * Creates a <a HREF="http://www.graphviz.org/">DOT</a> file showing the JBI MessageExchange
46  * flow within the JBI Container.
47
48  * @org.apache.xbean.XBean
49  * description="Generates DOT visualisations of the exchanges flow inside ServiceMix"
50  *
51  * @version $Revision: 391707 $
52  */

53 public class DotViewFlowListener extends DotViewEndpointListener
54     implements ExchangeListener, ComponentListener {
55
56     private Map JavaDoc flow;
57     private Set JavaDoc flowLinks;
58     
59     public DotViewFlowListener() {
60         setFile("ServiceMixFlow.dot");
61         flow = new ConcurrentHashMap();
62         flowLinks = new CopyOnWriteArraySet();
63     }
64
65     // Implementation methods
66
// -------------------------------------------------------------------------
67

68     protected void generateFile(PrintWriter JavaDoc writer) throws Exception JavaDoc {
69         writer.println("digraph \"Apache ServiceMix\" {");
70         writer.println();
71         writer.println("label = \"Apache ServiceMix flow\";");
72         writer.println("node [style = \"rounded,filled\", fillcolor = yellow, fontname=\"Helvetica-Oblique\"];");
73         writer.println();
74
75         List JavaDoc brokerLinks = new ArrayList JavaDoc();
76         Registry registry = getContainer().getRegistry();
77         Collection JavaDoc components = registry.getComponents();
78         for (Iterator JavaDoc iter = components.iterator(); iter.hasNext();) {
79             ComponentMBeanImpl component = (ComponentMBeanImpl) iter.next();
80             ServiceEndpoint[] ses = registry.getEndpointsForComponent(component.getComponentNameSpace());
81
82             String JavaDoc name = component.getName();
83             String JavaDoc id = encode(name);
84
85             writer.println("subgraph cluster_" + id + " {");
86             writer.println(" label=\"" + name + "\";");
87             writer.println(" node [fillcolor = green];");
88             writer.println(" " + id + " [label=\"" + name + "\"];");
89             writer.println(" node [fillcolor = red];");
90             
91             for (int i = 0; i < ses.length; i++) {
92                 String JavaDoc epname = EndpointSupport.getKey(ses[i]);
93                 String JavaDoc epid = encode(epname);
94                 writer.println(" " + epid + " [label=\"" + epname + "\"];");
95             }
96             
97             writer.println("}");
98         }
99         writer.println();
100         generateLinks(writer, brokerLinks);
101
102         writer.println();
103
104         generateLinks(writer, flowLinks);
105         
106         writer.println("}");
107         
108     }
109
110     public void exchangeSent(ExchangeEvent event) {
111         MessageExchange me = event.getExchange();
112         if (me.getEndpoint() != null &&
113             me instanceof MessageExchangeImpl) {
114             MessageExchangeImpl mei = (MessageExchangeImpl) me;
115             String JavaDoc source = (String JavaDoc) me.getProperty(JbiConstants.SENDER_ENDPOINT);
116             if (source == null) {
117                 source = mei.getSourceId().getName();
118             }
119             String JavaDoc dest = EndpointSupport.getKey(mei.getEndpoint());
120             Map JavaDoc componentFlow = (Map JavaDoc) flow.get(source);
121             if (componentFlow.put(dest, Boolean.TRUE) == null) {
122                 flowLinks.add(encode(source) + " -> " + encode(dest));
123                 viewIsDirty(mei.getEndpoint());
124             }
125         }
126         
127     }
128     
129     protected void createSource(String JavaDoc name) {
130         synchronized (flow) {
131             Map JavaDoc componentFlow = (Map JavaDoc) flow.get(name);
132             if (componentFlow == null) {
133                 componentFlow = new ConcurrentHashMap();
134                 flow.put(name, componentFlow);
135             }
136         }
137     }
138
139     public void internalEndpointRegistered(EndpointEvent event) {
140         String JavaDoc key = EndpointSupport.getKey(event.getEndpoint());
141         createSource(key);
142         super.internalEndpointRegistered(event);
143     }
144     
145     public void externalEndpointRegistered(EndpointEvent event) {
146         String JavaDoc key = EndpointSupport.getKey(event.getEndpoint());
147         createSource(key);
148         super.externalEndpointRegistered(event);
149     }
150     
151     public void linkedEndpointRegistered(EndpointEvent event) {
152         String JavaDoc key = EndpointSupport.getKey(event.getEndpoint());
153         createSource(key);
154         super.linkedEndpointRegistered(event);
155     }
156     
157     public void componentInstalled(ComponentEvent event) {
158         createSource(event.getComponent().getName());
159     }
160
161     public void componentStarted(ComponentEvent event) {
162         createSource(event.getComponent().getName());
163     }
164
165     public void componentStopped(ComponentEvent event) {
166     }
167
168     public void componentShutDown(ComponentEvent event) {
169     }
170
171     public void componentUninstalled(ComponentEvent event) {
172     }
173
174 }
175
Popular Tags