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.tck;
018    
019    import java.util.HashMap;
020    import java.util.Iterator;
021    import java.util.Map;
022    
023    import javax.jbi.messaging.ExchangeStatus;
024    import javax.jbi.messaging.MessageExchange;
025    
026    import junit.framework.Assert;
027    
028    import org.apache.servicemix.jbi.event.ExchangeEvent;
029    import org.apache.servicemix.jbi.event.ExchangeListener;
030    
031    public class ExchangeCompletedListener extends Assert implements ExchangeListener {
032    
033        private Map exchanges = new HashMap();
034    
035        private long timeout;
036    
037        public ExchangeCompletedListener() {
038            this(1000);
039        }
040    
041        public ExchangeCompletedListener(long timeout) {
042            this.timeout = timeout;
043        }
044    
045        public void exchangeSent(ExchangeEvent event) {
046            synchronized (exchanges) {
047                exchanges.put(event.getExchange().getExchangeId(), event.getExchange());
048                exchanges.notify();
049            }
050        }
051        
052        public void exchangeAccepted(ExchangeEvent event) {
053        }
054    
055        public void assertExchangeCompleted() throws Exception {
056            long start = System.currentTimeMillis();
057            MessageExchange active = null;
058            while (true) {
059                synchronized (exchanges) {
060                    for (Iterator it = exchanges.values().iterator(); it.hasNext();) {
061                        active = null;
062                        MessageExchange me = (MessageExchange) it.next();
063                        if (me.getStatus() == ExchangeStatus.ACTIVE) {
064                            active = me;
065                            break;
066                        }
067                    }
068                    if (active == null) {
069                        break;
070                    }
071                    long remain = timeout - (System.currentTimeMillis() - start);
072                    if (remain <= 0) {
073                        assertTrue("Exchange is ACTIVE: " + active, active.getStatus() != ExchangeStatus.ACTIVE);
074                    } else {
075                        exchanges.wait(remain);
076                    }
077                }
078            }
079        }
080    
081    }