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 import org.apache.velocity.runtime.parser.CharStream;
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 * @since 1.5
33 */
34 public class ParserPoolImpl implements ParserPool {
35
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 max = rsvc.getInt(RuntimeConstants.PARSER_POOL_SIZE, RuntimeConstants.NUMBER_OF_PARSERS);
46 pool = new SimplePool(max);
47
48 for (int i = 0; i < max; i++)
49 {
50 pool.put(rsvc.createNewParser());
51 }
52
53 if (rsvc.getLog().isDebugEnabled())
54 {
55 rsvc.getLog().debug("Created '" + max + "' parsers.");
56 }
57 }
58
59 /**
60 * Call the wrapped pool. If none are available, it will create a new
61 * temporary one.
62 * @return A parser Object.
63 */
64 public Parser get()
65 {
66 return (Parser) pool.get();
67 }
68
69 /**
70 * Call the wrapped pool.
71 * @param parser
72 */
73 public void put(Parser parser)
74 {
75 parser.ReInit((CharStream) null);
76 pool.put(parser);
77 }
78 }