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 */ 017package org.apache.camel.view; 018 019import java.io.File; 020import java.io.FileWriter; 021import java.io.IOException; 022import java.io.PrintWriter; 023import java.io.StringWriter; 024import java.util.HashMap; 025import java.util.List; 026import java.util.Map; 027import java.util.Set; 028 029import org.apache.camel.CamelContext; 030import org.apache.camel.model.ModelCamelContext; 031import org.apache.camel.model.RouteDefinition; 032 033/** 034 * @version 035 */ 036@Deprecated 037public abstract class GraphGeneratorSupport extends GraphSupport { 038 protected String dir; 039 protected int clusterCounter; 040 protected String extension; 041 042 private final boolean makeParentDirs = true; 043 private Map<String, List<RouteDefinition>> routeGroupMap; 044 045 protected GraphGeneratorSupport(String dir, String extension) { 046 this.dir = dir; 047 this.extension = extension; 048 } 049 050 public String getRoutesText(CamelContext context) throws IOException { 051 // used by web console 052 List<RouteDefinition> routes = ((ModelCamelContext)context).getRouteDefinitions(); 053 routeGroupMap = createRouteGroupMap(routes); 054 return createRouteMapText(); 055 } 056 057 private String createRouteMapText() { 058 StringWriter buffer = new StringWriter(); 059 PrintWriter writer = new PrintWriter(buffer); 060 generateFile(writer, routeGroupMap); 061 writer.close(); 062 return buffer.toString(); 063 } 064 065 public void drawRoutes(CamelContext context) throws IOException { 066 File parent = new File(dir); 067 if (makeParentDirs) { 068 parent.mkdirs(); 069 } 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}