01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.servicemix.jbi.nmr.flow;
18:
19: import javax.jbi.messaging.MessageExchange;
20: import javax.jbi.messaging.MessagingException;
21:
22: import org.apache.servicemix.JbiConstants;
23: import org.apache.servicemix.jbi.messaging.MessageExchangeImpl;
24:
25: public class DefaultFlowChooser implements FlowChooser {
26:
27: public DefaultFlowChooser() {
28: }
29:
30: public Flow chooseFlow(Flow[] flows, MessageExchange exchange)
31: throws MessagingException {
32: // Check if flow was specified
33: String flow = (String) exchange
34: .getProperty(JbiConstants.FLOW_PROPERTY_NAME);
35: if (flow != null) {
36: Flow foundFlow = null;
37: for (int i = 0; i < flows.length; i++) {
38: if (flows[i].getName().equalsIgnoreCase(flow)) {
39: foundFlow = flows[i];
40: break;
41: }
42: }
43: if (foundFlow == null) {
44: throw new MessagingException("Flow '" + flow
45: + "' was specified but not found");
46: }
47: if (foundFlow.canHandle(exchange)) {
48: return foundFlow;
49: } else {
50: throw new MessagingException(
51: "Flow '"
52: + flow
53: + "' was specified but not able to handle exchange");
54: }
55: }
56: // Check against flow capabilities
57: for (int i = 0; i < flows.length; i++) {
58: if (flows[i].canHandle(exchange)) {
59: ((MessageExchangeImpl) exchange).getPacket()
60: .setProperty(JbiConstants.FLOW_PROPERTY_NAME,
61: flows[i].getName());
62: return flows[i];
63: }
64: }
65: return null;
66: }
67:
68: }
|