servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r391705 - in /incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/view: ./ DotViewEndpointListener.java EndpointViewRenderer.java
Date Wed, 05 Apr 2006 18:44:47 GMT
Author: jstrachan
Date: Wed Apr  5 11:44:46 2006
New Revision: 391705

URL: http://svn.apache.org/viewcvs?rev=391705&view=rev
Log:
added a simple DOT visualiser which renders a graph of components and endpoints available
in servicemix

Added:
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/view/
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/view/DotViewEndpointListener.java
  (with props)
    incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/view/EndpointViewRenderer.java
  (with props)

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/view/DotViewEndpointListener.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/view/DotViewEndpointListener.java?rev=391705&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/view/DotViewEndpointListener.java
(added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/view/DotViewEndpointListener.java
Wed Apr  5 11:44:46 2006
@@ -0,0 +1,161 @@
+/**
+ *
+ * Copyright 2005-2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.jbi.view;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.servicemix.jbi.container.ContainerAware;
+import org.apache.servicemix.jbi.container.JBIContainer;
+import org.apache.servicemix.jbi.framework.ComponentMBeanImpl;
+import org.apache.servicemix.jbi.framework.Endpoint;
+import org.apache.servicemix.jbi.framework.Registry;
+
+import java.io.FileWriter;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Creates a <a href="http://www.graphviz.org/">DOT</a> file showing the various
components
+ * and endpoints within ServiceMix
+
+ * @org.apache.xbean.XBean 
+ * description="Generates DOT visualisations of the components and endpoints available inside
ServiceMix"
+ * 
+ * @version $Revision$
+ */
+public class DotViewEndpointListener extends EndpointViewRenderer implements ContainerAware
{
+
+    private static final Log log = LogFactory.getLog(DotViewEndpointListener.class);
+
+    private JBIContainer container;
+    private String file = "ServiceMixComponents.dot";
+    
+
+    public JBIContainer getContainer() {
+        return container;
+    }
+
+    public void setContainer(JBIContainer container) {
+        System.out.println("####Êregistering container: " + container);
+        this.container = container;
+    }
+
+    public String getFile() {
+        return file;
+    }
+
+    public void setFile(String file) {
+        this.file = file;
+    }
+
+    // Implementation methods
+    // -------------------------------------------------------------------------
+
+    protected void doRender() throws Exception {
+        if (log.isDebugEnabled()) {
+            log.debug("Creating DOT file at: " + file);
+        }
+        System.out.println("####Êcreating: " + file);
+        PrintWriter writer = new PrintWriter(new FileWriter(file));
+        try {
+            generateFile(writer);
+        }
+        finally {
+            writer.close();
+        }
+    }
+
+    protected void generateFile(PrintWriter writer) throws Exception {
+        writer.println("digraph \"Apache ServiceMix\" {");
+        writer.println();
+        writer.println("node [style = \"rounded,filled\", fillcolor = yellow, fontname=\"Helvetica-Oblique\"];");
+        writer.println();
+        writer.println("jbi [label=\"Apache ServiceMix: " + container.getName() + "\"];");
+        writer.println();
+        writer.println("node [fillcolor = green];");
+        writer.println();
+
+        List brokerLinks = new ArrayList();
+        Registry registry = container.getRegistry();
+        Collection components = registry.getComponents();
+        for (Iterator iter = components.iterator(); iter.hasNext();) {
+            ComponentMBeanImpl component = (ComponentMBeanImpl) iter.next();
+            String name = component.getName();
+            String id = encode(name);
+
+            writer.print(id);
+            writer.print(" [ label=\"");
+            writer.print(name);
+            writer.println("\" ];");
+
+            brokerLinks.add("jbi -> " + id);
+        }
+        writer.println();
+        generateLinks(writer, brokerLinks);
+
+        writer.println("node [fillcolor = red];");
+        writer.println();
+
+        List componentEndpointLinks = new ArrayList();
+        Collection endpointMBeans = registry.getEndpointRegistry().getEndpointMBeans();
+        for (Iterator iter = endpointMBeans.iterator(); iter.hasNext();) {
+            Endpoint endpoint = (Endpoint) iter.next();
+            String name = endpoint.getEndpointName();
+            String componentName = encode(endpoint.getComponentName());
+            String id = encode(name);
+            writer.print(id);
+            writer.print(" [ label=\"");
+            writer.print(name);
+            writer.println("\" ];");
+            
+            componentEndpointLinks.add(componentName + " -> " + id);
+        }
+        generateLinks(writer, componentEndpointLinks);
+        
+        writer.println("}");
+    }
+
+    protected void generateLinks(PrintWriter writer, List lines) {
+        for (Iterator iter = lines.iterator(); iter.hasNext();) {
+            writer.print(iter.next().toString());
+            writer.println(";");
+        }
+        writer.println();
+    }
+
+    /**
+     * Lets strip out any non supported characters
+     */
+    protected String encode(String name) {
+        StringBuffer buffer = new StringBuffer();
+        for (int i = 0, size = name.length(); i < size; i++) {
+            char ch = name.charAt(i);
+            if (Character.isLetterOrDigit(ch) || ch == '_') {
+                buffer.append(ch);
+            }
+            else {
+                buffer.append('_');
+            }
+        }
+        return buffer.toString();
+    }
+
+
+}

Propchange: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/view/DotViewEndpointListener.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/view/DotViewEndpointListener.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/view/DotViewEndpointListener.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/view/EndpointViewRenderer.java
URL: http://svn.apache.org/viewcvs/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/view/EndpointViewRenderer.java?rev=391705&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/view/EndpointViewRenderer.java
(added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/view/EndpointViewRenderer.java
Wed Apr  5 11:44:46 2006
@@ -0,0 +1,106 @@
+/**
+ *
+ * Copyright 2005-2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.jbi.view;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.servicemix.jbi.event.EndpointEvent;
+import org.apache.servicemix.jbi.event.EndpointListener;
+
+import javax.jbi.servicedesc.ServiceEndpoint;
+
+/**
+ * A base class for renderings of endpoints which can re-render whenever something
+ * changes or mark things as dirty so that they can be re-rendered on demand or in a time
based way
+ * 
+ * @version $Revision$
+ */
+public abstract class EndpointViewRenderer implements EndpointListener {
+
+    private static final Log log = LogFactory.getLog(EndpointViewRenderer.class);
+    
+    private boolean dirty;
+    private boolean rerenderOnChange = true;
+
+    public void render() throws Exception {
+        doRender();
+        dirty = false;
+    }
+
+    public void internalEndpointRegistered(EndpointEvent event) {
+        viewIsDirty(event.getEndpoint());
+    }
+
+    public void internalEndpointUnregistered(EndpointEvent event) {
+        viewIsDirty(event.getEndpoint());
+    }
+
+    public void externalEndpointRegistered(EndpointEvent event) {
+        viewIsDirty(event.getEndpoint());
+    }
+
+    public void externalEndpointUnregistered(EndpointEvent event) {
+        viewIsDirty(event.getEndpoint());
+    }
+
+    public void linkedEndpointRegistered(EndpointEvent event) {
+        viewIsDirty(event.getEndpoint());
+    }
+
+    public void linkedEndpointUnregistered(EndpointEvent event) {
+        viewIsDirty(event.getEndpoint());
+    }
+
+    public void remoteEndpointRegistered(EndpointEvent event) {
+        viewIsDirty(event.getEndpoint());
+    }
+
+    public void remoteEndpointUnregistered(EndpointEvent event) {
+        viewIsDirty(event.getEndpoint());
+    }
+
+    // Properties
+    // -------------------------------------------------------------------------
+    public boolean isRerenderOnChange() {
+        return rerenderOnChange;
+    }
+
+    public void setRerenderOnChange(boolean rerenderOnChange) {
+        this.rerenderOnChange = rerenderOnChange;
+    }
+
+    public boolean isDirty() {
+        return dirty;
+    }
+
+    // Implementation methods
+    // -------------------------------------------------------------------------
+    protected abstract void doRender() throws Exception;
+
+    protected void viewIsDirty(ServiceEndpoint endpoint) {
+        dirty = true;
+        if (rerenderOnChange) {
+            try {
+                render();
+            }
+            catch (Exception e) {
+                log.warn("Failed to render view: " + e, e);
+            }
+        }
+    }
+
+}

Propchange: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/view/EndpointViewRenderer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/view/EndpointViewRenderer.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/view/EndpointViewRenderer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message