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