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.util.ArrayList; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023 024import org.apache.camel.model.ChoiceDefinition; 025import org.apache.camel.model.FromDefinition; 026import org.apache.camel.model.MulticastDefinition; 027import org.apache.camel.model.PipelineDefinition; 028import org.apache.camel.model.ProcessorDefinition; 029import org.apache.camel.model.RouteDefinition; 030import org.apache.camel.model.ToDefinition; 031import org.apache.camel.model.language.ExpressionDefinition; 032import org.apache.camel.util.CollectionStringBuffer; 033import org.slf4j.Logger; 034import org.slf4j.LoggerFactory; 035 036/** 037 * A base class for Graph processing code of Camel EIPs containing a number of helper methods 038 * 039 * @version 040 */ 041public class GraphSupport { 042 protected final Logger log = LoggerFactory.getLogger(getClass()); 043 protected final Map<Object, NodeData> nodeMap = new HashMap<Object, NodeData>(); 044 private String imagePrefix = "http://camel.apache.org/images/eip/"; 045 046 protected String getLabel(List<ExpressionDefinition> expressions) { 047 CollectionStringBuffer buffer = new CollectionStringBuffer(); 048 for (ExpressionDefinition expression : expressions) { 049 buffer.append(getLabel(expression)); 050 } 051 return buffer.toString(); 052 } 053 054 protected String getLabel(ExpressionDefinition expression) { 055 if (expression != null) { 056 return expression.getLabel(); 057 } 058 return ""; 059 } 060 061 protected NodeData getNodeData(Object node) { 062 Object key = node; 063 if (node instanceof FromDefinition) { 064 FromDefinition fromType = (FromDefinition) node; 065 key = fromType.getUriOrRef(); 066 } else if (node instanceof ToDefinition) { 067 ToDefinition toType = (ToDefinition) node; 068 key = toType.getUriOrRef(); 069 } 070 NodeData answer = null; 071 if (key != null) { 072 answer = nodeMap.get(key); 073 } 074 if (answer == null) { 075 String id = "node" + (nodeMap.size() + 1); 076 answer = new NodeData(id, node, imagePrefix); 077 nodeMap.put(key, answer); 078 } 079 return answer; 080 } 081 082 protected Map<String, List<RouteDefinition>> createRouteGroupMap(List<RouteDefinition> routes) { 083 Map<String, List<RouteDefinition>> map = new HashMap<String, List<RouteDefinition>>(); 084 for (RouteDefinition route : routes) { 085 addRouteToMap(map, route); 086 } 087 return map; 088 } 089 090 protected void addRouteToMap(Map<String, List<RouteDefinition>> map, RouteDefinition route) { 091 String group = route.getGroup(); 092 if (group == null) { 093 group = "Camel Routes"; 094 } 095 List<RouteDefinition> list = map.get(group); 096 if (list == null) { 097 list = new ArrayList<RouteDefinition>(); 098 map.put(group, list); 099 } 100 list.add(route); 101 } 102 103 protected boolean isMulticastNode(ProcessorDefinition<?> node) { 104 return node instanceof MulticastDefinition || node instanceof ChoiceDefinition; 105 } 106 107 /** 108 * Is the given node a pipeline 109 */ 110 protected boolean isPipeline(ProcessorDefinition<?> node) { 111 if (node instanceof MulticastDefinition) { 112 return false; 113 } 114 if (node instanceof PipelineDefinition) { 115 return true; 116 } 117 if (node.getOutputs().size() > 1) { 118 // is pipeline if there is more than 1 output and they are all To types 119 for (Object type : node.getOutputs()) { 120 if (!(type instanceof ToDefinition)) { 121 return false; 122 } 123 } 124 return true; 125 } 126 return false; 127 } 128 129 public String getImagePrefix() { 130 return imagePrefix; 131 } 132 133 public void setImagePrefix(String imagePrefix) { 134 this.imagePrefix = imagePrefix; 135 } 136}