Commit 1491a7ff authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Small optimization to reduce memory consumption.

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@9616 b35dd754-fafc-0310-a699-88a17e54d16e
parent 17593954
...@@ -14,18 +14,26 @@ import org.dom4j.DocumentHelper; ...@@ -14,18 +14,26 @@ import org.dom4j.DocumentHelper;
import org.dom4j.Element; import org.dom4j.Element;
import org.dom4j.Namespace; import org.dom4j.Namespace;
import org.dom4j.QName; import org.dom4j.QName;
import org.dom4j.io.XMPPPacketReader;
import org.jivesoftware.openfire.Connection; import org.jivesoftware.openfire.Connection;
import org.jivesoftware.openfire.PacketDeliverer; import org.jivesoftware.openfire.PacketDeliverer;
import org.jivesoftware.openfire.SessionPacketRouter; import org.jivesoftware.openfire.SessionPacketRouter;
import org.jivesoftware.openfire.StreamID; import org.jivesoftware.openfire.StreamID;
import org.jivesoftware.openfire.auth.UnauthorizedException; import org.jivesoftware.openfire.auth.UnauthorizedException;
import org.jivesoftware.openfire.multiplex.UnknownStanzaException; import org.jivesoftware.openfire.multiplex.UnknownStanzaException;
import org.jivesoftware.openfire.net.MXParser;
import org.jivesoftware.openfire.net.SASLAuthentication; import org.jivesoftware.openfire.net.SASLAuthentication;
import org.jivesoftware.openfire.net.VirtualConnection; import org.jivesoftware.openfire.net.VirtualConnection;
import org.jivesoftware.openfire.session.LocalClientSession; import org.jivesoftware.openfire.session.LocalClientSession;
import org.jivesoftware.util.Log; import org.jivesoftware.util.Log;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmpp.packet.IQ;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet; import org.xmpp.packet.Packet;
import org.xmpp.packet.Presence;
import java.io.StringReader;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
...@@ -41,6 +49,27 @@ import java.util.concurrent.CopyOnWriteArraySet; ...@@ -41,6 +49,27 @@ import java.util.concurrent.CopyOnWriteArraySet;
* @author Alexander Wenckus * @author Alexander Wenckus
*/ */
public class HttpSession extends LocalClientSession { public class HttpSession extends LocalClientSession {
private static XmlPullParserFactory factory = null;
private static ThreadLocal<XMPPPacketReader> localParser = null;
static {
try {
factory = XmlPullParserFactory.newInstance(MXParser.class.getName(), null);
factory.setNamespaceAware(true);
}
catch (XmlPullParserException e) {
Log.error("Error creating a parser factory", e);
}
// Create xmpp parser to keep in each thread
localParser = new ThreadLocal<XMPPPacketReader>() {
protected XMPPPacketReader initialValue() {
XMPPPacketReader parser = new XMPPPacketReader();
factory.setNamespaceAware(true);
parser.setXPPFactory(factory);
return parser;
}
};
}
private int wait; private int wait;
private int hold = 0; private int hold = 0;
private String language; private String language;
...@@ -513,17 +542,11 @@ public class HttpSession extends LocalClientSession { ...@@ -513,17 +542,11 @@ public class HttpSession extends LocalClientSession {
connection.setSession(this); connection.setSession(this);
// We aren't supposed to hold connections open or we already have some packets waiting // We aren't supposed to hold connections open or we already have some packets waiting
// to be sent to the client. // to be sent to the client.
if (hold <= 0 || (pendingElements.size() > 0 && connection.getRequestId() if (hold <= 0 || (pendingElements.size() > 0 && connection.getRequestId() == lastRequestID + 1)) {
== lastRequestID + 1)) {
try {
deliver(connection, pendingElements); deliver(connection, pendingElements);
lastRequestID = connection.getRequestId(); lastRequestID = connection.getRequestId();
pendingElements.clear(); pendingElements.clear();
} }
catch (HttpConnectionClosedException he) {
throw he;
}
}
else { else {
// With this connection we need to check if we will have too many connections open, // With this connection we need to check if we will have too many connections open,
// closing any extras. // closing any extras.
...@@ -651,7 +674,7 @@ public class HttpSession extends LocalClientSession { ...@@ -651,7 +674,7 @@ public class HttpSession extends LocalClientSession {
private void failDelivery() { private void failDelivery() {
for (Deliverable deliverable : pendingElements) { for (Deliverable deliverable : pendingElements) {
Collection<Packet> packet = deliverable.packets; Collection<Packet> packet = deliverable.getPackets();
if (packet != null) { if (packet != null) {
failDelivery(packet); failDelivery(packet);
} }
...@@ -735,7 +758,7 @@ public class HttpSession extends LocalClientSession { ...@@ -735,7 +758,7 @@ public class HttpSession extends LocalClientSession {
private class Deliverable implements Comparable<Deliverable> { private class Deliverable implements Comparable<Deliverable> {
private final String text; private final String text;
private final Collection<Packet> packets; private final Collection<String> packets;
private long requestID; private long requestID;
public Deliverable(String text) { public Deliverable(String text) {
...@@ -745,17 +768,17 @@ public class HttpSession extends LocalClientSession { ...@@ -745,17 +768,17 @@ public class HttpSession extends LocalClientSession {
public Deliverable(Collection<Packet> elements) { public Deliverable(Collection<Packet> elements) {
this.text = null; this.text = null;
this.packets = new ArrayList<Packet>(); this.packets = new ArrayList<String>();
for (Packet element : elements) { for (Packet packet : elements) {
this.packets.add(element.createCopy()); this.packets.add(packet.toXML());
} }
} }
public String getDeliverable() { public String getDeliverable() {
if (text == null) { if (text == null) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
for (Packet packet : packets) { for (String packet : packets) {
builder.append(packet.toXML()); builder.append(packet);
} }
return builder.toString(); return builder.toString();
} }
...@@ -772,6 +795,33 @@ public class HttpSession extends LocalClientSession { ...@@ -772,6 +795,33 @@ public class HttpSession extends LocalClientSession {
return requestID; return requestID;
} }
public Collection<Packet> getPackets() {
List<Packet> answer = new ArrayList<Packet>();
for (String packetXML : packets) {
try {
Packet packet = null;
// Parse the XML stanza
Element element = localParser.get().read(new StringReader(packetXML)).getRootElement();
String tag = element.getName();
if ("message".equals(tag)) {
packet = new Message(element, true);
}
else if ("presence".equals(tag)) {
packet = new Presence(element, true);
}
else if ("iq".equals(tag)) {
packet = new IQ(element, true);
}
// Add the reconstructed packet to the result
answer.add(packet);
}
catch (Exception e) {
Log.error("Error while parsing Privacy Property", e);
}
}
return answer;
}
public int compareTo(Deliverable o) { public int compareTo(Deliverable o) {
return (int) (o.getRequestID() - requestID); return (int) (o.getRequestID() - requestID);
} }
...@@ -797,7 +847,7 @@ public class HttpSession extends LocalClientSession { ...@@ -797,7 +847,7 @@ public class HttpSession extends LocalClientSession {
List<Packet> packets = new ArrayList<Packet>(); List<Packet> packets = new ArrayList<Packet>();
for (Deliverable deliverable : deliverables) { for (Deliverable deliverable : deliverables) {
if (deliverable.packets != null) { if (deliverable.packets != null) {
packets.addAll(deliverable.packets); packets.addAll(deliverable.getPackets());
} }
} }
return packets; return packets;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment