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