KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > eip > patterns > ContentBasedRouter


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.eip.patterns;
18
19 import javax.jbi.management.DeploymentException;
20 import javax.jbi.messaging.MessageExchange;
21 import javax.jbi.messaging.MessagingException;
22
23 import org.apache.servicemix.eip.support.AbstractContentBasedRouter;
24 import org.apache.servicemix.eip.support.ExchangeTarget;
25 import org.apache.servicemix.eip.support.RoutingRule;
26
27 /**
28  * ContentBasedRouter can be used for all kind of content-based routing.
29  * This component implements the
30  * <a HREF="http://www.enterpriseintegrationpatterns.com/ContentBasedRouter.html">Content-Based Router</a>
31  * pattern.
32  *
33  * @author gnodet
34  * @version $Revision: 376451 $
35  * @org.apache.xbean.XBean element="content-based-router"
36  * description="A Content-Based Router"
37  */

38 public class ContentBasedRouter extends AbstractContentBasedRouter {
39
40     /**
41      * Routing rules that are evaluated to find the target destination
42      */

43     private RoutingRule[] rules;
44     
45     /**
46      * @return Returns the rules.
47      */

48     public RoutingRule[] getRules() {
49         return rules;
50     }
51
52     /**
53      * @param rules The rules to set.
54      */

55     public void setRules(RoutingRule[] rules) {
56         this.rules = rules;
57     }
58
59     /* (non-Javadoc)
60      * @see org.apache.servicemix.eip.EIPEndpoint#validate()
61      */

62     public void validate() throws DeploymentException {
63         super.validate();
64         // Check rules
65
if (rules == null || rules.length == 0) {
66             throw new IllegalArgumentException JavaDoc("rules should contain at least one RoutingRule");
67         }
68     }
69
70     /**
71      * Find the target destination for the given JBI exchange
72      * @param exchange
73      * @return the target for the given exchange
74      * @throws Exception
75      */

76     protected ExchangeTarget getDestination(MessageExchange exchange) throws Exception JavaDoc {
77         for (int i = 0; i < rules.length; i++) {
78             if (rules[i].getPredicate() == null ||
79                 rules[i].getPredicate().matches(exchange)) {
80                 return rules[i].getTarget();
81             }
82         }
83         throw new MessagingException("No matching rule found for exchange");
84     }
85
86 }
87
Popular Tags