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.processor;
018
019import org.apache.camel.AsyncCallback;
020import org.apache.camel.AsyncProcessor;
021import org.apache.camel.CamelContext;
022import org.apache.camel.CamelContextAware;
023import org.apache.camel.Exchange;
024import org.apache.camel.Message;
025import org.apache.camel.Traceable;
026import org.apache.camel.converter.stream.OutputStreamBuilder;
027import org.apache.camel.spi.DataFormat;
028import org.apache.camel.spi.IdAware;
029import org.apache.camel.support.ServiceSupport;
030import org.apache.camel.util.AsyncProcessorHelper;
031import org.apache.camel.util.ObjectHelper;
032import org.apache.camel.util.ServiceHelper;
033
034/**
035 * Marshals the body of the incoming message using the given
036 * <a href="http://camel.apache.org/data-format.html">data format</a>
037 *
038 * @version
039 */
040public class MarshalProcessor extends ServiceSupport implements AsyncProcessor, Traceable, CamelContextAware, IdAware {
041    private String id;
042    private CamelContext camelContext;
043    private final DataFormat dataFormat;
044
045    public MarshalProcessor(DataFormat dataFormat) {
046        this.dataFormat = dataFormat;
047    }
048
049    public void process(Exchange exchange) throws Exception {
050        AsyncProcessorHelper.process(this, exchange);
051    }
052
053    public boolean process(Exchange exchange, AsyncCallback callback) {
054        ObjectHelper.notNull(dataFormat, "dataFormat");
055
056        // if stream caching is enabled then use that so we can stream accordingly
057        // for example to overflow to disk for big streams
058        OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
059
060        Message in = exchange.getIn();
061        Object body = in.getBody();
062
063        // lets setup the out message before we invoke the dataFormat
064        // so that it can mutate it if necessary
065        Message out = exchange.getOut();
066        out.copyFrom(in);
067
068        try {
069            dataFormat.marshal(exchange, body, osb);
070            out.setBody(osb.build());
071        } catch (Throwable e) {
072            // remove OUT message, as an exception occurred
073            exchange.setOut(null);
074            exchange.setException(e);
075        }
076
077        callback.done(true);
078        return true;
079    }
080
081    @Override
082    public String toString() {
083        return "Marshal[" + dataFormat + "]";
084    }
085
086    public String getTraceLabel() {
087        return "marshal[" + dataFormat + "]";
088    }
089
090    public String getId() {
091        return id;
092    }
093
094    public void setId(String id) {
095        this.id = id;
096    }
097
098    public CamelContext getCamelContext() {
099        return camelContext;
100    }
101
102    public void setCamelContext(CamelContext camelContext) {
103        this.camelContext = camelContext;
104    }
105
106    @Override
107    protected void doStart() throws Exception {
108        // inject CamelContext on data format
109        if (dataFormat instanceof CamelContextAware) {
110            ((CamelContextAware) dataFormat).setCamelContext(camelContext);
111        }
112        // add dataFormat as service which will also start the service
113        // (false => we handle the lifecycle of the dataFormat)
114        getCamelContext().addService(dataFormat, false, true);
115    }
116
117    @Override
118    protected void doStop() throws Exception {
119        ServiceHelper.stopService(dataFormat);
120        getCamelContext().removeService(dataFormat);
121    }
122}