KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > server > OutgoingServerSocketReader


1 /**
2  * $RCSfile: OutgoingServerSocketReader.java,v $
3  * $Revision: 1.4 $
4  * $Date: 2005/06/17 21:38:27 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.server;
13
14 import org.dom4j.Element;
15 import org.dom4j.io.XPPPacketReader;
16 import org.jivesoftware.util.Log;
17
18 import java.io.IOException JavaDoc;
19 import java.util.concurrent.BlockingQueue JavaDoc;
20 import java.util.concurrent.LinkedBlockingQueue JavaDoc;
21 import java.util.concurrent.TimeUnit JavaDoc;
22
23 /**
24  * An OutgoingServerSocketReader is responsible for reading and queueing the DOM Element sent by
25  * a remote server. Since the DOM Elements are received using the outgoing connection only special
26  * stanzas may be sent by the remote server (eg. db:result stanzas for answering if the
27  * Authoritative Server verified the key sent by this server).<p>
28  *
29  * This class is also responsible for closing the outgoing connection if the remote server sent
30  * an end of the stream element.
31  *
32  * @author Gaston Dombiak
33  */

34 public class OutgoingServerSocketReader {
35
36     private OutgoingServerSession session;
37     private boolean open = true;
38     private XPPPacketReader reader = null;
39     /**
40      * Queue that holds the elements read by the XPPPacketReader.
41      */

42     private BlockingQueue JavaDoc<Element> elements = new LinkedBlockingQueue JavaDoc<Element>();
43
44     public OutgoingServerSocketReader(XPPPacketReader reader) {
45         this.reader = reader;
46         init();
47     }
48
49     /**
50      * Returns the OutgoingServerSession for which this reader is working for or <tt>null</tt> if
51      * a OutgoingServerSession was not created yet. While the OutgoingServerSession is being
52      * created it is possible to have a reader with no session.
53      *
54      * @return the OutgoingServerSession for which this reader is working for or <tt>null</tt> if
55      * a OutgoingServerSession was not created yet.
56      */

57     public OutgoingServerSession getSession() {
58         return session;
59     }
60
61     /**
62      * Sets the OutgoingServerSession for which this reader is working for.
63      *
64      * @param session the OutgoingServerSession for which this reader is working for
65      */

66     public void setSession(OutgoingServerSession session) {
67         this.session = session;
68     }
69
70     /**
71      * Retrieves and removes the first received element that was stored in the queue, waiting
72      * if necessary up to the specified wait time if no elements are present on this queue.
73      *
74      * @param timeout how long to wait before giving up, in units of <tt>unit</tt>.
75      * @param unit a <tt>TimeUnit</tt> determining how to interpret the <tt>timeout</tt> parameter.
76      * @return the head of this queue, or <tt>null</tt> if the specified waiting time elapses
77      * before an element is present.
78      * @throws InterruptedException if interrupted while waiting.
79      */

80     public Element getElement(long timeout, TimeUnit JavaDoc unit) throws InterruptedException JavaDoc {
81         return elements.poll(timeout, unit);
82     }
83
84     private void init() {
85         // Create a thread that will read and store DOM Elements.
86
Thread JavaDoc thread = new Thread JavaDoc("Outgoing Server Reader") {
87             public void run() {
88                 while (open) {
89                     Element doc = null;
90                     try {
91                         doc = reader.parseDocument().getRootElement();
92
93                         if (doc == null) {
94                             // Stop reading the stream since the remote server has sent an end of
95
// stream element and probably closed the connection.
96
closeSession();
97                         }
98                         else {
99                             elements.add(doc);
100                         }
101                     }
102                     catch (IOException JavaDoc e) {
103                         String JavaDoc message = "Finishing Outgoing Server Reader. ";
104                         if (session != null) {
105                             message = message + "Closing session: " + session.toString();
106                         }
107                         else {
108                             message = message + "No session to close.";
109                         }
110                         Log.debug(message, e);
111                         closeSession();
112                     }
113                     catch (Exception JavaDoc e) {
114                         String JavaDoc message = "Finishing Outgoing Server Reader. ";
115                         if (session != null) {
116                             message = message + "Closing session: " + session.toString();
117                         }
118                         else {
119                             message = message + "No session to close.";
120                         }
121                         Log.error(message, e);
122                         closeSession();
123                     }
124                 }
125             }
126         };
127         thread.setDaemon(true);
128         thread.start();
129     }
130
131     private void closeSession() {
132         open = false;
133         if (session != null) {
134             session.getConnection().close();
135         }
136     }
137 }
138
Popular Tags