private void initializeParserPool()

in velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java [1184:1284]


    private void initializeParserPool()
    {
        /*
         * First initialize parser class. If it's not valid or not found, it will generate an error
         * later on in this method when parser creation is tester.
         */
        String parserClassName = getString(PARSER_CLASS, DEFAULT_PARSER_CLASS);
        Class<? extends Parser> parserClass;
        try
        {
            parserClass = (Class<? extends Parser>)ClassUtils.getClass(parserClassName);
        }
        catch (ClassNotFoundException cnfe)
        {
            throw new VelocityException("parser class not found: " + parserClassName, cnfe);
        }
        try
        {
            parserConstructor = parserClass.getConstructor(RuntimeServices.class);
        }
        catch (NoSuchMethodException nsme)
        {
            throw new VelocityException("parser class must provide a constructor taking a RuntimeServices argument", nsme);
        }

        /*
         * Which parser pool?
         */
        String pp = getString(RuntimeConstants.PARSER_POOL_CLASS);

        if (pp != null && pp.length() > 0)
        {
            /*
             *  if something was specified, then make one.
             *  if that isn't a ParserPool, consider
             *  this a huge error and throw
             */

            Object o = null;

            try
            {
                o = ClassUtils.getNewInstance( pp );
            }
            catch (ClassNotFoundException cnfe )
            {
                String err = "The specified class for ParserPool ("
                    + pp
                    + ") does not exist (or is not accessible to the current classloader.";
                log.error(err);
                throw new VelocityException(err, cnfe);
            }
            catch (InstantiationException ie)
            {
              throw new VelocityException("Could not instantiate class '" + pp + "'", ie);
            }
            catch (IllegalAccessException ae)
            {
              throw new VelocityException("Cannot access class '" + pp + "'", ae);
            }

            if (!(o instanceof ParserPool))
            {
                String err = "The specified class for ParserPool ("
                    + pp + ") does not implement " + ParserPool.class
                    + " Velocity not initialized correctly.";

                log.error(err);
                throw new VelocityException(err);
            }

            parserPool = (ParserPool) o;

            parserPool.initialize(this);

            /*
             * test parser creation and use generated parser to fill up customized characters
             */
            Parser parser = parserPool.get();
            parserConfiguration = new ParserConfiguration();
            parserConfiguration.setDollarChar(parser.dollar());
            parserConfiguration.setHashChar(parser.hash());
            parserConfiguration.setAtChar(parser.at());
            parserConfiguration.setAsteriskChar(parser.asterisk());
            parserPool.put(parser);
        }
        else
        {
            /*
             *  someone screwed up.  Lets not fool around...
             */

            String err = "It appears that no class was specified as the"
                + " ParserPool.  Please ensure that all configuration"
                + " information is correct.";

            log.error(err);
            throw new VelocityException( err );
        }

    }