1 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 ; 19 import java.util.concurrent.BlockingQueue ; 20 import java.util.concurrent.LinkedBlockingQueue ; 21 import java.util.concurrent.TimeUnit ; 22 23 34 public class OutgoingServerSocketReader { 35 36 private OutgoingServerSession session; 37 private boolean open = true; 38 private XPPPacketReader reader = null; 39 42 private BlockingQueue <Element> elements = new LinkedBlockingQueue <Element>(); 43 44 public OutgoingServerSocketReader(XPPPacketReader reader) { 45 this.reader = reader; 46 init(); 47 } 48 49 57 public OutgoingServerSession getSession() { 58 return session; 59 } 60 61 66 public void setSession(OutgoingServerSession session) { 67 this.session = session; 68 } 69 70 80 public Element getElement(long timeout, TimeUnit unit) throws InterruptedException { 81 return elements.poll(timeout, unit); 82 } 83 84 private void init() { 85 Thread thread = new Thread ("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 closeSession(); 97 } 98 else { 99 elements.add(doc); 100 } 101 } 102 catch (IOException e) { 103 String 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 e) { 114 String 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 |