KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 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 : dream@objectweb.org
20  *
21  * Initial developer(s): Matthieu Leclercq
22  * Contributor(s): Vivien Quema
23  */

24
25 package org.objectweb.dream.router;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.objectweb.dream.AbstractComponent;
31 import org.objectweb.dream.Push;
32 import org.objectweb.dream.PushException;
33 import org.objectweb.dream.message.Message;
34 import org.objectweb.dream.message.manager.MessageManager;
35 import org.objectweb.fractal.api.NoSuchInterfaceException;
36 import org.objectweb.fractal.api.control.IllegalBindingException;
37 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
38 import org.objectweb.util.monolog.api.BasicLevel;
39
40 /**
41  * Abstract implementation of a basic Push/Push router. A Push/Push router
42  * routes messages received on its Push input to one of its Push outputs. A
43  * Push/Push router has also a <code>defaultOutPushItf</code> that can be
44  * bound to route messages for which no routes are found. <br>
45  * Outputs are stored in the <code>outPushMap</code> Map. The output
46  * corresponding to an incoming message is retrieved using the abstract
47  * {@link #getOutput(Message, Map) }method. To avoid unnecessary building of
48  * routes the {@link AbstractRouterImpl#getOutput(Message, Map) }can test the
49  * boolean <code>initialized</code> which is set to <code>false</code> each
50  * time the router is bound to a new output.
51  */

52 public abstract class AbstractRouterImpl extends AbstractComponent
53     implements
54       Push
55 {
56
57   /** Map of out-push interfaces binded to this router */
58   protected Map JavaDoc outPushMap;
59
60   /** The name of the default {@link Push}client interface. */
61   public static final String JavaDoc DEFAULT_OUT_PUSH_ITF_NAME = "defaultOutPush";
62
63   /**
64    * Default Push interface to be used if no Push interface can be found in
65    * <code>outPushTable</code>.
66    */

67   protected Push defaultOutPushItf = null;
68
69   /** the message manager client interface of this component */
70   protected MessageManager messageManagerItf;
71
72   /** <code>true</code> if the route table is initialized. */
73   protected boolean initialized;
74
75   /** Default contructor */
76   public AbstractRouterImpl()
77   {
78     outPushMap = new HashMap JavaDoc();
79     initialized = false;
80   }
81
82   protected abstract Push getOutput(Message message, Map JavaDoc context)
83       throws PushException;
84
85   /**
86    * Sends the incoming message to one of the router's output. The appropriate
87    * output is retrieved using the {@link #getOutput(Message, Map) }method. If
88    * this method returns <code>null</code>, the
89    * <code>defaultOutPushItf</code> is used if it is bound.
90    *
91    * @param message the pushed message
92    * @param context a context propagated to the selected route.
93    * @throws PushException if no route can be found, or thown by client push.
94    * @see Push#push(Message, Map)
95    */

96   public void push(Message message, Map JavaDoc context) throws PushException
97   {
98     Push outPush = getOutput(message, context);
99     if (outPush == null)
100     {
101       if (defaultOutPushItf == null)
102       {
103         logger.log(BasicLevel.ERROR, "No route for message " + message
104             + ". The message will be dropped");
105         messageManagerItf.deleteMessage(message);
106         return;
107       }
108       defaultOutPushItf.push(message, context);
109     }
110     else
111     {
112       outPush.push(message, context);
113     }
114   }
115
116   // ---------------------------------------------------------------------------
117
// Implementation of BindingController interface
118
// ---------------------------------------------------------------------------
119

120   /**
121    * @see org.objectweb.fractal.api.control.BindingController#listFc()
122    */

123   public String JavaDoc[] listFc()
124   {
125     String JavaDoc[] tab = new String JavaDoc[outPushMap.size() + 1];
126     outPushMap.keySet().toArray(tab);
127     tab[outPushMap.size()] = MessageManager.ITF_NAME;
128     return tab;
129   }
130
131   /**
132    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
133    * Object)
134    */

135   public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
136       throws NoSuchInterfaceException, IllegalBindingException,
137       IllegalLifeCycleException
138   {
139     super.bindFc(clientItfName, serverItf);
140     if (clientItfName.startsWith(OUT_PUSH_ITF_NAME))
141     {
142       outPushMap.put(clientItfName, serverItf);
143       initialized = false;
144     }
145     else if (clientItfName.equals(DEFAULT_OUT_PUSH_ITF_NAME))
146     {
147       defaultOutPushItf = (Push) serverItf;
148       initialized = false;
149     }
150     else if (clientItfName.equals(MessageManager.ITF_NAME))
151     {
152       messageManagerItf = (MessageManager) serverItf;
153     }
154   }
155
156   /**
157    * @see org.objectweb.fractal.api.control.BindingController#unbindFc(String)
158    */

159   public synchronized void unbindFc(String JavaDoc clientItfName)
160       throws NoSuchInterfaceException, IllegalBindingException,
161       IllegalLifeCycleException
162   {
163     super.unbindFc(clientItfName);
164     if (clientItfName.startsWith(OUT_PUSH_ITF_NAME))
165     {
166       outPushMap.remove(clientItfName);
167       initialized = false;
168     }
169     else if (clientItfName.equals(DEFAULT_OUT_PUSH_ITF_NAME))
170     {
171       defaultOutPushItf = null;
172       initialized = false;
173     }
174   }
175 }
Popular Tags