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 getShortName() {
068        return "process";
069    }
070
071    @Override
072    public String getLabel() {
073        if (ref != null) {
074            return "ref:" + ref;
075        } else if (processor != null) {
076            // do not invoke toString on the processor as we do not know what it would do
077            String id = ObjectHelper.getIdentityHashCode(processor);
078            return "Processor@" + id;
079        } else {
080            return "";
081        }
082    }
083
084    public String getRef() {
085        return ref;
086    }
087
088    /**
089     * Reference to the {@link Processor} to lookup in the registry to use.
090     */
091    public void setRef(String ref) {
092        this.ref = ref;
093    }
094
095    @Override
096    public Processor createProcessor(RouteContext routeContext) {
097        Processor answer = processor;
098        if (processor == null) {
099            ObjectHelper.notNull(ref, "ref", this);
100            answer = routeContext.mandatoryLookup(getRef(), Processor.class);
101        }
102
103        // ensure its wrapped in a Service so we can manage it from eg. JMX
104        // (a Processor must be a Service to be enlisted in JMX)
105        if (!(answer instanceof Service)) {
106            if (answer instanceof AsyncProcessor) {
107                // the processor is async by nature so use the async delegate
108                answer = new DelegateAsyncProcessor(answer);
109            } else {
110                // the processor is sync by nature so use the sync delegate
111                answer = new DelegateSyncProcessor(answer);
112            }
113        }
114        return answer;
115    }
116}