1 package org.apache.velocity.runtime;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.velocity.runtime.parser.Parser;
23 import org.apache.velocity.util.SimplePool;
24
25
26 /**
27 * This wraps the original parser SimplePool class. It also handles
28 * instantiating ad-hoc parsers if none are available.
29 *
30 * @author <a href="mailto:sergek@lokitech.com">Serge Knystautas</a>
31 * @version $Id: RuntimeInstance.java 384374 2006-03-08 23:19:30Z nbubna $
32 */
33 public class ParserPoolImpl implements ParserPool {
34
35 RuntimeServices rsvc = null;
36 SimplePool pool = null;
37 int max = RuntimeConstants.NUMBER_OF_PARSERS;
38
39 /**
40 * Create the underlying "pool".
41 * @param rsvc
42 */
43 public void initialize(RuntimeServices rsvc)
44 {
45 this.rsvc = rsvc;
46 max = rsvc.getInt(RuntimeConstants.PARSER_POOL_SIZE, RuntimeConstants.NUMBER_OF_PARSERS);
47 pool = new SimplePool(max);
48
49 for (int i = 0; i < max; i++)
50 {
51 pool.put(rsvc.createNewParser());
52 }
53
54 if (rsvc.getLog().isDebugEnabled())
55 {
56 rsvc.getLog().debug("Created '" + max + "' parsers.");
57 }
58 }
59
60 /**
61 * Call the wrapped pool. If none are available, it will create a new
62 * temporary one.
63 * @return A parser Object.
64 */
65 public Parser get()
66 {
67 Parser parser = (Parser) pool.get();
68 if (parser == null)
69 {
70 rsvc.getLog().debug("Created new " +
71 "parser (pool exhausted). Consider " +
72 "increasing pool size.");
73 parser = rsvc.createNewParser();
74 }
75 return parser;
76 }
77
78 /**
79 * Call the wrapped pool.
80 * @param parser
81 */
82 public void put(Parser parser)
83 {
84 pool.put(parser);
85 }
86 }