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