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 java.util.Collections;
020    import java.util.List;
021    
022    import javax.xml.bind.annotation.XmlAccessType;
023    import javax.xml.bind.annotation.XmlAccessorType;
024    import javax.xml.bind.annotation.XmlAttribute;
025    import javax.xml.bind.annotation.XmlRootElement;
026    import javax.xml.bind.annotation.XmlTransient;
027    
028    import org.apache.camel.Processor;
029    import org.apache.camel.processor.ConvertBodyProcessor;
030    import org.apache.camel.spi.RouteContext;
031    import org.apache.camel.util.ObjectHelper;
032    
033    /**
034     * Represents an XML <convertBodyTo/> element
035     */
036    @XmlRootElement(name = "convertBodyTo")
037    @XmlAccessorType(XmlAccessType.FIELD)
038    public class ConvertBodyType extends ProcessorType<ProcessorType> {
039        @XmlAttribute
040        private String type;
041        @XmlTransient
042        private Class typeClass;
043    
044        public ConvertBodyType() {
045        }
046    
047        public ConvertBodyType(String type) {
048            setType(type);
049        }
050    
051        public ConvertBodyType(Class typeClass) {
052            setTypeClass(typeClass);
053        }
054    
055        @Override
056        public String toString() {
057            return "convertBodyTo[ " + getType() + "]";
058        }
059    
060        @Override
061        public Processor createProcessor(RouteContext routeContext) throws Exception {
062            return new ConvertBodyProcessor(getTypeClass());
063        }
064    
065        @Override
066        public List<ProcessorType<?>> getOutputs() {
067            return Collections.EMPTY_LIST;
068        }    
069        
070        protected Class createTypeClass() {
071            return ObjectHelper.loadClass(getType(), getClass().getClassLoader());
072        }
073    
074        public void setType(String type) {
075            this.type = type;
076        }
077    
078        public String getType() {
079            return type;
080        }
081    
082        public void setTypeClass(Class typeClass) {
083            this.typeClass = typeClass;
084        }
085    
086        public Class getTypeClass() {
087            if (typeClass == null) {
088                setTypeClass(createTypeClass());
089            }
090            return typeClass;
091        }
092    }