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.aggregate.jdbc;
018    
019    import java.io.ByteArrayInputStream;
020    import java.io.ByteArrayOutputStream;
021    import java.io.IOException;
022    import java.io.ObjectInputStream;
023    import java.io.ObjectOutputStream;
024    
025    import org.apache.camel.CamelContext;
026    import org.apache.camel.Endpoint;
027    import org.apache.camel.Exchange;
028    import org.apache.camel.impl.DefaultExchange;
029    import org.apache.camel.impl.DefaultExchangeHolder;
030    
031    /**
032     * Adapted from HawtDBCamelCodec
033     */
034    public final class JdbcCamelCodec {
035    
036        public byte[] marshallExchange(CamelContext camelContext, Exchange exchange) throws IOException {
037            // use DefaultExchangeHolder to marshal to a serialized object
038            DefaultExchangeHolder pe = DefaultExchangeHolder.marshal(exchange, false);
039            // add the aggregated size and timeout property as the only properties we want to retain
040            DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_SIZE, exchange.getProperty(Exchange.AGGREGATED_SIZE, Integer.class));
041            DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_TIMEOUT, exchange.getProperty(Exchange.AGGREGATED_TIMEOUT, Long.class));
042            // add the aggregated completed by property to retain
043            DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_COMPLETED_BY, exchange.getProperty(Exchange.AGGREGATED_COMPLETED_BY, String.class));
044            // add the aggregated correlation key property to retain
045            DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_CORRELATION_KEY, exchange.getProperty(Exchange.AGGREGATED_CORRELATION_KEY, String.class));
046            // persist the from endpoint as well
047            if (exchange.getFromEndpoint() != null) {
048                DefaultExchangeHolder.addProperty(pe, "CamelAggregatedFromEndpoint", exchange.getFromEndpoint().getEndpointUri());
049            }
050            return encode(pe);
051        }
052    
053        public Exchange unmarshallExchange(CamelContext camelContext, byte[] buffer) throws IOException, ClassNotFoundException {
054            DefaultExchangeHolder pe = decode(camelContext, buffer);
055            Exchange answer = new DefaultExchange(camelContext);
056            DefaultExchangeHolder.unmarshal(answer, pe);
057            // restore the from endpoint
058            String fromEndpointUri = (String) answer.removeProperty("CamelAggregatedFromEndpoint");
059            if (fromEndpointUri != null) {
060                Endpoint fromEndpoint = camelContext.hasEndpoint(fromEndpointUri);
061                if (fromEndpoint != null) {
062                    answer.setFromEndpoint(fromEndpoint);
063                }
064            }
065            return answer;
066        }
067    
068        private byte[] encode(Object object) throws IOException {
069            ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
070            ObjectOutputStream objectOut = new ObjectOutputStream(bytesOut);
071            objectOut.writeObject(object);
072            objectOut.close();
073            byte[] data = bytesOut.toByteArray();
074            return data;
075        }
076    
077        private DefaultExchangeHolder decode(CamelContext camelContext, byte[] dataIn) throws IOException, ClassNotFoundException {
078            ByteArrayInputStream bytesIn = new ByteArrayInputStream(dataIn);
079            ObjectInputStream objectIn = new ClassLoadingAwareObjectInputStream(camelContext, bytesIn);
080            Object obj = objectIn.readObject();
081            return (DefaultExchangeHolder) obj;
082        }
083    
084    }
085