KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jms > client > p2p > P2PConnectionDelegate


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.jms.client.p2p;
8
9 import java.io.Serializable JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Collection JavaDoc;
13 import java.util.Enumeration JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import javax.jms.ExceptionListener JavaDoc;
18 import javax.jms.JMSException JavaDoc;
19
20 import org.jgroups.Address;
21 import org.jgroups.Channel;
22 import org.jgroups.ChannelException;
23 import org.jgroups.ChannelListener;
24 import org.jgroups.JChannelFactory;
25 import org.jgroups.Message;
26 import org.jgroups.MessageListener;
27 import org.jgroups.blocks.PullPushAdapter;
28 import org.jboss.jms.MessageImpl;
29 import org.jboss.jms.client.ConnectionDelegate;
30 import org.jboss.jms.client.SessionDelegate;
31 import org.jboss.jms.destination.JBossTemporaryDestination;
32 import org.jboss.util.id.GUID;
33
34 /**
35  * The p2p connection
36  *
37  * @author <a HREF="mailto:nathan@jboss.org">Nathan Phelps</a>
38  * @author <a HREF="mailto:adrian@jboss.org>Adrian Brock</a>
39  * @version $Revision: 1.4 $
40  */

41 public class P2PConnectionDelegate
42    implements ConnectionDelegate, ChannelListener, MessageListener
43 {
44    // Constants -----------------------------------------------------
45

46    // Attributes ----------------------------------------------------
47

48    private String JavaDoc clientId = null;
49    private ExceptionListener JavaDoc exceptionListener = null;
50    private boolean closed = false;
51    private String JavaDoc password = null;
52    private String JavaDoc username = null;
53    private List JavaDoc sessions = new ArrayList JavaDoc();
54
55    private Channel channel = null;
56    private PullPushAdapter connection = null;
57    private boolean started = false;
58
59    // Static --------------------------------------------------------
60

61    // Constructors --------------------------------------------------
62

63    public P2PConnectionDelegate(String JavaDoc username, String JavaDoc password)
64       throws JMSException JavaDoc
65    {
66       this.username = username;
67       this.password = password;
68
69       try
70       {
71           URL JavaDoc url = Thread.currentThread().getContextClassLoader().getResource("org/jboss/jms/p2p/jgroups-config.xml");
72           this.channel = new JChannelFactory().createChannel(url);
73           this.channel.setChannelListener(this);
74           this.channel.connect("org.jboss.jms.p2p");
75           this.connection = new PullPushAdapter(this.channel, this);
76           this.connection.start();
77       }
78       catch (ChannelException exception)
79       {
80           throw new JMSException JavaDoc(exception.getMessage());
81       }
82
83    }
84
85    // Public --------------------------------------------------------
86

87    // ConnectionDelegate implementation -----------------------------
88

89     public void close() throws JMSException JavaDoc
90     {
91       Iterator JavaDoc iterator = this.sessions.iterator();
92       while (iterator.hasNext())
93       {
94           ((SessionDelegate) iterator.next()).close();
95           iterator.remove();
96       }
97       this.closed = true;
98       this.connection.stop();
99       this.channel.disconnect();
100       this.channel.close();
101     }
102
103     public void closing() throws JMSException JavaDoc
104     {
105     }
106
107     public SessionDelegate createSession(boolean isXA, boolean transacted, int acknowledgeMode) throws JMSException JavaDoc
108     {
109       this.throwExceptionIfClosed();
110       this.generateClientIDIfNull();
111       SessionDelegate session = new P2PSessionDelegate(this, transacted, acknowledgeMode);
112       this.sessions.add(session);
113       return session;
114     }
115
116     public String JavaDoc getClientID() throws JMSException JavaDoc
117     {
118       this.throwExceptionIfClosed();
119       this.generateClientIDIfNull();
120       return this.clientId;
121     }
122
123     public Enumeration JavaDoc getJMSXPropertyNames() throws JMSException JavaDoc
124     {
125         // TODO getJMSXPropertyNames
126
return null;
127     }
128    
129    public void deleteTempDestination(JBossTemporaryDestination destination)
130    {
131       // TODO deleteTempDestination
132
}
133
134     public void setClientID(String JavaDoc id) throws JMSException JavaDoc
135     {
136       this.throwExceptionIfClosed();
137       if (this.clientId != null)
138       {
139           throw new IllegalStateException JavaDoc("The client Id has already been set by the provider. To supply your own value, you must set the client ID immediatly after creating the connection. See section 4.3.2 of the JMS specification for more information.");
140       }
141       this.clientId = id;
142     }
143
144     public void setExceptionListener(ExceptionListener JavaDoc listener) throws JMSException JavaDoc
145     {
146       this.throwExceptionIfClosed();
147       this.generateClientIDIfNull();
148       this.exceptionListener = listener;
149     }
150
151     public void start() throws JMSException JavaDoc
152     {
153       this.throwExceptionIfClosed();
154       this.generateClientIDIfNull();
155       this.started = true;
156     }
157
158     public void stop() throws JMSException JavaDoc
159     {
160       this.throwExceptionIfClosed();
161       this.generateClientIDIfNull();
162       this.started = false;
163     }
164
165    // ChannelListener implementation --------------------------------
166

167    public void channelClosed(Channel arg0)
168    {
169       if (this.closed != false && this.exceptionListener != null)
170       {
171           this.exceptionListener.onException(new JMSException JavaDoc("We were unexpectedly disconnected"));
172       }
173    }
174
175    public void channelConnected(Channel arg0)
176    {
177    }
178
179    public void channelDisconnected(Channel arg0)
180    {
181       this.channelClosed(channel);
182    }
183
184    public void channelReconnected(Address arg0)
185    {
186    }
187
188    public void channelShunned()
189    {
190       if (this.exceptionListener != null)
191       {
192           this.exceptionListener.onException(new JMSException JavaDoc("We were shunned."));
193       }
194    }
195
196    // MessageListener implementation --------------------------------
197

198    public byte[] getState()
199    {
200       return new byte[0];
201    }
202
203    public void receive(Message message)
204    {
205       if (this.started)
206       {
207           Object JavaDoc object = message.getObject();
208           if (object instanceof List JavaDoc)
209           {
210               List JavaDoc theList = (List JavaDoc) object;
211               Iterator JavaDoc iterator = theList.iterator();
212               while (iterator.hasNext())
213               {
214                   Object JavaDoc listObject = iterator.next();
215                   if (listObject instanceof MessageImpl)
216                   {
217                       MessageImpl currentMessage = (MessageImpl)listObject;
218                       if (currentMessage.getOrigianClientID().equals(this.clientId))
219                       {
220                           currentMessage.setIsLocal(true);
221                       }
222                       Iterator JavaDoc sessionIterator = this.sessions.iterator();
223                       while (sessionIterator.hasNext())
224                       {
225                           ((P2PSessionDelegate) sessionIterator.next()).deliver(currentMessage);
226                       }
227                   }
228               }
229           }
230           else if (object instanceof MessageImpl)
231           {
232               MessageImpl theMessage = (MessageImpl) object;
233               if (theMessage.getOrigianClientID().equals(this.clientId))
234               {
235                   theMessage.setIsLocal(true);
236               }
237               Iterator JavaDoc iterator = this.sessions.iterator();
238               while (iterator.hasNext())
239               {
240                   ((P2PSessionDelegate) iterator.next()).deliver(theMessage);
241               }
242           }
243       }
244    }
245
246    public void setState(byte[] arg0)
247    {
248    }
249
250    // Object overrides -----------------------------------------------
251

252    public void finalize() throws Throwable JavaDoc
253    {
254        if (!this.closed)
255        {
256            this.close();
257        }
258    }
259
260    // Protected ------------------------------------------------------
261

262    // Package Private ------------------------------------------------
263

264    ///////////////////////////////////////////////////////////////////////////////////////////////
265
// Methods that the session calls //
266
///////////////////////////////////////////////////////////////////////////////////////////////
267

268    void send(MessageImpl message) throws JMSException JavaDoc
269    {
270        try
271        {
272            message.setOriginClientID(this.clientId);
273            this.connection.send(new Message(null, null, (Serializable JavaDoc) message));
274        }
275        catch (Exception JavaDoc exception)
276        {
277            throw new JMSException JavaDoc(exception.getMessage());
278        }
279    }
280
281    void send(Collection JavaDoc messages) throws JMSException JavaDoc
282    {
283        try
284        {
285            Iterator JavaDoc iterator = messages.iterator();
286            while (iterator.hasNext())
287            {
288                ((MessageImpl)iterator.next()).setOriginClientID(this.clientId);
289            }
290            this.connection.send(new Message(null, null, (Serializable JavaDoc) messages));
291        }
292        catch (Exception JavaDoc exception)
293        {
294            throw new JMSException JavaDoc(exception.getMessage());
295        }
296    }
297
298    // Private --------------------------------------------------------
299

300    private void throwExceptionIfClosed()
301    {
302        if (this.closed)
303        {
304            throw new IllegalStateException JavaDoc("The connection is closed.");
305        }
306    }
307
308    private synchronized void generateClientIDIfNull() throws JMSException JavaDoc
309    {
310        if (this.clientId == null)
311        {
312            this.setClientID(new GUID().toString().toUpperCase());
313        }
314    }
315
316    // Inner Classes --------------------------------------------------
317

318 }
319
Popular Tags