01: package org.apache.velocity.runtime;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "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: * This wraps the original parser SimplePool class. It also handles
27: * instantiating ad-hoc parsers if none are available.
28: *
29: * @author <a href="mailto:sergek@lokitech.com">Serge Knystautas</a>
30: * @version $Id: RuntimeInstance.java 384374 2006-03-08 23:19:30Z nbubna $
31: */
32: public class ParserPoolImpl implements ParserPool {
33:
34: RuntimeServices rsvc = null;
35: SimplePool pool = null;
36: int max = RuntimeConstants.NUMBER_OF_PARSERS;
37:
38: /**
39: * Create the underlying "pool".
40: * @param rsvc
41: */
42: public void initialize(RuntimeServices rsvc) {
43: this .rsvc = rsvc;
44: max = rsvc.getInt(RuntimeConstants.PARSER_POOL_SIZE,
45: RuntimeConstants.NUMBER_OF_PARSERS);
46: pool = new SimplePool(max);
47:
48: for (int i = 0; i < max; i++) {
49: pool.put(rsvc.createNewParser());
50: }
51:
52: if (rsvc.getLog().isDebugEnabled()) {
53: rsvc.getLog().debug("Created '" + max + "' parsers.");
54: }
55: }
56:
57: /**
58: * Call the wrapped pool. If none are available, it will create a new
59: * temporary one.
60: * @return A parser Object.
61: */
62: public Parser get() {
63: Parser parser = (Parser) pool.get();
64: if (parser == null) {
65: rsvc.getLog().debug(
66: "Created new "
67: + "parser (pool exhausted). Consider "
68: + "increasing pool size.");
69: parser = rsvc.createNewParser();
70: }
71: return parser;
72: }
73:
74: /**
75: * Call the wrapped pool.
76: * @param parser
77: */
78: public void put(Parser parser) {
79: pool.put(parser);
80: }
81: }
|