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.processor.binding;
018    
019    import org.apache.camel.Processor;
020    import org.apache.camel.processor.MarshalProcessor;
021    import org.apache.camel.processor.UnmarshalProcessor;
022    import org.apache.camel.spi.Binding;
023    import org.apache.camel.spi.DataFormat;
024    import org.apache.camel.support.ServiceSupport;
025    import org.apache.camel.util.ObjectHelper;
026    
027    /**
028     * Represents a {@link org.apache.camel.spi.Binding} which Marshals the message in the ProduceProcessor and
029     * Unmarshals the message in the ConsumeProcessor
030     */
031    public class DataFormatBinding extends ServiceSupport implements Binding {
032        private DataFormat producerDataFormat;
033        private DataFormat consumerDataFormat;
034    
035        public DataFormatBinding() {
036        }
037    
038        public DataFormatBinding(DataFormat dataFormat) {
039            this(dataFormat, dataFormat);
040        }
041    
042        public DataFormatBinding(DataFormat consumerDataFormat, DataFormat producerDataFormat) {
043            this.consumerDataFormat = consumerDataFormat;
044            this.producerDataFormat = producerDataFormat;
045        }
046    
047        @Override
048        public Processor createProduceProcessor() {
049            ObjectHelper.notNull(producerDataFormat, "producerDataFormat");
050            return new MarshalProcessor(producerDataFormat);
051        }
052    
053        @Override
054        public Processor createConsumeProcessor() {
055            ObjectHelper.notNull(consumerDataFormat, "consumerDataFormat");
056            return new UnmarshalProcessor(consumerDataFormat);
057        }
058    
059        /**
060         * Sets the data format for both producer and consumer sides
061         */
062        public void setDataFormat(DataFormat dataFormat) {
063            setConsumerDataFormat(dataFormat);
064            setProducerDataFormat(dataFormat);
065        }
066    
067        public DataFormat getConsumerDataFormat() {
068            return consumerDataFormat;
069        }
070    
071        public void setConsumerDataFormat(DataFormat consumerDataFormat) {
072            this.consumerDataFormat = consumerDataFormat;
073        }
074    
075        public DataFormat getProducerDataFormat() {
076            return producerDataFormat;
077        }
078    
079        public void setProducerDataFormat(DataFormat producerDataFormat) {
080            this.producerDataFormat = producerDataFormat;
081        }
082    
083        @Override
084        protected void doStart() throws Exception {
085            // noop
086        }
087    
088        @Override
089        protected void doStop() throws Exception {
090            // noop
091        }
092    }