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