Commit 969e85b4 authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Create a parser/reader per processing thread and not per connection. JM-925

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@6656 b35dd754-fafc-0310-a699-88a17e54d16e
parent 11e32674
......@@ -22,7 +22,6 @@ import org.jivesoftware.wildfire.auth.UnauthorizedException;
import org.jivesoftware.wildfire.session.Session;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmpp.packet.*;
import java.io.IOException;
......@@ -40,11 +39,6 @@ public abstract class StanzaHandler {
*/
protected static String CHARSET = "UTF-8";
private static final String STREAM_START = "<stream:stream";
/**
* Reuse the same factory for all the connections.
*/
private static XmlPullParserFactory factory = null;
private Connection connection;
// DANIELE: Indicate if a session is already created
......@@ -75,18 +69,6 @@ public abstract class StanzaHandler {
*/
private PacketRouter router;
private XMPPPacketReader reader;
static {
try {
factory = XmlPullParserFactory.newInstance(MXParser.class.getName(), null);
factory.setNamespaceAware(true);
}
catch (XmlPullParserException e) {
Log.error("Error creating a parser factory", e);
}
}
/**
* Creates a dedicated reader for a socket.
*
......@@ -98,12 +80,9 @@ public abstract class StanzaHandler {
this.serverName = serverName;
this.router = router;
this.connection = connection;
// Create reader/parser
reader = new XMPPPacketReader();
reader.setXPPFactory(factory);
}
public void process(String stanza) throws Exception {
public void process(String stanza, XMPPPacketReader reader) throws Exception {
boolean initialStream = stanza.startsWith(STREAM_START);
if (!sessionCreated || initialStream) {
......@@ -114,7 +93,7 @@ public abstract class StanzaHandler {
// Found an stream:stream tag...
if (!sessionCreated) {
sessionCreated = true;
MXParser parser = (MXParser) factory.newPullParser();
MXParser parser = reader.getXPPParser();
parser.setInput(new StringReader(stanza));
createSession(parser);
} else if (startedSASL && saslStatus == SASLAuthentication.Status.authenticated) {
......
......@@ -13,11 +13,17 @@ package org.jivesoftware.wildfire.nio;
import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.dom4j.io.XMPPPacketReader;
import org.jivesoftware.util.Log;
import org.jivesoftware.wildfire.Connection;
import org.jivesoftware.wildfire.net.MXParser;
import org.jivesoftware.wildfire.net.StanzaHandler;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* A ConnectionHandler is responsible for creating new sessions, destroying sessions and delivering
......@@ -36,7 +42,21 @@ public abstract class ConnectionHandler extends IoHandlerAdapter {
private static final String CONNECTION = "CONNECTION";
protected String serverName;
private static Map<Integer, XMPPPacketReader> parsers = new ConcurrentHashMap<Integer, XMPPPacketReader>();
/**
* Reuse the same factory for all the connections.
*/
private static XmlPullParserFactory factory = null;
static {
try {
factory = XmlPullParserFactory.newInstance(MXParser.class.getName(), null);
factory.setNamespaceAware(true);
}
catch (XmlPullParserException e) {
Log.error("Error creating a parser factory", e);
}
}
protected ConnectionHandler(String serverName) {
this.serverName = serverName;
......@@ -88,9 +108,20 @@ public abstract class ConnectionHandler extends IoHandlerAdapter {
//System.out.println("RCVD: " + message);
// Get the stanza handler for this session
StanzaHandler handler = (StanzaHandler) session.getAttribute(HANDLER);
// Get the parser to use to process stanza. For optimization there is going
// to be a parser for each running thread. Each Filter will be executed
// by the Executor placed as the first Filter. So we can have a parser associated
// to each Thread
int hashCode = Thread.currentThread().hashCode();
XMPPPacketReader parser = parsers.get(hashCode);
if (parser == null) {
parser = new XMPPPacketReader();
parser.setXPPFactory(factory);
parsers.put(hashCode, parser);
}
// Let the stanza handler process the received stanza
try {
handler.process( (String) message);
handler.process((String) message, parser);
} catch (Exception e) {
Log.error("Closing connection due to error while processing message: " + message, e);
Connection connection = (Connection) session.getAttribute(CONNECTION);
......
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