325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.messaging.saaj.util;
325N/A
325N/A
325N/Aimport org.xml.sax.SAXException;
325N/Aimport java.util.concurrent.ArrayBlockingQueue;
325N/Aimport java.util.concurrent.BlockingQueue;
325N/Aimport javax.xml.parsers.ParserConfigurationException;
325N/Aimport javax.xml.parsers.SAXParser;
325N/Aimport javax.xml.parsers.SAXParserFactory;
325N/Aimport org.xml.sax.SAXNotRecognizedException;
325N/Aimport org.xml.sax.SAXNotSupportedException;
325N/A
325N/A
325N/A/**
325N/A * Pool of SAXParser objects
325N/A */
325N/Apublic class ParserPool {
325N/A private final BlockingQueue queue;
325N/A private SAXParserFactory factory;
325N/A private int capacity;
325N/A
325N/A public ParserPool(int capacity) {
325N/A this.capacity = capacity;
325N/A queue = new ArrayBlockingQueue(capacity);
325N/A //factory = SAXParserFactory.newInstance();
325N/A factory = new com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl();
325N/A factory.setNamespaceAware(true);
325N/A for (int i=0; i < capacity; i++) {
325N/A try {
325N/A queue.put(factory.newSAXParser());
325N/A } catch (InterruptedException ex) {
325N/A Thread.currentThread().interrupt();
325N/A throw new RuntimeException(ex);
325N/A } catch (ParserConfigurationException ex) {
325N/A throw new RuntimeException(ex);
325N/A } catch (SAXException ex) {
325N/A throw new RuntimeException(ex);
325N/A }
325N/A }
325N/A }
325N/A
325N/A public SAXParser get() throws ParserConfigurationException,
325N/A SAXException {
325N/A
325N/A try {
325N/A return (SAXParser) queue.take();
325N/A } catch (InterruptedException ex) {
325N/A throw new SAXException(ex);
325N/A }
325N/A
325N/A }
325N/A
325N/A public void put(SAXParser parser) {
325N/A queue.offer(parser);
325N/A }
325N/A
325N/A public void returnParser(SAXParser saxParser) {
325N/A saxParser.reset();
325N/A resetSaxParser(saxParser);
325N/A put(saxParser);
325N/A }
325N/A
325N/A
325N/A /**
325N/A * SAAJ Issue 46 :https://saaj.dev.java.net/issues/show_bug.cgi?id=46
325N/A * Xerces does not provide a way to reset the SymbolTable
325N/A * So we are trying to reset it using the proprietary code below.
325N/A * Temporary Until the bug : https://jaxp.dev.java.net/issues/show_bug.cgi?id=59
325N/A * is fixed.
325N/A * @param parser the parser from the pool whose Symbol Table needs to be reset.
325N/A */
325N/A private void resetSaxParser(SAXParser parser) {
325N/A try {
325N/A //Object obj = parser.getProperty("http://apache.org/xml/properties/internal/symbol-table");
325N/A com.sun.org.apache.xerces.internal.util.SymbolTable table = new com.sun.org.apache.xerces.internal.util.SymbolTable();
325N/A parser.setProperty("http://apache.org/xml/properties/internal/symbol-table", table);
325N/A //obj = parser.getProperty("http://apache.org/xml/properties/internal/symbol-table");
325N/A } catch (SAXNotRecognizedException ex) {
325N/A //nothing to do
325N/A } catch (SAXNotSupportedException ex) {
325N/A //nothing to do
325N/A }
325N/A }
325N/A
325N/A}