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.drools.model;
018    
019    import javax.jbi.messaging.ExchangeStatus;
020    import javax.jbi.messaging.InOnly;
021    import javax.jbi.messaging.InOptionalOut;
022    import javax.jbi.messaging.InOut;
023    import javax.jbi.messaging.MessageExchange;
024    import javax.jbi.messaging.NormalizedMessage;
025    import javax.jbi.messaging.RobustInOnly;
026    import javax.xml.namespace.NamespaceContext;
027    
028    public class Exchange {
029    
030        public static final String IN_ONLY = "InOnly";
031        public static final String ROBUST_IN_ONLY = "RobustInOnly";
032        public static final String IN_OUT = "InOut";
033        public static final String IN_OPTIONAL_OUT = "InOptionalOut";
034        
035        public static final String ACTIVE = "Active";
036        public static final String ERROR = "Error";
037        public static final String DONE = "Done";
038        
039        private final MessageExchange exchange;
040        private Message in;
041        private Message out;
042        private Message fault;
043        private NamespaceContext namespaceContext;
044        
045        public Exchange(MessageExchange exchange, NamespaceContext namespaceContext) {
046            this.exchange = exchange;
047            this.namespaceContext = namespaceContext;
048            if (in == null) {
049                NormalizedMessage msg = exchange.getMessage("in");
050                in = msg != null ? new Message(msg, this.namespaceContext) : null;
051            }
052            if (out == null) {
053                NormalizedMessage msg = exchange.getMessage("out");
054                out = msg != null ? new Message(msg, this.namespaceContext) : null;
055            }
056            if (fault == null) {
057                javax.jbi.messaging.Fault msg = exchange.getFault();
058                fault = msg != null ? new Fault(msg, this.namespaceContext) : null;
059            }
060        }
061        
062        public MessageExchange getInternalExchange() {
063            return exchange;
064        }
065        
066        public String getMep() {
067            if (exchange instanceof InOnly) {
068                return IN_ONLY;
069            } else if (exchange instanceof RobustInOnly) {
070                return ROBUST_IN_ONLY;
071            } else if (exchange instanceof InOut) {
072                return IN_OUT;
073            } else if (exchange instanceof InOptionalOut) {
074                return IN_OPTIONAL_OUT;
075            } else {
076                return exchange.getPattern().toString();
077            }
078        }
079        
080        public String getStatus() {
081            if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
082                return ACTIVE;
083            } else if (exchange.getStatus() == ExchangeStatus.DONE) {
084                return DONE;
085            } else if (exchange.getStatus() == ExchangeStatus.ERROR) {
086                return ERROR;
087            } else {
088                throw new IllegalStateException("Unkown exchange status");
089            }
090        }
091        
092        public String getOperation() {
093            return exchange.getOperation() != null ? exchange.getOperation().toString() : null;
094        }
095        
096        public Object getProperty(String name) {
097            return exchange.getProperty(name);
098        }
099        
100        public void setProperty(String name, Object value) {
101            exchange.setProperty(name, value);
102        }
103        
104        public Message getIn() {
105            return in;
106        }
107        
108        public Message getOut() {
109            return out;
110        }
111        
112        public Message getFault() {
113            return fault;
114        }
115        
116        protected Message getMessage(String name) {
117            NormalizedMessage msg = exchange.getMessage(name);
118            return msg != null ? new Message(msg, this.namespaceContext) : null;
119        }
120        
121    }
122