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 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 Object correlationId;
037 private final Set<MessageExchange> exchanges = new HashSet<MessageExchange>();
038
039 public Request() {
040 }
041
042 public Request(Object correlationId, Object bean, MessageExchange exchange) {
043 this.correlationId = correlationId;
044 this.bean = bean;
045 exchanges.add(exchange);
046 }
047
048 /**
049 * @return the bean
050 */
051 public Object getBean() {
052 return bean;
053 }
054
055 /**
056 * @param bean the bean to set
057 */
058 public void setBean(Object bean) {
059 this.bean = bean;
060 }
061
062 public Object getCorrelationId() {
063 return correlationId;
064 }
065
066 /**
067 * @return the callbacks
068 */
069 public Map<Method, Boolean> getCallbacks() {
070 if (callbacks == null) {
071 callbacks = new HashMap<Method, Boolean>();
072 }
073 return callbacks;
074 }
075
076 /**
077 * Check if this request is completely finished.
078 *
079 * @return <code>true</code> if both the Exchange is DONE and there are no more outstanding sent exchanges
080 */
081 public boolean isFinished() {
082 for (MessageExchange exchange : exchanges) {
083 if (ExchangeStatus.ACTIVE.equals(exchange.getStatus())) {
084 return false;
085 }
086 }
087 return true;
088 }
089
090 /**
091 * Add an exchange to this request. All exchanges that are added to the request have to be finished
092 * @param exchange
093 */
094 public void addExchange(MessageExchange exchange) {
095 exchanges.add(exchange);
096 exchange.setProperty(BeanEndpoint.CORRELATION_ID, correlationId);
097 }
098
099 /**
100 * Get all the MessageExchanges that are involved in this request
101 *
102 * @return an unmodifiable list of {@link MessageExchange}s
103 */
104 public Set<MessageExchange> getExchanges() {
105 return Collections.unmodifiableSet(exchanges);
106 }
107 }