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.bean.support;
018
019 import java.lang.reflect.Method;
020 import java.util.Collections;
021 import java.util.HashMap;
022 import java.util.HashSet;
023 import java.util.Map;
024 import java.util.Set;
025
026 import javax.jbi.messaging.ExchangeStatus;
027 import javax.jbi.messaging.MessageExchange;
028
029 import org.apache.servicemix.bean.BeanEndpoint;
030
031 public class Request {
032 private final Object bean;
033 // Keep track of callbacks already called, so that the same callback
034 // can not be called twice
035 private Map<Method, Boolean> callbacks;
036 private final Object correlationId;
037 private final Set<MessageExchange> exchanges = new HashSet<MessageExchange>();
038
039 public Request(Object correlationId, Object bean, MessageExchange exchange) {
040 this.correlationId = correlationId;
041 this.bean = bean;
042 exchanges.add(exchange);
043 }
044
045 /**
046 * @return the bean
047 */
048 public Object getBean() {
049 return bean;
050 }
051
052 public Object getCorrelationId() {
053 return correlationId;
054 }
055
056 /**
057 * @return the callbacks
058 */
059 public Map<Method, Boolean> getCallbacks() {
060 if (callbacks == null) {
061 callbacks = new HashMap<Method, Boolean>();
062 }
063 return callbacks;
064 }
065
066 /**
067 * Check if this request is completely finished.
068 *
069 * @return <code>true</code> if both the Exchange is DONE and there are no more outstanding sent exchanges
070 */
071 public boolean isFinished() {
072 for (MessageExchange exchange : exchanges) {
073 if (ExchangeStatus.ACTIVE.equals(exchange.getStatus())) {
074 return false;
075 }
076 }
077 return true;
078 }
079
080 /**
081 * Add an exchange to this request. All exchanges that are added to the request have to be finished
082 * @param exchange
083 */
084 public void addExchange(MessageExchange exchange) {
085 exchanges.add(exchange);
086 exchange.setProperty(BeanEndpoint.CORRELATION_ID, correlationId);
087 }
088
089 /**
090 * Get all the MessageExchanges that are involved in this request
091 *
092 * @return an unmodifiable list of {@link MessageExchange}s
093 */
094 public Set<MessageExchange> getExchanges() {
095 return Collections.unmodifiableSet(exchanges);
096 }
097 }