private static synchronized DocumentBuilder getDocumentBuilder()

in velocity-tools-generic/src/main/java/org/apache/velocity/tools/XmlUtils.java [216:253]


    private static synchronized DocumentBuilder getDocumentBuilder()
    {
        DocumentBuilder builder = null;
        if (canReuseBuilders && builderPool.size() > 0)
        {
            builder = builderPool.pollFirst().get();
        }
        if (builder == null)
        {
            if(!canReuseBuilders || currentBuildersCount < maxBuildersCount)
            {
                try
                {
                    builder = builderFactory.newDocumentBuilder();
                    builder.setErrorHandler(errorHandler);
                    ++currentBuildersCount;
                }
                catch(Exception e)
                {
                    /* this is a fatal error */
                    throw new RuntimeException("could not create a new XML DocumentBuilder instance", e);
                }
            }
            else
            {
                try
                {
                    LOGGER.warn("reached XML DocumentBuilder pool size limit, current thread needs to wait; you can increase pool size with the {} system property", BUILDER_MAX_INSTANCES_KEY);
                    builder = builderPool.takeFirst().get();
                }
                catch(InterruptedException ie)
                {
                    LOGGER.warn("caught an InterruptedException while waiting for a DocumentBuilder instance");
                }
            }
        }
        return builder;
    }