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.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.ModelCamelContext;
031 import org.apache.camel.model.RouteDefinition;
032
033 /**
034 * @version
035 */
036 public abstract class GraphGeneratorSupport extends GraphSupport {
037 protected String dir;
038 protected int clusterCounter;
039 protected String extension;
040
041 private final boolean makeParentDirs = true;
042 private Map<String, List<RouteDefinition>> routeGroupMap;
043
044 protected GraphGeneratorSupport(String dir, String extension) {
045 this.dir = dir;
046 this.extension = extension;
047 }
048
049 public String getRoutesText(CamelContext context) throws IOException {
050 // used by web console
051 List<RouteDefinition> routes = ((ModelCamelContext)context).getRouteDefinitions();
052 routeGroupMap = createRouteGroupMap(routes);
053 return createRouteMapText();
054 }
055
056 private String createRouteMapText() {
057 StringWriter buffer = new StringWriter();
058 PrintWriter writer = new PrintWriter(buffer);
059 generateFile(writer, routeGroupMap);
060 writer.close();
061 return buffer.toString();
062 }
063
064 public void drawRoutes(CamelContext context) throws IOException {
065 File parent = new File(dir);
066 if (makeParentDirs) {
067 parent.mkdirs();
068 }
069 @SuppressWarnings("deprecation")
070 List<RouteDefinition> routes = context.getRouteDefinitions();
071 routeGroupMap = createRouteGroupMap(routes);
072
073 // generate the global file
074 generateFile(parent, "routes" + extension, routeGroupMap);
075
076 if (routeGroupMap.size() >= 1) {
077 Set<Map.Entry<String, List<RouteDefinition>>> entries = routeGroupMap.entrySet();
078 for (Map.Entry<String, List<RouteDefinition>> entry : entries) {
079
080 Map<String, List<RouteDefinition>> map = new HashMap<String, List<RouteDefinition>>();
081 String group = entry.getKey();
082 map.put(group, entry.getValue());
083
084 // generate the file containing just the routes in this group
085 generateFile(parent, group + extension, map);
086 }
087 }
088 }
089
090 private void generateFile(File parent, String fileName, Map<String, List<RouteDefinition>> map) throws IOException {
091 nodeMap.clear();
092 clusterCounter = 0;
093
094 PrintWriter writer = new PrintWriter(new FileWriter(new File(parent, fileName)));
095 try {
096 generateFile(writer, map);
097 } finally {
098 writer.close();
099 }
100 }
101
102 protected abstract void generateFile(PrintWriter writer, Map<String, List<RouteDefinition>> map);
103
104 }