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@UriEndpoint(scheme = "dataformat", syntax = "dataformat:name:operation", producerOnly = true, label = "core,transformation")
036public class DataFormatEndpoint extends DefaultEndpoint {
037
038    private MarshalProcessor marshal;
039    private UnmarshalProcessor unmarshal;
040    private DataFormat dataFormat;
041
042    @UriPath(description = "Name of data format") @Metadata(required = "true")
043    private String name;
044    @UriPath(enums = "marshal,unmarshal") @Metadata(required = "true")
045    private String operation;
046
047    public DataFormatEndpoint() {
048    }
049
050    public DataFormatEndpoint(String endpointUri, Component component, DataFormat dataFormat) {
051        super(endpointUri, component);
052        this.dataFormat = dataFormat;
053    }
054
055    public String getName() {
056        return name;
057    }
058
059    public void setName(String name) {
060        this.name = name;
061    }
062
063    public DataFormat getDataFormat() {
064        return dataFormat;
065    }
066
067    public void setDataFormat(DataFormat dataFormat) {
068        this.dataFormat = dataFormat;
069    }
070
071    public String getOperation() {
072        return operation;
073    }
074
075    /**
076     * Operation to use either marshal or unmarshal
077     */
078    public void setOperation(String operation) {
079        this.operation = operation;
080    }
081
082    @Override
083    public Producer createProducer() throws Exception {
084        return new DefaultAsyncProducer(this) {
085            @Override
086            public boolean process(Exchange exchange, AsyncCallback callback) {
087                if (marshal != null) {
088                    return marshal.process(exchange, callback);
089                } else {
090                    return unmarshal.process(exchange, callback);
091                }
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    protected void doStart() throws Exception {
113        if (dataFormat == null && name != null) {
114            dataFormat = getCamelContext().resolveDataFormat(name);
115        }
116        if (operation.equals("marshal")) {
117            marshal = new MarshalProcessor(dataFormat);
118            marshal.setCamelContext(getCamelContext());
119        } else {
120            unmarshal = new UnmarshalProcessor(dataFormat);
121            unmarshal.setCamelContext(getCamelContext());
122        }
123
124        ServiceHelper.startServices(dataFormat, marshal, unmarshal);
125        super.doStart();
126    }
127
128    @Override
129    protected void doStop() throws Exception {
130        ServiceHelper.stopServices(marshal, unmarshal, dataFormat);
131        super.doStop();
132    }
133}