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.dataformat; 018 019 import javax.xml.bind.annotation.XmlAccessType; 020 import javax.xml.bind.annotation.XmlAccessorType; 021 import javax.xml.bind.annotation.XmlAttribute; 022 import javax.xml.bind.annotation.XmlRootElement; 023 import javax.xml.bind.annotation.XmlTransient; 024 025 import org.apache.camel.model.DataFormatDefinition; 026 import org.apache.camel.spi.DataFormat; 027 import org.apache.camel.spi.RouteContext; 028 import org.apache.camel.util.ObjectHelper; 029 030 /** 031 * Represents the Bindy {@link org.apache.camel.spi.DataFormat} 032 * 033 * @version 034 */ 035 @XmlRootElement(name = "bindy") 036 @XmlAccessorType(XmlAccessType.FIELD) 037 public class BindyDataFormat extends DataFormatDefinition { 038 @XmlAttribute(required = true) 039 private BindyType type; 040 @XmlAttribute 041 private String[] packages; 042 @XmlAttribute 043 private String classType; 044 @XmlAttribute 045 private String locale; 046 @XmlTransient 047 private Class<?> clazz; 048 049 public BindyDataFormat() { 050 } 051 052 public BindyType getType() { 053 return type; 054 } 055 056 public void setType(BindyType type) { 057 this.type = type; 058 } 059 060 public String[] getPackages() { 061 return packages; 062 } 063 064 public void setPackages(String[] packages) { 065 this.packages = packages; 066 } 067 068 public String getClassType() { 069 return classType; 070 } 071 072 public void setClassType(String classType) { 073 this.classType = classType; 074 } 075 076 public void setClassType(Class<?> classType) { 077 this.clazz = classType; 078 } 079 080 public String getLocale() { 081 return locale; 082 } 083 084 public void setLocale(String locale) { 085 this.locale = locale; 086 } 087 088 protected DataFormat createDataFormat(RouteContext routeContext) { 089 if (packages == null && (classType == null && clazz == null)) { 090 throw new IllegalArgumentException("Either packages or classType must be specified"); 091 } 092 if (packages != null && (classType != null || clazz != null)) { 093 throw new IllegalArgumentException("Only one of packages and classType must be specified"); 094 } 095 096 if (type == BindyType.Csv) { 097 setDataFormatName("bindy-csv"); 098 } else if (type == BindyType.Fixed) { 099 setDataFormatName("bindy-fixed"); 100 } else { 101 setDataFormatName("bindy-kvp"); 102 } 103 104 if (clazz == null && classType != null) { 105 try { 106 clazz = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(classType); 107 } catch (ClassNotFoundException e) { 108 throw ObjectHelper.wrapRuntimeCamelException(e); 109 } 110 } 111 return super.createDataFormat(routeContext); 112 } 113 114 @Override 115 protected void configureDataFormat(DataFormat dataFormat) { 116 setProperty(dataFormat, "packages", packages); 117 setProperty(dataFormat, "locale", locale); 118 setProperty(dataFormat, "classType", clazz); 119 } 120 121 }