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.component.dataformat;
018
019import org.apache.camel.AsyncCallback;
020import org.apache.camel.AsyncProcessor;
021import org.apache.camel.Component;
022import org.apache.camel.Consumer;
023import org.apache.camel.Exchange;
024import org.apache.camel.Processor;
025import org.apache.camel.Producer;
026import org.apache.camel.impl.DefaultAsyncProducer;
027import org.apache.camel.impl.DefaultEndpoint;
028import org.apache.camel.processor.MarshalProcessor;
029import org.apache.camel.processor.UnmarshalProcessor;
030import org.apache.camel.spi.DataFormat;
031import org.apache.camel.spi.Metadata;
032import org.apache.camel.spi.UriEndpoint;
033import org.apache.camel.spi.UriPath;
034import org.apache.camel.util.ServiceHelper;
035
036/**
037 * The dataformat component is used for working with Data Formats as if it was a regular Component supporting Endpoints and URIs.
038 */
039@UriEndpoint(firstVersion = "2.12.0", scheme = "dataformat", title = "Data Format", syntax = "dataformat:name:operation", producerOnly = true,
040        label = "core,transformation", lenientProperties = true)
041public class DataFormatEndpoint extends DefaultEndpoint {
042
043    private AsyncProcessor processor;
044    private DataFormat dataFormat;
045
046    @UriPath(description = "Name of data format") @Metadata(required = "true")
047    private String name;
048    @UriPath(enums = "marshal,unmarshal") @Metadata(required = "true")
049    private String operation;
050
051    public DataFormatEndpoint() {
052    }
053
054    public DataFormatEndpoint(String endpointUri, Component component, DataFormat dataFormat) {
055        super(endpointUri, component);
056        this.dataFormat = dataFormat;
057    }
058
059    public String getName() {
060        return name;
061    }
062
063    public void setName(String name) {
064        this.name = name;
065    }
066
067    public DataFormat getDataFormat() {
068        return dataFormat;
069    }
070
071    public void setDataFormat(DataFormat dataFormat) {
072        this.dataFormat = dataFormat;
073    }
074
075    public String getOperation() {
076        return operation;
077    }
078
079    /**
080     * Operation to use either marshal or unmarshal
081     */
082    public void setOperation(String operation) {
083        this.operation = operation;
084    }
085
086    @Override
087    public Producer createProducer() throws Exception {
088        return new DefaultAsyncProducer(this) {
089            @Override
090            public boolean process(Exchange exchange, AsyncCallback callback) {
091                return processor.process(exchange, callback);
092            }
093
094            @Override
095            public String toString() {
096                return "DataFormatProducer[" + dataFormat + "]";
097            }
098        };
099    }
100
101    @Override
102    public Consumer createConsumer(Processor processor) throws Exception {
103        throw new UnsupportedOperationException("Cannot consume from data format");
104    }
105
106    @Override
107    public boolean isSingleton() {
108        return true;
109    }
110
111    @Override
112    public boolean isLenientProperties() {
113        return true;
114    }
115
116    @Override
117    protected void doStart() throws Exception {
118        if (dataFormat == null && name != null) {
119            dataFormat = getCamelContext().resolveDataFormat(name);
120        }
121        if (operation.equals("marshal")) {
122            MarshalProcessor marshal = new MarshalProcessor(dataFormat);
123            marshal.setCamelContext(getCamelContext());
124
125            processor = marshal;
126        } else {
127            UnmarshalProcessor unmarshal = new UnmarshalProcessor(dataFormat);
128            unmarshal.setCamelContext(getCamelContext());
129
130            processor = unmarshal;
131        }
132
133        ServiceHelper.startServices(dataFormat, processor);
134        super.doStart();
135    }
136
137    @Override
138    protected void doStop() throws Exception {
139        ServiceHelper.stopServices(processor, dataFormat);
140        super.doStop();
141    }
142}