KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33
34 /**
35  * Creates a <a HREF="http://www.graphviz.org/">DOT</a> file showing the various components
36  * and endpoints within ServiceMix
37
38  * @org.apache.xbean.XBean
39  * description="Generates DOT visualisations of the components and endpoints available inside ServiceMix"
40  *
41  * @version $Revision: 432921 $
42  */

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 JavaDoc 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 JavaDoc getFile() {
60         return file;
61     }
62
63     public void setFile(String JavaDoc file) {
64         this.file = file;
65     }
66
67     // Implementation methods
68
// -------------------------------------------------------------------------
69

70     protected void doRender() throws Exception JavaDoc {
71         if (log.isDebugEnabled()) {
72             log.debug("Creating DOT file at: " + file);
73         }
74         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new FileWriter JavaDoc(file));
75         try {
76             generateFile(writer);
77         }
78         finally {
79             writer.close();
80         }
81     }
82
83     protected void generateFile(PrintWriter JavaDoc writer) throws Exception JavaDoc {
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 JavaDoc brokerLinks = new ArrayList JavaDoc();
94         Registry registry = container.getRegistry();
95         Collection JavaDoc components = registry.getComponents();
96         for (Iterator JavaDoc iter = components.iterator(); iter.hasNext();) {
97             ComponentMBeanImpl component = (ComponentMBeanImpl) iter.next();
98             String JavaDoc name = component.getName();
99             String JavaDoc 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 JavaDoc componentEndpointLinks = new ArrayList JavaDoc();
115         Collection JavaDoc endpointMBeans = registry.getEndpointRegistry().getEndpointMBeans();
116         for (Iterator JavaDoc iter = endpointMBeans.iterator(); iter.hasNext();) {
117             Endpoint endpoint = (Endpoint) iter.next();
118             String JavaDoc name = endpoint.getEndpointName();
119             String JavaDoc componentName = encode(endpoint.getComponentName());
120             String JavaDoc 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 JavaDoc writer, Collection JavaDoc lines, String JavaDoc style) {
134         for (Iterator JavaDoc 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 JavaDoc writer, Collection JavaDoc lines) {
145         generateLinks(writer, lines, null);
146     }
147
148     /**
149      * Lets strip out any non supported characters
150      */

151     protected String JavaDoc encode(String JavaDoc name) {
152         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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