KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > channel > GenericPushChannelOutImpl


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):
23  */

24
25 package org.objectweb.dream.channel;
26
27 import java.io.IOException JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.objectweb.dream.AbstractComponent;
31 import org.objectweb.dream.IOPushException;
32 import org.objectweb.dream.Push;
33 import org.objectweb.dream.PushException;
34 import org.objectweb.dream.message.Message;
35 import org.objectweb.dream.message.codec.MessageCodec;
36 import org.objectweb.dream.message.manager.MessageManager;
37 import org.objectweb.fractal.api.NoSuchInterfaceException;
38 import org.objectweb.fractal.api.control.IllegalBindingException;
39 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
40 import org.objectweb.util.monolog.api.BasicLevel;
41
42 /**
43  * Generic Push ChannelOut. The {@link #push(Message, Map) push }method
44  * retreive a socket using the bound {@link SocketManager }interface. it send
45  * the message using the bound {@link MessageCodec }interface. Then if the
46  * <code>"wait-ack-opt"</code> client interface is bound, it use it to wait an
47  * acknowledgment from the ChannelIn. Finaly the socket is released and the
48  * message is deleted.
49  */

50 public class GenericPushChannelOutImpl extends AbstractComponent
51     implements
52       Push
53 {
54
55   /**
56    * The name of the optional client interface used by this coponent to wait for
57    * an acknowlegdment
58    */

59   public static final String JavaDoc WAIT_ACK_OPT_ITF_NAME = "wait-ack-opt";
60
61   // ---------------------------------------------------------------------------
62
// Client interfaces
63
// ---------------------------------------------------------------------------
64

65   protected MessageManager messageManagerItf;
66   protected MessageCodec messageCodecItf;
67   protected SocketManager socketManagerItf;
68   protected WaitByte waitAckOptItf;
69
70   // ---------------------------------------------------------------------------
71
// Implementation of the Push interface
72
// ---------------------------------------------------------------------------
73

74   /**
75    * @see org.objectweb.dream.Push#push(Message, Map)
76    */

77   public void push(Message message, Map JavaDoc context) throws PushException
78   {
79     if (logger.isLoggable(BasicLevel.DEBUG))
80     {
81       logger.log(BasicLevel.DEBUG, "try to send message -> " + message);
82     }
83     SocketState socketState = null;
84     try
85     {
86       socketState = socketManagerItf.getSocket(message);
87     }
88     catch (InterruptedException JavaDoc e1)
89     {
90       throw new PushException(e1);
91     }
92     catch (IOException JavaDoc e)
93     {
94       throw new IOPushException(e);
95     }
96     try
97     {
98       logger.log(BasicLevel.DEBUG, "write message");
99       messageCodecItf.encode(socketState, message);
100
101       if (waitAckOptItf != null)
102       {
103         int b = waitAckOptItf.waitByte(socketState.getInput());
104         if (b != 0)
105         {
106           socketManagerItf.releaseSocket(socketState, false);
107           throw new PushException("Receive nack for message.");
108         }
109       }
110     }
111     catch (IOException JavaDoc e)
112     {
113       socketManagerItf.releaseSocket(socketState, true);
114       throw new IOPushException(e);
115     }
116
117     socketManagerItf.releaseSocket(socketState, false);
118     messageManagerItf.deleteMessage(message);
119   }
120
121   // ---------------------------------------------------------------------------
122
// Implementation of the BindingController interface
123
// ---------------------------------------------------------------------------
124

125   /**
126    * @see org.objectweb.fractal.api.control.BindingController#listFc()
127    */

128   public String JavaDoc[] listFc()
129   {
130     return new String JavaDoc[]{MessageManager.ITF_NAME, MessageCodec.ITF_NAME,
131         SocketManager.ITF_NAME, WAIT_ACK_OPT_ITF_NAME};
132   }
133
134   /**
135    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
136    * Object)
137    */

138   public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
139       throws NoSuchInterfaceException, IllegalBindingException,
140       IllegalLifeCycleException
141   {
142     super.bindFc(clientItfName, serverItf);
143     if (clientItfName.equals(MessageManager.ITF_NAME))
144     {
145       messageManagerItf = (MessageManager) serverItf;
146     }
147     else if (clientItfName.equals(MessageCodec.ITF_NAME))
148     {
149       messageCodecItf = (MessageCodec) serverItf;
150     }
151     else if (clientItfName.equals(SocketManager.ITF_NAME))
152     {
153       socketManagerItf = (SocketManager) serverItf;
154     }
155     else if (clientItfName.equals(WAIT_ACK_OPT_ITF_NAME))
156     {
157       waitAckOptItf = (WaitByte) serverItf;
158     }
159   }
160
161   /**
162    * @see org.objectweb.fractal.api.control.BindingController#unbindFc(String)
163    */

164   public void unbindFc(String JavaDoc clientItfName) throws NoSuchInterfaceException,
165       IllegalBindingException, IllegalLifeCycleException
166   {
167     super.unbindFc(clientItfName);
168     if (clientItfName.equals(WAIT_ACK_OPT_ITF_NAME))
169     {
170       waitAckOptItf = null;
171     }
172   }
173 }
Popular Tags