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.jbi.resolver;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import javax.jbi.component.ComponentContext;
023    import javax.jbi.messaging.MessageExchange;
024    import javax.jbi.servicedesc.ServiceEndpoint;
025    import javax.xml.namespace.QName;
026    
027    /**
028     * a simple round robin endpoint resolver policy to have a 
029     * basic load balancing
030     * 
031     * @author lhein
032     */
033    public class ServiceRoundRobinPolicy implements EndpointChooser {
034    
035        private Map<QName, Integer> lastIndexMap = new HashMap<QName, Integer>();
036        private int lastIndex;
037    
038        /*
039         * (non-Javadoc)
040         * @see org.apache.servicemix.jbi.resolver.EndpointChooser#chooseEndpoint
041         * (javax.jbi.servicedesc.ServiceEndpoint[], javax.jbi.component.ComponentContext, 
042         * javax.jbi.messaging.MessageExchange)
043         */
044        public ServiceEndpoint chooseEndpoint(ServiceEndpoint[] endpoints, ComponentContext context,
045                                              MessageExchange exchange) {
046            // if there are no endpoints, then just return null
047            if (endpoints.length == 0) {
048                return null;
049            }
050    
051            if (exchange.getService() == null) {
052                return endpoints[0];
053            }
054            
055            // check for saved index for that service
056            if (lastIndexMap.containsKey(exchange.getService())) {
057                // ok, there is already something
058                lastIndex = lastIndexMap.get(exchange.getService());
059            } else {
060                // fresh value
061                lastIndex = 0;
062            }
063    
064            // check if the index needs a reset
065            if (lastIndex >= endpoints.length || lastIndex < 0) {
066                // reset the index
067                lastIndex = 0;
068            }
069    
070            // determine the next endpoint to use
071            ServiceEndpoint result = endpoints[lastIndex++];
072    
073            // save the index
074            lastIndexMap.put(exchange.getService(), lastIndex);
075    
076            // return the endpoint
077            return result;
078        }
079    }