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