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.model;
018
019import javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlRootElement;
023import javax.xml.bind.annotation.XmlTransient;
024
025import org.apache.camel.AsyncProcessor;
026import org.apache.camel.Processor;
027import org.apache.camel.Service;
028import org.apache.camel.processor.DelegateAsyncProcessor;
029import org.apache.camel.processor.DelegateSyncProcessor;
030import org.apache.camel.spi.Metadata;
031import org.apache.camel.spi.RouteContext;
032import org.apache.camel.util.ObjectHelper;
033
034/**
035 * Calls a Camel processor
036 *
037 * @version 
038 */
039@Metadata(label = "eip,endpoint")
040@XmlRootElement(name = "process")
041@XmlAccessorType(XmlAccessType.FIELD)
042public class ProcessDefinition extends NoOutputDefinition<ProcessDefinition> {
043    @XmlAttribute(required = true)
044    private String ref;
045    @XmlTransient
046    private Processor processor;
047
048    public ProcessDefinition() {
049    }
050
051    public ProcessDefinition(Processor processor) {
052        this.processor = processor;
053    }
054
055    @Override
056    public String toString() {
057        if (ref != null) {
058            return "process[ref:" + ref + "]";
059        } else {
060            // do not invoke toString on the processor as we do not know what it would do
061            String id = ObjectHelper.getIdentityHashCode(processor);
062            return "process[Processor@" + id + "]";
063        }
064    }
065
066    @Override
067    public String getLabel() {
068        if (ref != null) {
069            return "ref:" + ref;
070        } else if (processor != null) {
071            // do not invoke toString on the processor as we do not know what it would do
072            String id = ObjectHelper.getIdentityHashCode(processor);
073            return "Processor@" + id;
074        } else {
075            return "";
076        }
077    }
078
079    public String getRef() {
080        return ref;
081    }
082
083    /**
084     * Reference to the {@link Processor} to lookup in the registry to use.
085     */
086    public void setRef(String ref) {
087        this.ref = ref;
088    }
089
090    @Override
091    public Processor createProcessor(RouteContext routeContext) {
092        Processor answer = processor;
093        if (processor == null) {
094            ObjectHelper.notNull(ref, "ref", this);
095            answer = routeContext.mandatoryLookup(getRef(), Processor.class);
096        }
097
098        // ensure its wrapped in a Service so we can manage it from eg. JMX
099        // (a Processor must be a Service to be enlisted in JMX)
100        if (!(answer instanceof Service)) {
101            if (answer instanceof AsyncProcessor) {
102                // the processor is async by nature so use the async delegate
103                answer = new DelegateAsyncProcessor(answer);
104            } else {
105                // the processor is sync by nature so use the sync delegate
106                answer = new DelegateSyncProcessor(answer);
107            }
108        }
109        return answer;
110    }
111}