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    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    
030    /**
031     * Represents the <a href="http://camel.apache.org/artix-data-services.html">Artix Data Services</a>
032     * {@link DataFormat}
033     *
034     * @version $Revision: 782535 $
035     */
036    @XmlRootElement(name = "artixDS")
037    @XmlAccessorType(XmlAccessType.FIELD)
038    public class ArtixDSDataFormat extends DataFormatDefinition {
039        private static final transient Log LOG = LogFactory.getLog(ArtixDSDataFormat.class);
040    
041        @XmlAttribute(required = false)
042        private String elementTypeName;
043        @XmlAttribute(required = false)
044        private String format;
045        @XmlAttribute(required = false)
046        private Class<?> elementType;
047        @XmlAttribute(required = false)
048        private ArtixDSContentType contentType;
049    
050        public ArtixDSDataFormat() {
051            super("org.apache.camel.artix.ds.ArtixDSFormat");
052        }
053    
054        public ArtixDSDataFormat(Class<?> elementType) {
055            this();
056            this.elementType = elementType;
057        }
058    
059        public ArtixDSDataFormat(Class<?> elementType, ArtixDSContentType contentType) {
060            this();
061            this.elementType = elementType;
062            this.contentType = contentType;
063        }
064    
065        public ArtixDSDataFormat(ArtixDSContentType contentType) {
066            this();
067            this.contentType = contentType;
068        }
069    
070        // Properties
071        //-------------------------------------------------------------------------
072    
073        public String getElementTypeName() {
074            return elementTypeName;
075        }
076    
077        public void setElementTypeName(String elementTypeName) {
078            this.elementTypeName = elementTypeName;
079        }
080    
081        public ArtixDSContentType getContentType() {
082            return contentType;
083        }
084    
085        public void setContentType(ArtixDSContentType contentType) {
086            this.contentType = contentType;
087        }
088    
089        public Class<?> getElementType() {
090            if (elementType == null) {
091                if (elementTypeName != null) {
092                    elementType = ObjectHelper.loadClass(elementTypeName, getClass().getClassLoader());
093                    if (elementType == null) {
094                        throw new IllegalArgumentException("The ArtixDS Element class " + elementTypeName + " is not on the classpath! Cannot use the dataFormat " + this);
095                    }
096                }
097            }
098            return elementType;
099        }
100    
101        public void setElementType(Class<?> elementType) {
102            this.elementType = elementType;
103        }
104    
105        public String getFormat() {
106            return format;
107        }
108    
109        public void setFormat(String format) {
110            this.format = format;
111        }
112    
113        // Implementation methods
114        //-------------------------------------------------------------------------
115    
116        @Override
117        protected void configureDataFormat(DataFormat dataFormat) {
118            Class<?> type = getElementType();
119            if (type != null) {
120                setProperty(dataFormat, "elementType", type);
121            }
122            ArtixDSContentType content = getContentType();
123            if (content != null) {
124                setProperty(dataFormat, "contentType", content);
125            }
126        }
127    }