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.servicemix.jbi.messaging;
018    
019    import java.io.IOException;
020    import java.io.ObjectInput;
021    
022    import javax.jbi.messaging.InOptionalOut;
023    
024    /**
025     * InOptionalOut message exchange.
026     * 
027     * @version $Revision: 564607 $
028     */
029    public class InOptionalOutImpl extends MessageExchangeImpl implements InOptionalOut {
030    
031        private static final long serialVersionUID = -3145649037372074912L;
032    
033        private static final int[][] STATES_CONSUMER = {
034            {CAN_CONSUMER + CAN_OWNER + CAN_SET_IN_MSG + CAN_SEND + CAN_STATUS_ACTIVE, 1, -1, -1, -1 },
035            {CAN_CONSUMER, 2, 3, 4, 4 },
036            {CAN_CONSUMER + CAN_OWNER + CAN_SEND + CAN_SET_FAULT_MSG + CAN_STATUS_ACTIVE
037                        + CAN_STATUS_ERROR + CAN_STATUS_DONE, -1, 5, 6, 6 }, 
038            {CAN_CONSUMER + CAN_OWNER + CAN_SEND + CAN_STATUS_ERROR + CAN_STATUS_DONE, -1, -1, 6, 6 },
039            {CAN_CONSUMER + CAN_OWNER, -1, -1, -1, -1 }, 
040            {CAN_CONSUMER, -1, -1, 7, 7 }, 
041            {CAN_CONSUMER, -1, -1, -1, -1 },
042            {CAN_CONSUMER + CAN_OWNER, -1, -1, -1, -1 }
043        };
044    
045        private static final int[][] STATES_PROVIDER = {
046            {CAN_PROVIDER, 1, -1, -1 },
047            {CAN_PROVIDER + CAN_OWNER + CAN_SET_OUT_MSG + CAN_SET_FAULT_MSG + CAN_SEND
048                        + CAN_STATUS_ACTIVE + CAN_STATUS_ERROR + CAN_STATUS_DONE, 2, 3, 4, 4 }, 
049            {CAN_PROVIDER, -1, 5, 6, 6 },
050            {CAN_PROVIDER, -1, -1, 6, 6 }, 
051            {CAN_PROVIDER, -1, -1, -1, -1 },
052            {CAN_PROVIDER + CAN_OWNER + CAN_SEND + CAN_STATUS_ERROR + CAN_STATUS_DONE, -1, -1, 7, 7 },
053            {CAN_PROVIDER + CAN_OWNER, -1, -1, -1, -1 }, 
054            {CAN_PROVIDER, -1, -1, -1, -1 }
055        };
056    
057        public InOptionalOutImpl() {
058        }
059    
060        public InOptionalOutImpl(String exchangeId) {
061            super(exchangeId, MessageExchangeSupport.IN_OPTIONAL_OUT, STATES_CONSUMER);
062            this.mirror = new InOptionalOutImpl(this);
063        }
064    
065        public InOptionalOutImpl(ExchangePacket packet) {
066            super(packet, STATES_CONSUMER);
067            this.mirror = new InOptionalOutImpl(this);
068        }
069    
070        protected InOptionalOutImpl(InOptionalOutImpl mep) {
071            super(mep.packet, STATES_PROVIDER);
072            this.mirror = mep;
073        }
074    
075        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
076            this.packet = new ExchangePacket();
077            this.packet.readExternal(in);
078            if (this.packet.in != null) {
079                this.packet.in.exchange = this;
080            }
081            if (this.packet.out != null) {
082                this.packet.out.exchange = this;
083            }
084            if (this.packet.fault != null) {
085                this.packet.fault.exchange = this;
086            }
087            this.state = in.read();
088            this.mirror = new InOptionalOutImpl();
089            this.mirror.mirror = this;
090            this.mirror.packet = this.packet;
091            this.mirror.state = in.read();
092            boolean provider = in.readBoolean();
093            if (provider) {
094                this.states = STATES_PROVIDER;
095                this.mirror.states = STATES_CONSUMER;
096            } else {
097                this.states = STATES_CONSUMER;
098                this.mirror.states = STATES_PROVIDER;
099            }
100        }
101    
102    }