com.sun.xml.messaging.saaj.util.ParserPool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of saaj-impl Show documentation
Show all versions of saaj-impl Show documentation
Open source Reference Implementation of JSR-67: SOAP with Attachments API for Java (SAAJ MR: 1.4)
The newest version!
/*
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package com.sun.xml.messaging.saaj.util;
import org.xml.sax.SAXException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
/**
* Pool of SAXParser objects
*/
public class ParserPool {
private final BlockingQueue queue;
private SAXParserFactory factory;
public ParserPool(int capacity) {
queue = new ArrayBlockingQueue<>(capacity);
factory = SAXParserFactory.newInstance("com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl", SAAJUtil.getSystemClassLoader());
try {
factory.setFeature("jdk.xml.resetSymbolTable", true);
} catch(SAXException | ParserConfigurationException e) {
}
factory.setNamespaceAware(true);
for (int i = 0; i < capacity; i++) {
try {
queue.put(factory.newSAXParser());
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new RuntimeException(ex);
} catch (ParserConfigurationException | SAXException ex) {
throw new RuntimeException(ex);
}
}
}
public SAXParser get() throws ParserConfigurationException,
SAXException {
try {
return queue.take();
} catch (InterruptedException ex) {
throw new SAXException(ex);
}
}
public boolean put(SAXParser parser) {
return queue.offer(parser);
}
public void returnParser(SAXParser saxParser) {
saxParser.reset();
put(saxParser);
}
}