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    
024    import org.apache.camel.model.DataFormatDefinition;
025    import org.apache.camel.spi.DataFormat;
026    import org.apache.camel.util.ObjectHelper;
027    
028    /**
029     * Represents the <a href="http://fabric.fusesource.org/documentation/camel/c24io.html">C24IO</a>
030     * {@link DataFormat}
031     *
032     * @version $Revision$
033     */
034    @XmlRootElement(name = "c24io")
035    @XmlAccessorType(XmlAccessType.FIELD)
036    public class C24IODataFormat extends DataFormatDefinition {
037        @XmlAttribute(required = false)
038        private String elementTypeName;
039        @XmlAttribute(required = false)
040        private Class<?> elementType;
041        @XmlAttribute(required = false)
042        private C24IOContentType contentType;
043    
044        public C24IODataFormat() {
045            super("org.fusesource.fabric.camel.c24io.C24IOFormat");
046        }
047    
048        public C24IODataFormat(Class<?> elementType) {
049            this();
050            this.elementType = elementType;
051        }
052    
053        public C24IODataFormat(Class<?> elementType, C24IOContentType contentType) {
054            this();
055            this.elementType = elementType;
056            this.contentType = contentType;
057        }
058    
059        public C24IODataFormat(C24IOContentType contentType) {
060            this();
061            this.contentType = contentType;
062        }
063    
064        // Properties
065        //-------------------------------------------------------------------------
066    
067        public String getElementTypeName() {
068            return elementTypeName;
069        }
070    
071        public void setElementTypeName(String elementTypeName) {
072            this.elementTypeName = elementTypeName;
073        }
074    
075        public C24IOContentType getContentType() {
076            return contentType;
077        }
078    
079        public void setContentType(C24IOContentType contentType) {
080            this.contentType = contentType;
081        }
082    
083        public Class<?> getElementType() {
084            if (elementType == null) {
085                if (elementTypeName != null) {
086                    elementType = ObjectHelper.loadClass(elementTypeName, getClass().getClassLoader());
087                    if (elementType == null) {
088                        throw new IllegalArgumentException("The C24IO Element class " + elementTypeName + " is not on the classpath! Cannot use the dataFormat " + this);
089                    }
090                }
091            }
092            return elementType;
093        }
094    
095        public void setElementType(Class<?> elementType) {
096            this.elementType = elementType;
097        }
098    
099        // Implementation methods
100        //-------------------------------------------------------------------------
101    
102        @Override
103        protected void configureDataFormat(DataFormat dataFormat) {
104            Class<?> type = getElementType();
105            if (type != null) {
106                setProperty(dataFormat, "elementType", type);
107            }
108            C24IOContentType content = getContentType();
109            if (content != null) {
110                setProperty(dataFormat, "contentType", content);
111            }
112        }
113    }