Author: jbalunas(a)redhat.com
Date: 2010-12-17 11:26:12 -0500 (Fri, 17 Dec 2010)
New Revision: 20659
Modified:
branches/enterprise/3.3.1.SP2_RFPL-967/framework/impl/src/main/java/org/ajax4jsf/util/DocumentBuilderPool.java
Log:
RFPL-967, RF-10035 Updated based on review
Modified:
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 2010-12-17
16:17:57 UTC (rev 20658)
+++
branches/enterprise/3.3.1.SP2_RFPL-967/framework/impl/src/main/java/org/ajax4jsf/util/DocumentBuilderPool.java 2010-12-17
16:26:12 UTC (rev 20659)
@@ -36,15 +36,26 @@
* @author balunasj(a)redhat.com
*
*/
-public class DocumentBuilderPool {
+public final class DocumentBuilderPool {
- // How big the pool is
- private static final int POOL_SIZE = 100;
-
- // status array for pool storage
- private static ArrayStack _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 ArrayStack _documentBuilderPool = new ArrayStack(DOC_BUILDER_POOL_SIZE);
+ }
+
+ /**
+ * Private constructor
+ */
+ private DocumentBuilderPool (){}
+
+ /**
* Get a pooled instance of DocumentBuilder
* @return Pooled DocumentBulder
* @throws ParserConfigurationException
@@ -52,22 +63,11 @@
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) {
+ synchronized (DocumentBuilderPoolHolder._documentBuilderPool) {
//pop the top builder - if empty it will through EmptyStackException
- builder = (DocumentBuilder) _documentBuilderPool.pop();
+ builder = (DocumentBuilder) DocumentBuilderPoolHolder._documentBuilderPool.pop();
}
} catch (EmptyStackException e) {
//Stack is empty, create a new builder
@@ -84,14 +84,14 @@
public static void returnDocumentBuilder(DocumentBuilder builder) {
//validate the builder
if (null != builder) {
- synchronized (_documentBuilderPool) {
+ synchronized (DocumentBuilderPoolHolder._documentBuilderPool) {
//only add if the pool is full
- if (_documentBuilderPool.size() < POOL_SIZE) {
+ if (DocumentBuilderPoolHolder._documentBuilderPool.size() < DOC_BUILDER_POOL_SIZE)
{
//reset the builder to initial state
builder.reset();
//push the builder back
- _documentBuilderPool.push(builder);
+ DocumentBuilderPoolHolder._documentBuilderPool.push(builder);
}
}
}
Show replies by date