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 */ 017package org.apache.camel.model.dataformat; 018 019import javax.xml.bind.annotation.XmlAccessType; 020import javax.xml.bind.annotation.XmlAccessorType; 021import javax.xml.bind.annotation.XmlAttribute; 022import javax.xml.bind.annotation.XmlRootElement; 023import javax.xml.bind.annotation.XmlTransient; 024 025import org.apache.camel.CamelContext; 026import org.apache.camel.model.DataFormatDefinition; 027import org.apache.camel.spi.DataFormat; 028import org.apache.camel.spi.Metadata; 029import org.apache.camel.spi.RouteContext; 030import org.apache.camel.util.ObjectHelper; 031 032/** 033 * Bindy data format 034 * 035 * @version 036 */ 037@Metadata(label = "dataformat,transformation", title = "Bindy") 038@XmlRootElement(name = "bindy") 039@XmlAccessorType(XmlAccessType.FIELD) 040public class BindyDataFormat extends DataFormatDefinition { 041 @XmlAttribute(required = true) 042 private BindyType type; 043 @XmlAttribute 044 private String[] packages; 045 @XmlAttribute 046 private String classType; 047 @XmlAttribute 048 private String locale; 049 @XmlTransient 050 private Class<?> clazz; 051 052 public BindyDataFormat() { 053 super("bindy"); 054 } 055 056 public BindyType getType() { 057 return type; 058 } 059 060 /** 061 * Whether to use csv, fixed or key value pairs mode. 062 */ 063 public void setType(BindyType type) { 064 this.type = type; 065 } 066 067 public String[] getPackages() { 068 return packages; 069 } 070 071 /** 072 * The java package names to scan for model classes. 073 */ 074 public void setPackages(String[] packages) { 075 this.packages = packages; 076 } 077 078 public String getClassType() { 079 return classType; 080 } 081 082 /** 083 * Name of model class to use. 084 */ 085 public void setClassType(String classType) { 086 this.classType = classType; 087 } 088 089 public void setClassType(Class<?> classType) { 090 this.clazz = classType; 091 } 092 093 public String getLocale() { 094 return locale; 095 } 096 097 /** 098 * To configure a default locale to use, such as <tt>us</tt> for united states. 099 * <p/> 100 * To use the JVM platform default locale then use the name <tt>default</tt> 101 */ 102 public void setLocale(String locale) { 103 this.locale = locale; 104 } 105 106 protected DataFormat createDataFormat(RouteContext routeContext) { 107 if (packages == null && (classType == null && clazz == null)) { 108 throw new IllegalArgumentException("Either packages or classType must be specified"); 109 } 110 if (packages != null && (classType != null || clazz != null)) { 111 throw new IllegalArgumentException("Only one of packages and classType must be specified"); 112 } 113 114 if (type == BindyType.Csv) { 115 setDataFormatName("bindy-csv"); 116 } else if (type == BindyType.Fixed) { 117 setDataFormatName("bindy-fixed"); 118 } else { 119 setDataFormatName("bindy-kvp"); 120 } 121 122 if (clazz == null && classType != null) { 123 try { 124 clazz = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(classType); 125 } catch (ClassNotFoundException e) { 126 throw ObjectHelper.wrapRuntimeCamelException(e); 127 } 128 } 129 return super.createDataFormat(routeContext); 130 } 131 132 @Override 133 protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) { 134 setProperty(camelContext, dataFormat, "packages", packages); 135 setProperty(camelContext, dataFormat, "locale", locale); 136 setProperty(camelContext, dataFormat, "classType", clazz); 137 } 138 139}