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.eip.patterns;
018    
019    import javax.jbi.management.DeploymentException;
020    import javax.jbi.messaging.MessageExchange;
021    import javax.jbi.messaging.MessagingException;
022    
023    import org.apache.servicemix.eip.support.AbstractContentBasedRouter;
024    import org.apache.servicemix.eip.support.ExchangeTarget;
025    import org.apache.servicemix.eip.support.RoutingRule;
026    
027    /**
028     * ContentBasedRouter can be used for all kind of content-based routing.
029     * This component implements the  
030     * <a href="http://www.enterpriseintegrationpatterns.com/ContentBasedRouter.html">Content-Based Router</a> 
031     * pattern.
032     * 
033     * @author gnodet
034     * @version $Revision: 376451 $
035     * @org.apache.xbean.XBean element="content-based-router"
036     *                  description="A Content-Based Router"
037     */
038    public class ContentBasedRouter extends AbstractContentBasedRouter {
039    
040        /**
041         * Routing rules that are evaluated to find the target destination
042         */
043        private RoutingRule[] rules;
044        
045        /**
046         * @return Returns the rules.
047         */
048        public RoutingRule[] getRules() {
049            return rules;
050        }
051    
052        /**
053         * @param rules The rules to set.
054         */
055        public void setRules(RoutingRule[] rules) {
056            this.rules = rules;
057        }
058    
059        /* (non-Javadoc)
060         * @see org.apache.servicemix.eip.EIPEndpoint#validate()
061         */
062        public void validate() throws DeploymentException {
063            super.validate();
064            // Check rules
065            if (rules == null || rules.length == 0) {
066                throw new IllegalArgumentException("rules should contain at least one RoutingRule");
067            }
068        }
069    
070        /**
071         * Find the target destination for the given JBI exchange
072         * @param exchange
073         * @return the target for the given exchange
074         * @throws Exception
075         */
076        protected ExchangeTarget getDestination(MessageExchange exchange) throws Exception {
077            for (int i = 0; i < rules.length; i++) {
078                if (rules[i].getPredicate() == null
079                    || rules[i].getPredicate().matches(exchange)) {
080                    return rules[i].getTarget();
081                }
082            }
083            throw new MessagingException("No matching rule found for exchange");
084        }
085    
086    }