Author: jbalunas(a)redhat.com
Date: 2011-01-31 12:30:51 -0500 (Mon, 31 Jan 2011)
New Revision: 21338
Added:
branches/enterprise/3.3.X/framework/impl/src/main/java/org/ajax4jsf/util/DocumentBuilderPool.java
Modified:
branches/enterprise/3.3.X/framework/impl/src/main/java/org/ajax4jsf/context/ResponseWriterContentHandler.java
Log:
RF-10035 updated 3.3.1.SP3-SNAPSHOT with changes
Modified:
branches/enterprise/3.3.X/framework/impl/src/main/java/org/ajax4jsf/context/ResponseWriterContentHandler.java
===================================================================
---
branches/enterprise/3.3.X/framework/impl/src/main/java/org/ajax4jsf/context/ResponseWriterContentHandler.java 2011-01-31
17:24:35 UTC (rev 21337)
+++
branches/enterprise/3.3.X/framework/impl/src/main/java/org/ajax4jsf/context/ResponseWriterContentHandler.java 2011-01-31
17:30:51 UTC (rev 21338)
@@ -22,10 +22,11 @@
package org.ajax4jsf.context;
import javax.faces.FacesException;
-import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.ajax4jsf.renderkit.RendererUtils.HTML;
+import org.ajax4jsf.util.DocumentBuilderPool;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@@ -44,12 +45,21 @@
public ResponseWriterContentHandler(String linkClass) {
super();
+ DocumentBuilder builder = null;
+
try {
- Document document =
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+ //Pull the document builder from pool
+ builder = DocumentBuilderPool.getDocumentBuilder();
+
+ //create a new document & initialize
+ Document document = builder.newDocument();
node = document.createElement("head");
document.appendChild(node);
} catch (ParserConfigurationException e) {
throw new FacesException(e.getLocalizedMessage(), e);
+ }finally{
+ //Must always return the document builder to pool
+ DocumentBuilderPool.returnDocumentBuilder(builder);
}
this.linkClass = linkClass;
@@ -127,5 +137,4 @@
return list;
}
-
}
\ No newline at end of file
Added:
branches/enterprise/3.3.X/framework/impl/src/main/java/org/ajax4jsf/util/DocumentBuilderPool.java
===================================================================
---
branches/enterprise/3.3.X/framework/impl/src/main/java/org/ajax4jsf/util/DocumentBuilderPool.java
(rev 0)
+++
branches/enterprise/3.3.X/framework/impl/src/main/java/org/ajax4jsf/util/DocumentBuilderPool.java 2011-01-31
17:30:51 UTC (rev 21338)
@@ -0,0 +1,99 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright ${year}, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.ajax4jsf.util;
+
+import java.util.LinkedList;
+import java.util.NoSuchElementException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+/**
+ * Manages a shared pool of DocumentBuilders so that there is not a performance
+ * hit for every request to create.
+ *
+ * @author balunasj(a)redhat.com
+ *
+ */
+public final class DocumentBuilderPool {
+
+ /**
+ * How big the pool is
+ */
+ private static final int DOC_BUILDER_POOL_SIZE = 100;
+
+ /**
+ * Creates and holds the pool using the Initialization On Demand Holder idiom.
+ */
+ private static class DocumentBuilderPoolHolder {
+ static LinkedList<DocumentBuilder> _documentBuilderPool = new
LinkedList<DocumentBuilder>();
+ }
+
+ /**
+ * Private constructor
+ */
+ private DocumentBuilderPool (){}
+
+ /**
+ * Get a pooled instance of DocumentBuilder
+ * @return Pooled DocumentBulder
+ * @throws ParserConfigurationException
+ */
+ public static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException{
+ DocumentBuilder builder = null;
+
+ try {
+ //freeze the pool
+ synchronized (DocumentBuilderPoolHolder._documentBuilderPool) {
+ //pop the top builder - if empty it will through EmptyStackException
+ builder = (DocumentBuilder) DocumentBuilderPoolHolder._documentBuilderPool.pop();
+ }
+ } catch (NoSuchElementException e) {
+ //Stack is empty, create a new builder
+ builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ }
+
+ return builder;
+ }
+
+ /**
+ * Returns a builder to the pool
+ * @param builder
+ */
+ public static void returnDocumentBuilder(DocumentBuilder builder) {
+ //validate the builder
+ if (null != builder) {
+ synchronized (DocumentBuilderPoolHolder._documentBuilderPool) {
+ //only add if the pool is full
+ if (DocumentBuilderPoolHolder._documentBuilderPool.size() < DOC_BUILDER_POOL_SIZE)
{
+ //reset the builder to initial state
+ builder.reset();
+
+ //push the builder back
+ DocumentBuilderPoolHolder._documentBuilderPool.push(builder);
+ }
+ }
+ }
+ }
+
+}