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.impl;
018
019import org.apache.camel.DelegateProcessor;
020import org.apache.camel.Exchange;
021import org.apache.camel.Expression;
022import org.apache.camel.Processor;
023import org.apache.camel.RouteNode;
024import org.apache.camel.Traceable;
025import org.apache.camel.model.ProcessorDefinition;
026
027/**
028 * A default implementation of the {@link org.apache.camel.RouteNode}
029 *
030 * @version 
031 */
032public class DefaultRouteNode implements RouteNode {
033
034    private Expression expression;
035    private Processor processor;
036    private ProcessorDefinition<?> processorDefinition;
037
038    public DefaultRouteNode(ProcessorDefinition<?> processorDefinition, Processor processor) {
039        this.processor = processor;
040        this.processorDefinition = processorDefinition;
041    }
042
043    public DefaultRouteNode(ProcessorDefinition<?> processorDefinition, Expression expression) {
044        this.processorDefinition = processorDefinition;
045        this.expression = expression;
046    }
047
048    public Processor getProcessor() {
049        return processor;
050    }
051
052    public ProcessorDefinition<?> getProcessorDefinition() {
053        return processorDefinition;
054    }
055
056    public String getLabel(Exchange exchange) {
057        if (expression != null) {
058            return expression.evaluate(exchange, String.class);
059        }
060
061        String label = getTraceLabel(processor);
062        if (label == null) {
063            // no label then default to use the definition label
064            label = processorDefinition.getLabel();
065        }
066        return label;
067    }
068
069    @SuppressWarnings("deprecation")
070    private String getTraceLabel(Processor target) {
071        if (target == null) {
072            return null;
073        }
074
075        if (target instanceof Traceable) {
076            Traceable trace = (Traceable) target;
077            return trace.getTraceLabel();
078        } else if (target instanceof org.apache.camel.processor.Traceable) {
079            // to be backwards compatible
080            org.apache.camel.processor.Traceable trace = (org.apache.camel.processor.Traceable) target;
081            return trace.getTraceLabel();
082        }
083
084        // if we are a delegate then drill down
085        if (target instanceof DelegateProcessor) {
086            return getTraceLabel(((DelegateProcessor) target).getProcessor());
087        }
088
089        return null;
090    }
091
092    public boolean isAbstract() {
093        return processor == null;
094    }
095
096    @Override
097    public String toString() {
098        return "RouteNode[" + processorDefinition + "]";
099    }
100}