Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OF-2244 Store presence stanza for offline subscription requests #1855

Merged
merged 15 commits into from
Jul 7, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@

package org.jivesoftware.util.cache;

import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
* Utility methods to assist in working with the Externalizable interfaces. This class
Expand All @@ -35,14 +41,37 @@
*/
public class ExternalizableUtil {

private static ExternalizableUtil instance = new ExternalizableUtil();
private static ExternalizableUtil instance;

private ExternalizableUtilStrategy strategy = new DefaultExternalizableUtil();

static {
instance = new ExternalizableUtil();
}

/**
* Pool of SAX Readers. SAXReader is not thread safe so we need to have a pool of readers.
*/
private final static int POOL_SIZE = 10;
private final BlockingQueue<SAXReader> xmlReaders = new LinkedBlockingQueue<>(POOL_SIZE);
guusdk marked this conversation as resolved.
Show resolved Hide resolved

private SAXReader takeSAXReader() throws InterruptedException {
SAXReader xmlReader;
if (! xmlReaders.isEmpty()) {
xmlReader = new SAXReader();
xmlReader.setEncoding("UTF-8");
guusdk marked this conversation as resolved.
Show resolved Hide resolved
} else {
xmlReader = xmlReaders.take();
}
return xmlReader;
}
guusdk marked this conversation as resolved.
Show resolved Hide resolved

private void returnSAXReader(SAXReader xmlReader) {
if (xmlReader != null) {
xmlReaders.offer(xmlReader); // If full, toss it.
}
}

public static ExternalizableUtil getInstance() {
return instance;
}
Expand Down Expand Up @@ -372,4 +401,20 @@ public void writeStrings(DataOutput out, Collection<String> collection) throws I
public int readStrings(DataInput in, Collection<String> collection) throws IOException {
return strategy.readStrings(in, collection);
}

public void writeXML(DataOutput out, Element element) throws IOException {
strategy.writeSafeUTF(out, element.asXML());
}

public Element readXML(DataInput in) throws IOException {
SAXReader xmlReader = null;
try {
xmlReader = takeSAXReader();
return xmlReader.read(new StringReader(readSafeUTF(in))).getRootElement();
} catch(DocumentException | InterruptedException e) {
throw new IOException(e.getMessage());
} finally {
returnSAXReader(xmlReader);
}
}
}