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.CharStream; 23 import org.apache.velocity.runtime.parser.Parser; 24 import org.apache.velocity.util.SimplePool; 25 import org.slf4j.Logger; 26 27 /** 28 * This wraps the original parser SimplePool class. It also handles 29 * instantiating ad-hoc parsers if none are available. 30 * 31 * @author <a href="mailto:sergek@lokitech.com">Serge Knystautas</a> 32 * @version $Id: RuntimeInstance.java 384374 2006-03-08 23:19:30Z nbubna $ 33 * @since 1.5 34 */ 35 public class ParserPoolImpl implements ParserPool { 36 37 SimplePool pool = null; 38 int max = RuntimeConstants.NUMBER_OF_PARSERS; 39 Logger log; 40 41 /** 42 * Create the underlying "pool". 43 * @param rsvc 44 */ 45 @Override initialize(RuntimeServices rsvc)46 public void initialize(RuntimeServices rsvc) 47 { 48 log = rsvc.getLog("parser"); 49 max = rsvc.getInt(RuntimeConstants.PARSER_POOL_SIZE, RuntimeConstants.NUMBER_OF_PARSERS); 50 pool = new SimplePool(max); 51 52 for (int i = 0; i < max; i++) 53 { 54 pool.put(rsvc.createNewParser()); 55 } 56 57 log.debug("Created '{}' parsers.", max); 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 @Override get()66 public Parser get() 67 { 68 return (Parser) pool.get(); 69 } 70 71 /** 72 * Call the wrapped pool. 73 * @param parser 74 */ 75 @Override put(Parser parser)76 public void put(Parser parser) 77 { 78 parser.ReInit((CharStream) null); 79 pool.put(parser); 80 } 81 } 82