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: 883288 $
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 final String imagePrefix = "http://camel.apache.org/images/eip/";
052        private final Map<Object, NodeData> nodeMap = new HashMap<Object, NodeData>();
053        private final boolean makeParentDirs = true;
054        private Map<String, List<RouteDefinition>> routeGroupMap;
055    
056        protected GraphGeneratorSupport(String dir, String extension) {
057            this.dir = dir;
058            this.extension = extension;
059        }
060    
061        public String getRoutesText(CamelContext context) throws IOException {
062            // used by web console
063            List<RouteDefinition> routes = context.getRouteDefinitions();
064            routeGroupMap = createRouteGroupMap(routes);
065            return createRouteMapText();
066        }
067    
068        private String createRouteMapText() {
069            StringWriter buffer = new StringWriter();
070            PrintWriter writer = new PrintWriter(buffer);
071            generateFile(writer, routeGroupMap);
072            writer.close();
073            return buffer.toString();
074        }
075    
076        public void drawRoutes(CamelContext context) throws IOException {
077            File parent = new File(dir);
078            if (makeParentDirs) {
079                parent.mkdirs();
080            }
081            List<RouteDefinition> routes = context.getRouteDefinitions();
082            routeGroupMap = createRouteGroupMap(routes);
083    
084            // generate the global file
085            generateFile(parent, "routes" + extension, routeGroupMap);
086    
087            if (routeGroupMap.size() >= 1) {
088                Set<Map.Entry<String, List<RouteDefinition>>> entries = routeGroupMap.entrySet();
089                for (Map.Entry<String, List<RouteDefinition>> entry : entries) {
090    
091                    Map<String, List<RouteDefinition>> map = new HashMap<String, List<RouteDefinition>>();
092                    String group = entry.getKey();
093                    map.put(group, entry.getValue());
094    
095                    // generate the file containing just the routes in this group
096                    generateFile(parent, group + extension, map);
097                }
098            }
099        }
100    
101        private void generateFile(File parent, String fileName, Map<String, List<RouteDefinition>> map) throws IOException {
102            nodeMap.clear();
103            clusterCounter = 0;
104    
105            PrintWriter writer = new PrintWriter(new FileWriter(new File(parent, fileName)));
106            try {
107                generateFile(writer, map);
108            } finally {
109                writer.close();
110            }
111        }
112    
113        protected abstract void generateFile(PrintWriter writer, Map<String, List<RouteDefinition>> map);
114    
115        protected boolean isMulticastNode(ProcessorDefinition node) {
116            return node instanceof MulticastDefinition || node instanceof ChoiceDefinition;
117        }
118    
119        protected String getLabel(List<ExpressionDefinition> expressions) {
120            CollectionStringBuffer buffer = new CollectionStringBuffer();
121            for (ExpressionDefinition expression : expressions) {
122                buffer.append(getLabel(expression));
123            }
124            return buffer.toString();
125        }
126    
127        protected String getLabel(ExpressionDefinition expression) {
128            if (expression != null) {
129                return expression.getLabel();
130            }
131            return "";
132        }
133    
134        protected NodeData getNodeData(Object node) {
135            Object key = node;
136            if (node instanceof FromDefinition) {
137                FromDefinition fromType = (FromDefinition) node;
138                key = fromType.getUriOrRef();
139            } else if (node instanceof ToDefinition) {
140                ToDefinition toType = (ToDefinition) node;
141                key = toType.getUriOrRef();
142            }
143            NodeData answer = nodeMap.get(key);
144            if (answer == null) {
145                String id = "node" + (nodeMap.size() + 1);
146                answer = new NodeData(id, node, imagePrefix);
147                nodeMap.put(key, answer);
148            }
149            return answer;
150        }
151    
152        protected Map<String, List<RouteDefinition>> createRouteGroupMap(List<RouteDefinition> routes) {
153            Map<String, List<RouteDefinition>> map = new HashMap<String, List<RouteDefinition>>();
154            for (RouteDefinition route : routes) {
155                addRouteToMap(map, route);
156            }
157            return map;
158        }
159    
160        protected void addRouteToMap(Map<String, List<RouteDefinition>> map, RouteDefinition route) {
161            String group = route.getGroup();
162            if (group == null) {
163                group = "Camel Routes";
164            }
165            List<RouteDefinition> list = map.get(group);
166            if (list == null) {
167                list = new ArrayList<RouteDefinition>();
168                map.put(group, list);
169            }
170            list.add(route);
171        }
172    }