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