geirm 01/03/30 21:28:01
Modified: src/java/org/apache/velocity/runtime Runtime.java
RuntimeConstants.java
Log:
1) Made the number of parsers created at init() time a property, so it
can be set externally. 20, the default should be enough for many, but
for a busy site, being able to set this is important.
2) Added code to catch when we run out of parsers. In that event, a
new on is created, used and disposed of, and a notice is put into the
log. This is considered right now an exceptional situation. Will
revisit to put in a dynamically growable parserpool if needs warrant.
Added new property
parser.pool.size
Revision Changes Path
1.106 +49 -7
jakarta-velocity/src/java/org/apache/velocity/runtime/Runtime.java
Index: Runtime.java
===================================================================
RCS file:
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/Runtime.java,v
retrieving revision 1.105
retrieving revision 1.106
diff -u -r1.105 -r1.106
--- Runtime.java 2001/03/26 04:18:52 1.105
+++ Runtime.java 2001/03/31 05:28:01 1.106
@@ -142,7 +142,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Jeff Bowden</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Geir Magusson Jr.</a>
- * @version $Id: Runtime.java,v 1.105 2001/03/26 04:18:52 jvanzyl Exp $
+ * @version $Id: Runtime.java,v 1.106 2001/03/31 05:28:01 geirm Exp $
*/
public class Runtime implements RuntimeConstants
{
@@ -544,12 +544,16 @@
*/
private static void initializeParserPool()
{
- parserPool = new SimplePool(NUMBER_OF_PARSERS);
- for (int i=0;i<NUMBER_OF_PARSERS ;i++ )
+ int numParsers = getInt( PARSER_POOL_SIZE, NUMBER_OF_PARSERS);
+
+ parserPool = new SimplePool( numParsers);
+
+ for (int i=0; i < numParsers ;i++ )
{
parserPool.put (createNewParser());
}
- Runtime.info ("Created: " + NUMBER_OF_PARSERS + " parsers.");
+
+ Runtime.info ("Created: " + numParsers + " parsers.");
}
/**
@@ -567,6 +571,14 @@
/**
* Parse the input stream and return the root of
* AST node structure.
+ * <br><br>
+ * In the event that it runs out of parsers in the
+ * pool, it will create and let them be GC'd
+ * dynamically, logging that it has to do that. This
+ * is considered an exceptional condition. It is
+ * expected that the user will set the
+ * PARSER_POOL_SIZE property appropriately for their
+ * application. We will revisit this.
*
* @param InputStream inputstream retrieved by a resource loader
* @param String name of the template being parsed
@@ -576,7 +588,31 @@
{
SimpleNode ast = null;
Parser parser = (Parser) parserPool.get();
-
+ boolean madeNew = false;
+
+ if (parser == null)
+ {
+ /*
+ * if we couldn't get a parser from the pool
+ * make one and log it.
+ */
+
+ error("Runtime : ran out of parsers. Creating new. "
+ + " Please increment the parser.ppol.size property."
+ + " The current value is too small.");
+
+ parser = createNewParser();
+
+ if( parser != null )
+ {
+ madeNew = true;
+ }
+ }
+
+ /*
+ * now, if we have a parser
+ */
+
if (parser != null)
{
try
@@ -585,12 +621,18 @@
}
finally
{
- parserPool.put(parser);
+ /*
+ * if this came from the pool, then put back
+ */
+ if (!madeNew)
+ {
+ parserPool.put(parser);
+ }
}
}
else
{
- error("Runtime : ran out of parsers!");
+ error("Runtime : ran out of parsers and unable to create more.");
}
return ast;
}
1.23 +3 -1
jakarta-velocity/src/java/org/apache/velocity/runtime/RuntimeConstants.java
Index: RuntimeConstants.java
===================================================================
RCS file:
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/RuntimeConstants.java,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -r1.22 -r1.23
--- RuntimeConstants.java 2001/03/20 01:11:14 1.22
+++ RuntimeConstants.java 2001/03/31 05:28:01 1.23
@@ -62,7 +62,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Jon S. Stevens</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
- * @version $Id: RuntimeConstants.java,v 1.22 2001/03/20 01:11:14 jon Exp $
+ * @version $Id: RuntimeConstants.java,v 1.23 2001/03/31 05:28:01 geirm Exp $
*/
public interface RuntimeConstants
{
@@ -360,4 +360,6 @@
* Number of parsers to create
*/
final static int NUMBER_OF_PARSERS = 20;
+
+ final static String PARSER_POOL_SIZE = "parser.pool.size";
}