Author: jbalunas(a)redhat.com
Date: 2010-12-17 10:24:53 -0500 (Fri, 17 Dec 2010)
New Revision: 20655
Added:
branches/enterprise/3.3.1.SP2_RFPL-967/framework/impl/src/main/java/org/ajax4jsf/util/DocumentBuilderPool.java
Modified:
branches/enterprise/3.3.1.SP2_RFPL-967/framework/impl/src/main/java/org/ajax4jsf/context/ResponseWriterContentHandler.java
Log:
RFPL-967, RF-10035 - added pooling for document builder
Modified:
branches/enterprise/3.3.1.SP2_RFPL-967/framework/impl/src/main/java/org/ajax4jsf/context/ResponseWriterContentHandler.java
===================================================================
---
branches/enterprise/3.3.1.SP2_RFPL-967/framework/impl/src/main/java/org/ajax4jsf/context/ResponseWriterContentHandler.java 2010-12-17
15:11:12 UTC (rev 20654)
+++
branches/enterprise/3.3.1.SP2_RFPL-967/framework/impl/src/main/java/org/ajax4jsf/context/ResponseWriterContentHandler.java 2010-12-17
15:24:53 UTC (rev 20655)
@@ -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.1.SP2_RFPL-967/framework/impl/src/main/java/org/ajax4jsf/util/DocumentBuilderPool.java
===================================================================
---
branches/enterprise/3.3.1.SP2_RFPL-967/framework/impl/src/main/java/org/ajax4jsf/util/DocumentBuilderPool.java
(rev 0)
+++
branches/enterprise/3.3.1.SP2_RFPL-967/framework/impl/src/main/java/org/ajax4jsf/util/DocumentBuilderPool.java 2010-12-17
15:24:53 UTC (rev 20655)
@@ -0,0 +1,100 @@
+/*
+ * 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.EmptyStackException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.apache.commons.collections.ArrayStack;
+
+/**
+ * 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 class DocumentBuilderPool {
+
+ // How big the pool is
+ private static final int POOL_SIZE = 100;
+
+ // status array for pool storage
+ private static ArrayStack _documentBuilderPool;
+
+ /**
+ * Get a pooled instance of DocumentBuilder
+ * @return Pooled DocumentBulder
+ * @throws ParserConfigurationException
+ */
+ public static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException{
+ DocumentBuilder builder = null;
+
+ //Check if pool needs to be initialized
+ if (_documentBuilderPool == null){
+ //Sync to avoid conflicts
+ synchronized (DocumentBuilderPool.class) {
+ //Second level check
+ if (_documentBuilderPool == null){
+ _documentBuilderPool = new ArrayStack(POOL_SIZE);
+ }
+ }
+ }
+
+ try {
+ //freeze the pool
+ synchronized (_documentBuilderPool) {
+ //pop the top builder - if empty it will through EmptyStackException
+ builder = (DocumentBuilder) _documentBuilderPool.pop();
+ }
+ } catch (EmptyStackException 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 (_documentBuilderPool) {
+ //only add if the pool is full
+ if (_documentBuilderPool.size() < POOL_SIZE) {
+ //reset the builder to initial state
+ builder.reset();
+
+ //push the builder back
+ _documentBuilderPool.push(builder);
+ }
+ }
+ }
+ }
+
+}