001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.view;
019    
020    import org.apache.camel.CamelContext;
021    import org.apache.camel.Endpoint;
022    import org.apache.camel.Processor;
023    import org.apache.camel.Route;
024    import org.apache.camel.impl.EventDrivenConsumerRoute;
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    
028    import java.io.FileWriter;
029    import java.io.IOException;
030    import java.io.PrintWriter;
031    import java.util.List;
032    
033    /**
034     * A <a href="http://www.graphviz.org/">DOT</a> file creator plugin which
035     * creates a DOT file showing the current routes
036     *
037     * @version $Revision: 523881 $
038     */
039    public class RouteDotGenerator {
040        private static final transient Log log = LogFactory.getLog(RouteDotGenerator.class);
041        private String file = "CamelRoutes.dot";
042    
043        public String getFile() {
044            return file;
045        }
046    
047        /**
048         * Sets the destination file name to create the destination diagram
049         */
050        public void setFile(String file) {
051            this.file = file;
052        }
053    
054        public void drawRoutes(CamelContext context) throws IOException {
055            PrintWriter writer = new PrintWriter(new FileWriter(file));
056            generateFile(writer, context);
057        }
058    
059        protected void generateFile(PrintWriter writer, CamelContext context) {
060            writer.println("digraph \"Camel Routes\" {");
061            writer.println();
062            writer.println("label=\"Camel Container: " + context + "\"];");
063            writer.println();
064            writer.println("node [style = \"rounded,filled\", fillcolor = yellow, fontname=\"Helvetica-Oblique\"];");
065            writer.println();
066            printRoutes(writer, context.getRoutes());
067        }
068    
069        protected void printRoutes(PrintWriter writer, List<Route> routes) {
070            for (Route r : routes) {
071                Endpoint end = r.getEndpoint();
072                writer.print(end.getEndpointUri());
073                writer.print(" -> ");
074                writer.print(r);
075                writer.print(" -> ");
076                if (r instanceof EventDrivenConsumerRoute) {
077                    EventDrivenConsumerRoute consumerRoute = (EventDrivenConsumerRoute) r;
078                    Processor p = consumerRoute.getProcessor();
079                    writer.println(p);
080                }
081            }
082        }
083    }