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 property as the only property we want to retain
040            DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_SIZE, exchange.getProperty(Exchange.AGGREGATED_SIZE, Integer.class));
041            // add the aggregated completed by property to retain
042            DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_COMPLETED_BY, exchange.getProperty(Exchange.AGGREGATED_COMPLETED_BY, String.class));
043            // add the aggregated correlation key property to retain
044            DefaultExchangeHolder.addProperty(pe, Exchange.AGGREGATED_CORRELATION_KEY, exchange.getProperty(Exchange.AGGREGATED_CORRELATION_KEY, String.class));
045            // persist the from endpoint as well
046            if (exchange.getFromEndpoint() != null) {
047                DefaultExchangeHolder.addProperty(pe, "CamelAggregatedFromEndpoint", exchange.getFromEndpoint().getEndpointUri());
048            }
049            return encode(pe);
050        }
051    
052        public Exchange unmarshallExchange(CamelContext camelContext, byte[] buffer) throws IOException, ClassNotFoundException {
053            DefaultExchangeHolder pe = decode(buffer);
054            Exchange answer = new DefaultExchange(camelContext);
055            DefaultExchangeHolder.unmarshal(answer, pe);
056            // restore the from endpoint
057            String fromEndpointUri = (String) answer.removeProperty("CamelAggregatedFromEndpoint");
058            if (fromEndpointUri != null) {
059                Endpoint fromEndpoint = camelContext.hasEndpoint(fromEndpointUri);
060                if (fromEndpoint != null) {
061                    answer.setFromEndpoint(fromEndpoint);
062                }
063            }
064            return answer;
065        }
066    
067        private byte[] encode(Object object) throws IOException {
068            ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
069            ObjectOutputStream objectOut = new ObjectOutputStream(bytesOut);
070            objectOut.writeObject(object);
071            objectOut.close();
072            byte[] data = bytesOut.toByteArray();
073            return data;
074        }
075    
076        private DefaultExchangeHolder decode(byte[] dataIn) throws IOException, ClassNotFoundException {
077            ByteArrayInputStream bytesIn = new ByteArrayInputStream(dataIn);
078            ObjectInputStream objectIn = new ClassLoadingAwareObjectInputStream(bytesIn);
079            Object obj = objectIn.readObject();
080            return (DefaultExchangeHolder) obj;
081        }
082    
083    }
084