KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > queue > PushOutgoingHandlerActiveImpl


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): Vivien Quema
22  * Contributor(s): Matthieu Leclercq
23  */

24
25 package org.objectweb.dream.queue;
26
27 import java.util.Collections JavaDoc;
28
29 import org.objectweb.dream.AbstractComponent;
30 import org.objectweb.dream.Push;
31 import org.objectweb.dream.PushException;
32 import org.objectweb.dream.control.activity.Util;
33 import org.objectweb.dream.control.activity.task.AbstractTask;
34 import org.objectweb.dream.control.activity.task.Task;
35 import org.objectweb.dream.message.Message;
36 import org.objectweb.dream.message.manager.MessageManager;
37 import org.objectweb.fractal.api.Component;
38 import org.objectweb.fractal.api.NoSuchInterfaceException;
39 import org.objectweb.fractal.api.control.IllegalBindingException;
40 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
41 import org.objectweb.util.monolog.api.BasicLevel;
42
43 /**
44  * Basic implementation of a Pull outgoing handler.
45  */

46 public class PushOutgoingHandlerActiveImpl extends AbstractComponent
47
48 {
49
50   // Client interfaces
51
protected MessageManager messageManagerItf;
52   protected Push outPushItf;
53   protected Buffer bufferItf;
54   protected BufferRemoveFirstLast bufferRemoveFirstLastItf;
55
56   // The task
57
protected Task queueTask = new PushPushQueueTask();
58
59   // -------------------------------------------------------------------------
60
// Overriden methods
61
// -------------------------------------------------------------------------
62

63   /**
64    * @see org.objectweb.dream.AbstractComponent#beforeFirstStart(org.objectweb.fractal.api.Component)
65    */

66   protected void beforeFirstStart(Component componentItf)
67       throws IllegalLifeCycleException
68   {
69     try
70     {
71       Util.addTask(componentItf, queueTask, Collections.EMPTY_MAP);
72     }
73     catch (Exception JavaDoc e)
74     {
75       throw new IllegalLifeCycleException("Can't add task");
76     }
77   }
78
79   class PushPushQueueTask extends AbstractTask
80   {
81
82     /**
83      * default constructor
84      */

85     public PushPushQueueTask()
86     {
87       super("PushPushQueueTask");
88     }
89
90     /**
91      * @see Task#execute(Object)
92      */

93     public Object JavaDoc execute(Object JavaDoc hints) throws InterruptedException JavaDoc
94     {
95       Message msg = bufferItf.remove();
96       try
97       {
98         outPushItf.push(msg, null);
99       }
100       catch (PushException e)
101       {
102         handlePushException(msg, e);
103       }
104       return EXECUTE_AGAIN;
105     }
106   }
107
108   // ---------------------------------------------------------------------------
109
// Utility methods.
110
// ---------------------------------------------------------------------------
111

112   /**
113    * Handles a {@link PushException}occuring when the activity try to push a
114    * message. This implementation simlpy delete message using message manger.
115    *
116    * @param message the pushed message
117    * @param exception the exceptino thrown during the push call.
118    */

119   protected void handlePushException(Message message, PushException exception)
120   {
121     logger.log(BasicLevel.WARN, "A Push exception has been cached", exception);
122     messageManagerItf.deleteMessage(message);
123   }
124
125   // ---------------------------------------------------------------------------
126
// Implementation of BindingController interface
127
// ---------------------------------------------------------------------------
128

129   /**
130    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
131    * Object)
132    */

133   public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
134       throws NoSuchInterfaceException, IllegalBindingException,
135       IllegalLifeCycleException
136   {
137     super.bindFc(clientItfName, serverItf);
138     if (clientItfName.equals(Push.OUT_PUSH_ITF_NAME))
139     {
140       outPushItf = (Push) serverItf;
141     }
142     else if (clientItfName.equals(Buffer.ITF_NAME))
143     {
144       bufferItf = (Buffer) serverItf;
145     }
146     else if (clientItfName.equals(BufferRemoveFirstLast.ITF_NAME))
147     {
148       bufferRemoveFirstLastItf = (BufferRemoveFirstLast) serverItf;
149     }
150   }
151
152   /**
153    * @see org.objectweb.fractal.api.control.BindingController#listFc()
154    */

155   public String JavaDoc[] listFc()
156   {
157     return new String JavaDoc[]{Push.OUT_PUSH_ITF_NAME, Buffer.ITF_NAME,
158         BufferRemoveFirstLast.ITF_NAME, MessageManager.ITF_NAME};
159   }
160
161 }
Popular Tags