001 // Copyright 2004, 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.services.impl; 016 017 import java.util.HashMap; 018 import java.util.LinkedList; 019 import java.util.List; 020 import java.util.Map; 021 022 import org.apache.tapestry.event.ResetEventListener; 023 import org.apache.tapestry.services.ObjectPool; 024 025 /** 026 * Implementation of the {@link org.apache.tapestry.services.ObjectPool} interface. 027 * 028 * <p> 029 * This ia a minimal implementation, one that has no concept of automatically removing 030 * unused pooled objects. Eventually, it will also register for notifications about 031 * general cache cleaning. 032 * 033 * @author Howard Lewis Ship 034 * @since 4.0 035 */ 036 public class ObjectPoolImpl implements ObjectPool, ResetEventListener 037 { 038 /** 039 * Pool of Lists (of pooled objects), keyed on arbitrary key. 040 */ 041 private Map _pool = new HashMap(); 042 043 public synchronized Object get(Object key) 044 { 045 List pooled = (List) _pool.get(key); 046 047 if (pooled == null || pooled.isEmpty()) 048 return null; 049 050 return pooled.remove(0); 051 } 052 053 public synchronized void store(Object key, Object value) 054 { 055 List pooled = (List) _pool.get(key); 056 057 if (pooled == null) 058 { 059 pooled = new LinkedList(); 060 _pool.put(key, pooled); 061 } 062 063 pooled.add(value); 064 } 065 066 public synchronized void resetEventDidOccur() 067 { 068 _pool.clear(); 069 } 070 071 }