KickJava   Java API By Example, From Geeks To Geeks.

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


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: matthieu.leclercq@inrialpes.fr
20  *
21  * Initial developer(s): Matthieu Leclercq
22  * Contributor(s):
23  */

24
25 package org.objectweb.dream.channel;
26
27 import java.net.InetAddress JavaDoc;
28 import java.net.UnknownHostException JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.objectweb.dream.PushException;
32 import org.objectweb.dream.PushPushDreamComponent;
33 import org.objectweb.dream.message.ChunkAlreadyExistsException;
34 import org.objectweb.dream.message.ExtensibleMessage;
35 import org.objectweb.dream.message.Message;
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  * This message transformer adds a {@link IPChannelSourceChunk }containing the
44  * inet address and the listenning port of a channel. <br>
45  * The name of the added chunk is by default
46  * {@link IPChannelSourceChunk#DEFAULT_NAME}, it can be changed using
47  * {@link AddIPChannelAttributeController#setChunkName(String)}.<br>
48  * The inet address can be specified using the
49  * {@link AddIPChannelAttributeController#setInetAddress(String) }method. If
50  * not specified, the local address is used.
51  */

52 public class AddIPChannelSourceChunkImpl extends PushPushDreamComponent
53     implements
54       AddIPChannelAttributeController
55 {
56
57   /** the message manager client interface of this component. */
58   protected MessageManager messageManagerItf;
59
60   /**
61    * the name of the <code>IPChannelDestinationChunk</code> added by this
62    * transformer.
63    */

64   protected String JavaDoc chunkName = IPChannelSourceChunk.DEFAULT_NAME;
65
66   /** the inet address set in the chunk. */
67   protected InetAddress JavaDoc inetAddress;
68
69   /** the port set in the chunk. */
70   protected int port;
71
72   /**
73    * @see org.objectweb.dream.Push#push(Message, Map)
74    */

75   public void push(Message message, Map JavaDoc context) throws PushException
76   {
77     IPChannelSourceChunk chunk = (IPChannelSourceChunk) message
78         .getChunk(chunkName);
79     if (chunk == null)
80     {
81       if (message instanceof ExtensibleMessage)
82       {
83         chunk = (IPChannelSourceChunk) messageManagerItf
84             .createChunk(IPChannelSourceChunk.TYPE);
85         try
86         {
87           ((ExtensibleMessage) message).addChunk(chunkName,
88               IPChannelDestinationChunk.TYPE, chunk);
89         }
90         catch (ChunkAlreadyExistsException e)
91         {
92           // can't happend
93
}
94       }
95       else
96       {
97         logger.log(BasicLevel.ERROR, "Invalid message : not extensible");
98         throw new InternalError JavaDoc("Invalid message : not extensible");
99       }
100     }
101     chunk.setChannelSourceAddr(inetAddress);
102     chunk.setChannelSourcePort(port);
103     outPushItf.push(message, context);
104   }
105
106   // ---------------------------------------------------------------------------
107
// Implementation of AddIPChannelAttributeController interface
108
// ---------------------------------------------------------------------------
109

110   /**
111    * @see AddIPChannelAttributeController#getChunkName()
112    */

113   public String JavaDoc getChunkName()
114   {
115     return chunkName;
116   }
117
118   /**
119    * @see AddIPChannelAttributeController#setChunkName(String)
120    */

121   public void setChunkName(String JavaDoc chunkName)
122   {
123     this.chunkName = chunkName;
124   }
125
126   /**
127    * @see AddIPChannelAttributeController#getInetAddress()
128    */

129   public String JavaDoc getInetAddress()
130   {
131     return inetAddress.getCanonicalHostName();
132   }
133
134   /**
135    * @see AddIPChannelAttributeController#setInetAddress(String)
136    */

137   public void setInetAddress(String JavaDoc inetAddress) throws UnknownHostException JavaDoc
138   {
139     this.inetAddress = InetAddress.getByName(inetAddress);
140   }
141
142   /**
143    * @see AddIPChannelAttributeController#getPort()
144    */

145   public int getPort()
146   {
147     return port;
148   }
149
150   /**
151    * @see AddIPChannelAttributeController#setPort(int)
152    */

153   public void setPort(int port)
154   {
155     this.port = port;
156   }
157
158   // ---------------------------------------------------------------------------
159
// Implementation of BindingController interface
160
// ---------------------------------------------------------------------------
161

162   /**
163    * @see org.objectweb.fractal.api.control.BindingController#listFc()
164    */

165   public String JavaDoc[] listFc()
166   {
167     return new String JavaDoc[]{OUT_PUSH_ITF_NAME, MessageManager.ITF_NAME};
168   }
169
170   /**
171    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
172    * Object)
173    */

174   public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
175       throws NoSuchInterfaceException, IllegalBindingException,
176       IllegalLifeCycleException
177   {
178     super.bindFc(clientItfName, serverItf);
179     if (clientItfName.equals(MessageManager.ITF_NAME))
180     {
181       messageManagerItf = (MessageManager) serverItf;
182     }
183   }
184
185   // ---------------------------------------------------------------------------
186
// Overriding of LifeCycleController methods
187
// ---------------------------------------------------------------------------
188

189   /**
190    * @see org.objectweb.fractal.api.control.LifeCycleController#startFc()
191    */

192   public void startFc() throws IllegalLifeCycleException
193   {
194     super.startFc();
195     if (inetAddress == null)
196     {
197       try
198       {
199         inetAddress = InetAddress.getLocalHost();
200       }
201       catch (UnknownHostException JavaDoc e)
202       {
203         throw new IllegalLifeCycleException(
204             "Unable to get local inet address : " + e.getLocalizedMessage());
205       }
206     }
207   }
208 }
Popular Tags