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 */
017 package org.apache.camel.processor;
018
019 import java.io.ByteArrayOutputStream;
020
021 import org.apache.camel.CamelContext;
022 import org.apache.camel.CamelContextAware;
023 import org.apache.camel.Exchange;
024 import org.apache.camel.Message;
025 import org.apache.camel.Processor;
026 import org.apache.camel.Traceable;
027 import org.apache.camel.spi.DataFormat;
028 import org.apache.camel.support.ServiceSupport;
029 import org.apache.camel.util.ObjectHelper;
030 import org.apache.camel.util.ServiceHelper;
031
032 /**
033 * Marshals the body of the incoming message using the given
034 * <a href="http://camel.apache.org/data-format.html">data format</a>
035 *
036 * @version
037 */
038 public class MarshalProcessor extends ServiceSupport implements Processor, Traceable, CamelContextAware {
039 private CamelContext camelContext;
040 private final DataFormat dataFormat;
041
042 public MarshalProcessor(DataFormat dataFormat) {
043 this.dataFormat = dataFormat;
044 }
045
046 public void process(Exchange exchange) throws Exception {
047 ObjectHelper.notNull(dataFormat, "dataFormat");
048
049 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
050 Message in = exchange.getIn();
051 Object body = in.getBody();
052
053 // lets setup the out message before we invoke the dataFormat
054 // so that it can mutate it if necessary
055 Message out = exchange.getOut();
056 out.copyFrom(in);
057
058 try {
059 dataFormat.marshal(exchange, body, buffer);
060 byte[] data = buffer.toByteArray();
061 out.setBody(data);
062 } catch (Exception e) {
063 // remove OUT message, as an exception occurred
064 exchange.setOut(null);
065 throw e;
066 }
067 }
068
069 @Override
070 public String toString() {
071 return "Marshal[" + dataFormat + "]";
072 }
073
074 public String getTraceLabel() {
075 return "marshal[" + dataFormat + "]";
076 }
077
078 public CamelContext getCamelContext() {
079 return camelContext;
080 }
081
082 public void setCamelContext(CamelContext camelContext) {
083 this.camelContext = camelContext;
084 }
085
086 @Override
087 protected void doStart() throws Exception {
088 // inject CamelContext on data format
089 if (dataFormat instanceof CamelContextAware) {
090 ((CamelContextAware) dataFormat).setCamelContext(camelContext);
091 }
092 ServiceHelper.startService(dataFormat);
093 }
094
095 @Override
096 protected void doStop() throws Exception {
097 ServiceHelper.stopService(dataFormat);
098 }
099 }