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.component.hawtdb;
018
019 import java.io.IOException;
020 import java.io.Serializable;
021
022 import org.apache.camel.CamelContext;
023 import org.apache.camel.Endpoint;
024 import org.apache.camel.Exchange;
025 import org.apache.camel.impl.DefaultExchange;
026 import org.apache.camel.impl.DefaultExchangeHolder;
027 import org.fusesource.hawtbuf.Buffer;
028 import org.fusesource.hawtbuf.DataByteArrayInputStream;
029 import org.fusesource.hawtbuf.DataByteArrayOutputStream;
030 import org.fusesource.hawtbuf.codec.Codec;
031 import org.fusesource.hawtbuf.codec.ObjectCodec;
032 import org.fusesource.hawtbuf.codec.StringCodec;
033
034 /**
035 * @version $Revision: 20171 $
036 */
037 public final class HawtDBCamelCodec {
038
039 private Codec<String> keyCodec = new StringCodec();
040 private Codec<DefaultExchangeHolder> exchangeCodec = new ObjectCodec<DefaultExchangeHolder>();
041
042 public Buffer marshallKey(String key) throws IOException {
043 DataByteArrayOutputStream baos = new DataByteArrayOutputStream();
044 keyCodec.encode(key, baos);
045 return baos.toBuffer();
046 }
047
048 public String unmarshallKey(Buffer buffer) throws IOException {
049 DataByteArrayInputStream bais = new DataByteArrayInputStream(buffer);
050 String key = keyCodec.decode(bais);
051 return key;
052 }
053
054 public Buffer marshallExchange(CamelContext camelContext, Exchange exchange) throws IOException {
055 DataByteArrayOutputStream baos = new DataByteArrayOutputStream();
056 // use DefaultExchangeHolder to marshal to a serialized object
057 DefaultExchangeHolder pe = DefaultExchangeHolder.marshal(exchange, false);
058 // add the aggregated size property as the only property we want to retain
059 DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_SIZE, exchange.getProperty(Exchange.AGGREGATED_SIZE, Integer.class));
060 // add the aggregated completed by property to retain
061 DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_COMPLETED_BY, exchange.getProperty(Exchange.AGGREGATED_COMPLETED_BY, String.class));
062 // add the aggregated correlation key property to retain
063 DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_CORRELATION_KEY, exchange.getProperty(Exchange.AGGREGATED_CORRELATION_KEY, String.class));
064 // persist the from endpoint as well
065 if (exchange.getFromEndpoint() != null) {
066 DefaultExchangeHolder.addProperty(pe, "CamelAggregatedFromEndpoint", exchange.getFromEndpoint().getEndpointUri());
067 }
068 exchangeCodec.encode(pe, baos);
069 return baos.toBuffer();
070 }
071
072 public Exchange unmarshallExchange(CamelContext camelContext, Buffer buffer) throws IOException {
073 DataByteArrayInputStream bais = new DataByteArrayInputStream(buffer);
074 DefaultExchangeHolder pe = exchangeCodec.decode(bais);
075 Exchange answer = new DefaultExchange(camelContext);
076 DefaultExchangeHolder.unmarshal(answer, pe);
077 // restore the from endpoint
078 String fromEndpointUri = (String) answer.removeProperty("CamelAggregatedFromEndpoint");
079 if (fromEndpointUri != null) {
080 Endpoint fromEndpoint = camelContext.hasEndpoint(fromEndpointUri);
081 if (fromEndpoint != null) {
082 answer.setFromEndpoint(fromEndpoint);
083 }
084 }
085 return answer;
086 }
087
088 }