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