1 17 package org.apache.servicemix.jbi.view; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.servicemix.jbi.container.ContainerAware; 22 import org.apache.servicemix.jbi.container.JBIContainer; 23 import org.apache.servicemix.jbi.framework.ComponentMBeanImpl; 24 import org.apache.servicemix.jbi.framework.Endpoint; 25 import org.apache.servicemix.jbi.framework.Registry; 26 27 import java.io.FileWriter ; 28 import java.io.PrintWriter ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 43 public class DotViewEndpointListener extends EndpointViewRenderer implements ContainerAware { 44 45 private static final Log log = LogFactory.getLog(DotViewEndpointListener.class); 46 47 private JBIContainer container; 48 private String file = "ServiceMixComponents.dot"; 49 50 51 public JBIContainer getContainer() { 52 return container; 53 } 54 55 public void setContainer(JBIContainer container) { 56 this.container = container; 57 } 58 59 public String getFile() { 60 return file; 61 } 62 63 public void setFile(String file) { 64 this.file = file; 65 } 66 67 70 protected void doRender() throws Exception { 71 if (log.isDebugEnabled()) { 72 log.debug("Creating DOT file at: " + file); 73 } 74 PrintWriter writer = new PrintWriter (new FileWriter (file)); 75 try { 76 generateFile(writer); 77 } 78 finally { 79 writer.close(); 80 } 81 } 82 83 protected void generateFile(PrintWriter writer) throws Exception { 84 writer.println("digraph \"Apache ServiceMix\" {"); 85 writer.println(); 86 writer.println("node [style = \"rounded,filled\", fillcolor = yellow, fontname=\"Helvetica-Oblique\"];"); 87 writer.println(); 88 writer.println("jbi [label=\"Apache ServiceMix: " + container.getName() + "\"];"); 89 writer.println(); 90 writer.println("node [fillcolor = green];"); 91 writer.println(); 92 93 List brokerLinks = new ArrayList (); 94 Registry registry = container.getRegistry(); 95 Collection components = registry.getComponents(); 96 for (Iterator iter = components.iterator(); iter.hasNext();) { 97 ComponentMBeanImpl component = (ComponentMBeanImpl) iter.next(); 98 String name = component.getName(); 99 String id = encode(name); 100 101 writer.print(id); 102 writer.print(" [ label=\""); 103 writer.print(name); 104 writer.println("\" ];"); 105 106 brokerLinks.add("jbi -> " + id); 107 } 108 writer.println(); 109 generateLinks(writer, brokerLinks); 110 111 writer.println("node [fillcolor = red];"); 112 writer.println(); 113 114 List componentEndpointLinks = new ArrayList (); 115 Collection endpointMBeans = registry.getEndpointRegistry().getEndpointMBeans(); 116 for (Iterator iter = endpointMBeans.iterator(); iter.hasNext();) { 117 Endpoint endpoint = (Endpoint) iter.next(); 118 String name = endpoint.getEndpointName(); 119 String componentName = encode(endpoint.getComponentName()); 120 String id = encode(name); 121 writer.print(id); 122 writer.print(" [ label=\""); 123 writer.print(name); 124 writer.println("\" ];"); 125 126 componentEndpointLinks.add(componentName + " -> " + id); 127 } 128 generateLinks(writer, componentEndpointLinks); 129 130 writer.println("}"); 131 } 132 133 protected void generateLinks(PrintWriter writer, Collection lines, String style) { 134 for (Iterator iter = lines.iterator(); iter.hasNext();) { 135 writer.print(iter.next().toString()); 136 if (style != null) { 137 writer.print(" [" + style + "]"); 138 } 139 writer.println(";"); 140 } 141 writer.println(); 142 } 143 144 protected void generateLinks(PrintWriter writer, Collection lines) { 145 generateLinks(writer, lines, null); 146 } 147 148 151 protected String encode(String name) { 152 StringBuffer buffer = new StringBuffer (); 153 for (int i = 0, size = name.length(); i < size; i++) { 154 char ch = name.charAt(i); 155 if (Character.isLetterOrDigit(ch) || ch == '_') { 156 buffer.append(ch); 157 } 158 else { 159 buffer.append('_'); 160 } 161 } 162 return buffer.toString(); 163 } 164 165 166 } 167 | Popular Tags |