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.File; 020 import java.io.FileWriter; 021 import java.io.IOException; 022 import java.io.PrintWriter; 023 import java.io.StringWriter; 024 import java.util.ArrayList; 025 import java.util.HashMap; 026 import java.util.List; 027 import java.util.Map; 028 import java.util.Set; 029 030 import org.apache.camel.CamelContext; 031 import org.apache.camel.model.ChoiceDefinition; 032 import org.apache.camel.model.FromDefinition; 033 import org.apache.camel.model.MulticastDefinition; 034 import org.apache.camel.model.ProcessorDefinition; 035 import org.apache.camel.model.RouteDefinition; 036 import org.apache.camel.model.ToDefinition; 037 import org.apache.camel.model.language.ExpressionDefinition; 038 import org.apache.camel.util.CollectionStringBuffer; 039 import org.apache.commons.logging.Log; 040 import org.apache.commons.logging.LogFactory; 041 042 /** 043 * @version $Revision: 750806 $ 044 */ 045 public abstract class GraphGeneratorSupport { 046 protected final transient Log log = LogFactory.getLog(getClass()); 047 protected String dir; 048 protected int clusterCounter; 049 protected String extension; 050 051 //private String imagePrefix = "http://www.enterpriseintegrationpatterns.com/img/"; 052 private String imagePrefix = "http://camel.apache.org/images/eip/"; 053 private Map<Object, NodeData> nodeMap = new HashMap<Object, NodeData>(); 054 private boolean makeParentDirs = true; 055 private Map<String, List<RouteDefinition>> routeGroupMap; 056 057 protected GraphGeneratorSupport(String dir, String extension) { 058 this.dir = dir; 059 this.extension = extension; 060 } 061 062 public String getDir() { 063 return dir; 064 } 065 066 /** 067 * Sets the destination directory in which to create the diagrams 068 */ 069 public void setDir(String dir) { 070 this.dir = dir; 071 } 072 073 public String getRoutesText(CamelContext context) throws IOException { 074 List<RouteDefinition> routes = context.getRouteDefinitions(); 075 routeGroupMap = createRouteGroupMap(routes); 076 return createRouteMapText(); 077 } 078 079 public String getRouteText(RouteDefinition route) throws IOException { 080 routeGroupMap = createRouteGroupMap(route); 081 return createRouteMapText(); 082 } 083 084 private String createRouteMapText() { 085 StringWriter buffer = new StringWriter(); 086 PrintWriter writer = new PrintWriter(buffer); 087 generateFile(writer, routeGroupMap); 088 writer.close(); 089 return buffer.toString(); 090 } 091 092 public void drawRoutes(CamelContext context) throws IOException { 093 File parent = new File(dir); 094 if (makeParentDirs) { 095 parent.mkdirs(); 096 } 097 List<RouteDefinition> routes = context.getRouteDefinitions(); 098 routeGroupMap = createRouteGroupMap(routes); 099 100 // generate the global file 101 generateFile(parent, "routes" + extension, routeGroupMap); 102 103 if (routeGroupMap.size() >= 1) { 104 Set<Map.Entry<String, List<RouteDefinition>>> entries = routeGroupMap.entrySet(); 105 for (Map.Entry<String, List<RouteDefinition>> entry : entries) { 106 107 Map<String, List<RouteDefinition>> map = new HashMap<String, List<RouteDefinition>>(); 108 String group = entry.getKey(); 109 map.put(group, entry.getValue()); 110 111 // generate the file containing just the routes in this group 112 generateFile(parent, group + extension, map); 113 } 114 } 115 } 116 117 private void generateFile(File parent, String fileName, Map<String, List<RouteDefinition>> map) throws IOException { 118 nodeMap.clear(); 119 clusterCounter = 0; 120 121 PrintWriter writer = new PrintWriter(new FileWriter(new File(parent, fileName))); 122 try { 123 generateFile(writer, map); 124 } finally { 125 writer.close(); 126 } 127 } 128 129 protected abstract void generateFile(PrintWriter writer, Map<String, List<RouteDefinition>> map); 130 131 protected boolean isMulticastNode(ProcessorDefinition node) { 132 return node instanceof MulticastDefinition || node instanceof ChoiceDefinition; 133 } 134 135 protected String getLabel(List<ExpressionDefinition> expressions) { 136 CollectionStringBuffer buffer = new CollectionStringBuffer(); 137 for (ExpressionDefinition expression : expressions) { 138 buffer.append(getLabel(expression)); 139 } 140 return buffer.toString(); 141 } 142 143 protected String getLabel(ExpressionDefinition expression) { 144 if (expression != null) { 145 return expression.getLabel(); 146 } 147 return ""; 148 } 149 150 protected NodeData getNodeData(Object node) { 151 Object key = node; 152 if (node instanceof FromDefinition) { 153 FromDefinition fromType = (FromDefinition) node; 154 key = fromType.getUriOrRef(); 155 } else if (node instanceof ToDefinition) { 156 ToDefinition toType = (ToDefinition) node; 157 key = toType.getUriOrRef(); 158 } 159 NodeData answer = nodeMap.get(key); 160 if (answer == null) { 161 String id = "node" + (nodeMap.size() + 1); 162 answer = new NodeData(id, node, imagePrefix); 163 nodeMap.put(key, answer); 164 } 165 return answer; 166 } 167 168 protected Map<String, List<RouteDefinition>> createRouteGroupMap(List<RouteDefinition> routes) { 169 Map<String, List<RouteDefinition>> map = new HashMap<String, List<RouteDefinition>>(); 170 for (RouteDefinition route : routes) { 171 addRouteToMap(map, route); 172 } 173 return map; 174 } 175 176 protected Map<String, List<RouteDefinition>> createRouteGroupMap(RouteDefinition route) { 177 Map<String, List<RouteDefinition>> map = new HashMap<String, List<RouteDefinition>>(); 178 addRouteToMap(map, route); 179 return map; 180 } 181 182 protected void addRouteToMap(Map<String, List<RouteDefinition>> map, RouteDefinition route) { 183 String group = route.getGroup(); 184 if (group == null) { 185 group = "Camel Routes"; 186 } 187 List<RouteDefinition> list = map.get(group); 188 if (list == null) { 189 list = new ArrayList<RouteDefinition>(); 190 map.put(group, list); 191 } 192 list.add(route); 193 } 194 }