Author: wglass Date: Tue Mar 14 20:11:43 2006 New Revision: 385967 URL: http://svn.apache.org/viewcvs?rev=385967&view=rev Log: Change the Parser Pool to be an interface with a default implementation. Thanks to Serge Knystautas for the idea and patch. VELOCITY-433.
Added: jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/ParserPool.java jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/ParserPoolImpl.java Modified: jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeConstants.java jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeServices.java jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/defaults/velocity.properties Added: jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/ParserPool.java URL: http://svn.apache.org/viewcvs/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/ParserPool.java?rev=385967&view=auto ============================================================================== --- jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/ParserPool.java (added) +++ jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/ParserPool.java Tue Mar 14 20:11:43 2006 @@ -0,0 +1,45 @@ +package org.apache.velocity.runtime; + +import org.apache.velocity.runtime.parser.Parser; + +/* + * Copyright 2000-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License") + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Provides instances of parsers as needed. get() will return a new parser if + * available. If a parser is acquired from the pool, put() should be called + * with that parser to make it available again for reuse. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Serge Knystautas</a> + * @version $Id: RuntimeInstance.java 384374 2006-03-08 23:19:30Z nbubna $ + */ +public interface ParserPool +{ + /** + * Initialize the pool so that it can begin serving parser instances. + */ + void initialize(RuntimeServices svc); + + /** + * Retrieve an instance of a parser pool. + */ + Parser get(); + + /** + * Return the parser to the pool so that it may be reused. + */ + void put(Parser parser); +} Added: jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/ParserPoolImpl.java URL: http://svn.apache.org/viewcvs/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/ParserPoolImpl.java?rev=385967&view=auto ============================================================================== --- jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/ParserPoolImpl.java (added) +++ jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/ParserPoolImpl.java Tue Mar 14 20:11:43 2006 @@ -0,0 +1,79 @@ +package org.apache.velocity.runtime; + +import org.apache.velocity.runtime.parser.Parser; +import org.apache.velocity.util.SimplePool; + +/* + * Copyright 2000-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License") + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * This wraps the original parser SimplePool class. It also handles + * instantiating ad-hoc parsers if none are available. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Serge Knystautas</a> + * @version $Id: RuntimeInstance.java 384374 2006-03-08 23:19:30Z nbubna $ + */ +public class ParserPoolImpl implements ParserPool { + + RuntimeServices rsvc = null; + SimplePool pool = null; + int max = RuntimeConstants.NUMBER_OF_PARSERS; + + /** + * Create the underlying "pool". + */ + public void initialize(RuntimeServices rsvc) + { + this.rsvc = rsvc; + max = rsvc.getInt(RuntimeConstants.PARSER_POOL_SIZE, RuntimeConstants.NUMBER_OF_PARSERS); + pool = new SimplePool(max); + + for (int i = 0; i < max; i++) + { + pool.put(rsvc.createNewParser()); + } + + if (rsvc.getLog().isDebugEnabled()) + { + rsvc.getLog().debug("Created '" + max + "' parsers."); + } + } + + /** + * Call the wrapped pool. If none are available, it will create a new + * temporary one. + */ + public Parser get() + { + Parser parser = (Parser) pool.get(); + if (parser == null) + { + rsvc.getLog().debug("Created new " + + "parser (pool exhausted). Consider " + + "increasing pool size."); + parser = rsvc.createNewParser(); + } + return parser; + } + + /** + * Call the wrapped pool. + */ + public void put(Parser parser) + { + pool.put(parser); + } +} Modified: jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeConstants.java URL: http://svn.apache.org/viewcvs/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeConstants.java?rev=385967&r1=385966&r2=385967&view=diff ============================================================================== --- jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeConstants.java (original) +++ jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeConstants.java Tue Mar 14 20:11:43 2006 @@ -337,6 +337,14 @@ public static final String UBERSPECT_CLASSNAME = "runtime.introspector.uberspect"; /** + * The <code>parser.pool.class</code> property + * specifies the name of the [EMAIL PROTECTED] + * org.apache.velocity.util.SimplePool} + * implementation to use. + */ + public static final String PARSER_POOL_CLASS = "parser.pool.class"; + + /** * @see #NUMBER_OF_PARSERS */ public static final String PARSER_POOL_SIZE = "parser.pool.size"; Modified: jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java URL: http://svn.apache.org/viewcvs/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java?rev=385967&r1=385966&r2=385967&view=diff ============================================================================== --- jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java (original) +++ jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java Tue Mar 14 20:11:43 2006 @@ -46,7 +46,6 @@ import org.apache.velocity.runtime.resource.ContentResource; import org.apache.velocity.runtime.resource.ResourceManager; import org.apache.velocity.util.ClassUtils; -import org.apache.velocity.util.SimplePool; import org.apache.velocity.util.StringUtils; import org.apache.velocity.util.introspection.Introspector; import org.apache.velocity.util.introspection.Uberspect; @@ -117,7 +116,7 @@ /** * The Runtime parser pool */ - private SimplePool parserPool; + private ParserPool parserPool; /** * Indicate whether the Runtime is in the midst of initialization. @@ -813,23 +812,67 @@ /** * Initializes the Velocity parser pool. - * This still needs to be implemented. */ private void initializeParserPool() + throws Exception { - int numParsers = getInt(PARSER_POOL_SIZE, NUMBER_OF_PARSERS); - - parserPool = new SimplePool(numParsers); - - for (int i = 0; i < numParsers; i++) + /* + * Which parser pool? + */ + String pp = getString(RuntimeConstants.PARSER_POOL_CLASS); + + if (pp != null && pp.length() > 0) { - parserPool.put(createNewParser()); + /* + * 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 Exception(err); + } + + if (!(o instanceof ParserPool)) + { + String err = "The specified class for ParserPool (" + + pp + + ") does not implement org.apache.velocity.runtime.ParserPool." + + " Velocity not initialized correctly."; + + log.error(err); + throw new Exception(err); + } + + parserPool = (ParserPool) o; + + parserPool.initialize(this); } - - if (log.isDebugEnabled()) + else { - log.debug("Created '" + numParsers + "' parsers."); + /* + * 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 Exception( err ); } + } /** @@ -958,12 +1001,10 @@ finally { /* - * if this came from the pool, then put back + * put it back */ - if (!madeNew) - { - parserPool.put(parser); - } + parserPool.put(parser); + } } else Modified: jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeServices.java URL: http://svn.apache.org/viewcvs/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeServices.java?rev=385967&r1=385966&r2=385967&view=diff ============================================================================== --- jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeServices.java (original) +++ jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeServices.java Tue Mar 14 20:11:43 2006 @@ -18,7 +18,6 @@ import java.io.Reader; import java.util.Properties; - import org.apache.commons.collections.ExtendedProperties; import org.apache.velocity.Template; import org.apache.velocity.app.event.EventCartridge; @@ -27,6 +26,7 @@ import org.apache.velocity.runtime.directive.Directive; import org.apache.velocity.runtime.log.Log; import org.apache.velocity.runtime.parser.ParseException; +import org.apache.velocity.runtime.parser.Parser; import org.apache.velocity.runtime.parser.node.SimpleNode; import org.apache.velocity.runtime.resource.ContentResource; import org.apache.velocity.util.introspection.Introspector; @@ -369,4 +369,8 @@ */ public boolean isInitialized(); + /** + * Create a new parser instance. + */ + public Parser createNewParser(); } Modified: jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/defaults/velocity.properties URL: http://svn.apache.org/viewcvs/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/defaults/velocity.properties?rev=385967&r1=385966&r2=385967&view=diff ============================================================================== --- jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/defaults/velocity.properties (original) +++ jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/defaults/velocity.properties Tue Mar 14 20:11:43 2006 @@ -120,6 +120,7 @@ #---------------------------------------------------------------------------- resource.manager.class = org.apache.velocity.runtime.resource.ResourceManagerImpl resource.manager.cache.class = org.apache.velocity.runtime.resource.ResourceCacheImpl +parser.pool.class = org.apache.velocity.runtime.ParserPoolImpl #---------------------------------------------------------------------------- --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]