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