1 17 package org.apache.servicemix.jbi.view; 18 19 import java.io.PrintWriter ; 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Set ; 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 53 public class DotViewFlowListener extends DotViewEndpointListener 54 implements ExchangeListener, ComponentListener { 55 56 private Map flow; 57 private Set flowLinks; 58 59 public DotViewFlowListener() { 60 setFile("ServiceMixFlow.dot"); 61 flow = new ConcurrentHashMap(); 62 flowLinks = new CopyOnWriteArraySet(); 63 } 64 65 68 protected void generateFile(PrintWriter writer) throws Exception { 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 brokerLinks = new ArrayList (); 76 Registry registry = getContainer().getRegistry(); 77 Collection components = registry.getComponents(); 78 for (Iterator iter = components.iterator(); iter.hasNext();) { 79 ComponentMBeanImpl component = (ComponentMBeanImpl) iter.next(); 80 ServiceEndpoint[] ses = registry.getEndpointsForComponent(component.getComponentNameSpace()); 81 82 String name = component.getName(); 83 String 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 epname = EndpointSupport.getKey(ses[i]); 93 String 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 source = (String ) me.getProperty(JbiConstants.SENDER_ENDPOINT); 116 if (source == null) { 117 source = mei.getSourceId().getName(); 118 } 119 String dest = EndpointSupport.getKey(mei.getEndpoint()); 120 Map componentFlow = (Map ) 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 name) { 130 synchronized (flow) { 131 Map componentFlow = (Map ) 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 key = EndpointSupport.getKey(event.getEndpoint()); 141 createSource(key); 142 super.internalEndpointRegistered(event); 143 } 144 145 public void externalEndpointRegistered(EndpointEvent event) { 146 String key = EndpointSupport.getKey(event.getEndpoint()); 147 createSource(key); 148 super.externalEndpointRegistered(event); 149 } 150 151 public void linkedEndpointRegistered(EndpointEvent event) { 152 String 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 |