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.RuntimeCamelException;
030 import org.apache.camel.processor.ConvertBodyProcessor;
031 import org.apache.camel.spi.RouteContext;
032 import org.apache.camel.util.ObjectHelper;
033
034 /**
035 * Represents an XML <convertBodyTo/> element
036 */
037 @XmlRootElement(name = "convertBodyTo")
038 @XmlAccessorType(XmlAccessType.FIELD)
039 public class ConvertBodyDefinition extends ProcessorDefinition<ProcessorDefinition> {
040 @XmlAttribute
041 private String type;
042 @XmlTransient
043 private Class typeClass;
044
045 public ConvertBodyDefinition() {
046 }
047
048 public ConvertBodyDefinition(String type) {
049 setType(type);
050 }
051
052 public ConvertBodyDefinition(Class typeClass) {
053 setTypeClass(typeClass);
054 setType(typeClass.getName());
055 }
056
057 @Override
058 public String toString() {
059 return "convertBodyTo[" + getType() + "]";
060 }
061
062 @Override
063 public String getShortName() {
064 return "convertBodyTo";
065 }
066
067 @Override
068 public Processor createProcessor(RouteContext routeContext) throws Exception {
069 return new ConvertBodyProcessor(getTypeClass());
070 }
071
072 @Override
073 @SuppressWarnings("unchecked")
074 public List<ProcessorDefinition> getOutputs() {
075 return Collections.EMPTY_LIST;
076 }
077
078 protected Class createTypeClass() {
079 return ObjectHelper.loadClass(getType(), getClass().getClassLoader());
080 }
081
082 public void setType(String type) {
083 this.type = type;
084 }
085
086 public String getType() {
087 return type;
088 }
089
090 public void setTypeClass(Class typeClass) {
091 this.typeClass = typeClass;
092 }
093
094 public Class getTypeClass() {
095 if (typeClass == null) {
096 Class clazz = createTypeClass();
097 if (clazz == null) {
098 throw new RuntimeCamelException("Cannot load the class with the class name: " + getType());
099 }
100 setTypeClass(clazz);
101 }
102 return typeClass;
103 }
104
105 // Fluent API
106 //-------------------------------------------------------------------------
107 /**
108 * Sets the type class that you want ConvertBodyType to covert
109 *
110 * @param typeClass the type class that you want to covert body instance to
111 * @return the builder
112 */
113 public ConvertBodyDefinition typeClass(Class typeClass) {
114 setTypeClass(typeClass);
115 return this;
116 }
117
118 /**
119 * Sets the type class name that you want to covert body instance to
120 *
121 * @param type the type class name
122 * @return the builder
123 */
124 public ConvertBodyDefinition type(String type) {
125 setType(type);
126 return this;
127 }
128 }