KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > nmr > flow > DefaultFlowChooser


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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) throws MessagingException {
31         // Check if flow was specified
32
String JavaDoc flow = (String JavaDoc) exchange.getProperty(JbiConstants.FLOW_PROPERTY_NAME);
33         if (flow != null) {
34             Flow foundFlow = null;
35             for (int i = 0; i < flows.length; i++) {
36                 if (flows[i].getName().equalsIgnoreCase(flow)) {
37                     foundFlow = flows[i];
38                     break;
39                 }
40             }
41             if (foundFlow == null) {
42                 throw new MessagingException("Flow '" + flow + "' was specified but not found");
43             } if (foundFlow.canHandle(exchange)) {
44                 return foundFlow;
45             } else {
46                 throw new MessagingException("Flow '" + flow + "' was specified but not able to handle exchange");
47             }
48         }
49         // Check against flow capabilities
50
for (int i = 0; i < flows.length; i++) {
51             if (flows[i].canHandle(exchange)) {
52                 ((MessageExchangeImpl) exchange).getPacket().setProperty(JbiConstants.FLOW_PROPERTY_NAME, flows[i].getName());
53                 return flows[i];
54             }
55         }
56         return null;
57     }
58     
59 }
60
Popular Tags