KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > router > RouterRoundRobinImpl


1 /**
2  * Dream
3  * Copyright (C) 2003 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: vivien.quema@inrialpes.fr
20  *
21  * Initial developer(s): Vivien Quema
22  * Contributor(s):
23  */

24
25 package org.objectweb.dream.router;
26
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.objectweb.dream.Push;
32 import org.objectweb.dream.message.Message;
33
34 /**
35  * This router implements a round-robin choice of its outputs. Note that the
36  * <code>defaultOutPushItf</code> is only chosen if no route is bound to the
37  * router.
38  */

39 public class RouterRoundRobinImpl extends AbstractRouterImpl
40 {
41
42   private int nextOutPushIndex = -1;
43   Push[] itfs;
44
45   /**
46    * @see org.objectweb.dream.router.AbstractRouterImpl#getOutput(org.objectweb.dream.message.Message,
47    * java.util.Map)
48    */

49   protected Push getOutput(Message message, Map JavaDoc context)
50   {
51     if (!initialized)
52     {
53       Collection JavaDoc values = outPushMap.values();
54       Iterator JavaDoc iter = values.iterator();
55       itfs = new Push[values.size()];
56       int i = 0;
57       while (iter.hasNext())
58       {
59         itfs[i] = (Push) iter.next();
60         i++;
61       }
62       initialized = true;
63     }
64     if (itfs.length > 0)
65     {
66       nextOutPushIndex = (nextOutPushIndex + 1) % itfs.length;
67       return itfs[nextOutPushIndex];
68     }
69     return null;
70   }
71
72 }
Popular Tags