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.Component;
021import org.apache.camel.Consumer;
022import org.apache.camel.Exchange;
023import org.apache.camel.Processor;
024import org.apache.camel.Producer;
025import org.apache.camel.impl.DefaultAsyncProducer;
026import org.apache.camel.impl.DefaultEndpoint;
027import org.apache.camel.processor.MarshalProcessor;
028import org.apache.camel.processor.UnmarshalProcessor;
029import org.apache.camel.spi.DataFormat;
030import org.apache.camel.spi.Metadata;
031import org.apache.camel.spi.UriEndpoint;
032import org.apache.camel.spi.UriPath;
033import org.apache.camel.util.ServiceHelper;
034
035/**
036 * The dataformat component is used for working with Data Formats as if it was a regular Component supporting Endpoints and URIs.
037 */
038@UriEndpoint(scheme = "dataformat", title = "Data Format", syntax = "dataformat:name:operation", producerOnly = true,
039        label = "core,transformation", lenientProperties = true)
040public class DataFormatEndpoint extends DefaultEndpoint {
041
042    private MarshalProcessor marshal;
043    private UnmarshalProcessor unmarshal;
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                if (marshal != null) {
092                    return marshal.process(exchange, callback);
093                } else {
094                    return unmarshal.process(exchange, callback);
095                }
096            }
097
098            @Override
099            public String toString() {
100                return "DataFormatProducer[" + dataFormat + "]";
101            }
102        };
103    }
104
105    @Override
106    public Consumer createConsumer(Processor processor) throws Exception {
107        throw new UnsupportedOperationException("Cannot consume from data format");
108    }
109
110    @Override
111    public boolean isSingleton() {
112        return true;
113    }
114
115    @Override
116    public boolean isLenientProperties() {
117        return true;
118    }
119
120    @Override
121    protected void doStart() throws Exception {
122        if (dataFormat == null && name != null) {
123            dataFormat = getCamelContext().resolveDataFormat(name);
124        }
125        if (operation.equals("marshal")) {
126            marshal = new MarshalProcessor(dataFormat);
127            marshal.setCamelContext(getCamelContext());
128        } else {
129            unmarshal = new UnmarshalProcessor(dataFormat);
130            unmarshal.setCamelContext(getCamelContext());
131        }
132
133        ServiceHelper.startServices(dataFormat, marshal, unmarshal);
134        super.doStart();
135    }
136
137    @Override
138    protected void doStop() throws Exception {
139        ServiceHelper.stopServices(marshal, unmarshal, dataFormat);
140        super.doStop();
141    }
142}