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.UriEndpoint;
031import org.apache.camel.spi.UriParam;
032import org.apache.camel.util.ServiceHelper;
033
034@UriEndpoint(scheme = "dataformat")
035public class DataFormatEndpoint extends DefaultEndpoint {
036
037    private MarshalProcessor marshal;
038    private UnmarshalProcessor unmarshal;
039    @UriParam
040    private DataFormat dataFormat;
041    @UriParam
042    private String operation;
043
044    public DataFormatEndpoint() {
045    }
046
047    public DataFormatEndpoint(String endpointUri, Component component, DataFormat dataFormat) {
048        super(endpointUri, component);
049        this.dataFormat = dataFormat;
050    }
051
052    public DataFormat getDataFormat() {
053        return dataFormat;
054    }
055
056    public void setDataFormat(DataFormat dataFormat) {
057        this.dataFormat = dataFormat;
058    }
059
060    public String getOperation() {
061        return operation;
062    }
063
064    public void setOperation(String operation) {
065        this.operation = operation;
066    }
067
068    @Override
069    public Producer createProducer() throws Exception {
070        return new DefaultAsyncProducer(this) {
071            @Override
072            public boolean process(Exchange exchange, AsyncCallback callback) {
073                if (marshal != null) {
074                    return marshal.process(exchange, callback);
075                } else {
076                    return unmarshal.process(exchange, callback);
077                }
078            }
079
080            @Override
081            public String toString() {
082                return "DataFormatProducer[" + dataFormat + "]";
083            }
084        };
085    }
086
087    @Override
088    public Consumer createConsumer(Processor processor) throws Exception {
089        throw new UnsupportedOperationException("Cannot consume from data format");
090    }
091
092    @Override
093    public boolean isSingleton() {
094        return true;
095    }
096
097    @Override
098    protected void doStart() throws Exception {
099        if (operation.equals("marshal")) {
100            marshal = new MarshalProcessor(dataFormat);
101            marshal.setCamelContext(getCamelContext());
102        } else {
103            unmarshal = new UnmarshalProcessor(dataFormat);
104            unmarshal.setCamelContext(getCamelContext());
105        }
106
107        ServiceHelper.startServices(dataFormat, marshal, unmarshal);
108        super.doStart();
109    }
110
111    @Override
112    protected void doStop() throws Exception {
113        ServiceHelper.stopServices(marshal, unmarshal, dataFormat);
114        super.doStop();
115    }
116}