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 String getShortName() {
062            return "convertBodyTo";
063        }
064    
065        @Override
066        public Processor createProcessor(RouteContext routeContext) throws Exception {
067            return new ConvertBodyProcessor(getTypeClass());
068        }
069    
070        @Override
071        public List<ProcessorType<?>> getOutputs() {
072            return Collections.EMPTY_LIST;
073        }    
074        
075        protected Class createTypeClass() {
076            return ObjectHelper.loadClass(getType(), getClass().getClassLoader());
077        }
078    
079        public void setType(String type) {
080            this.type = type;
081        }
082    
083        public String getType() {
084            return type;
085        }
086    
087        public void setTypeClass(Class typeClass) {
088            this.typeClass = typeClass;
089        }
090    
091        public Class getTypeClass() {
092            if (typeClass == null) {
093                setTypeClass(createTypeClass());
094            }
095            return typeClass;
096        }
097    }