Coverage Report - org.apache.tapestry.services.impl.ObjectPoolImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectPoolImpl
0%
0/37
0%
0/8
2.2
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.services.impl;
 16  
 
 17  
 import edu.emory.mathcs.backport.java.util.concurrent.locks.ReentrantLock;
 18  
 import org.apache.tapestry.event.ReportStatusEvent;
 19  
 import org.apache.tapestry.event.ReportStatusListener;
 20  
 import org.apache.tapestry.event.ResetEventListener;
 21  
 import org.apache.tapestry.services.ObjectPool;
 22  
 
 23  
 import java.util.*;
 24  
 
 25  
 /**
 26  
  * Implementation of the {@link org.apache.tapestry.services.ObjectPool} interface.
 27  
  * <p>
 28  
  * This ia a minimal implementation, one that has no concept of automatically removing unused pooled
 29  
  * objects. Eventually, it will also register for notifications about general cache cleaning.
 30  
  * 
 31  
  * @author Howard Lewis Ship
 32  
  * @since 4.0
 33  
  */
 34  0
 public class ObjectPoolImpl implements ObjectPool, ResetEventListener, ReportStatusListener
 35  
 {
 36  
     private String _serviceId;
 37  
 
 38  0
     private int _count = 0;
 39  
 
 40  0
     private final ReentrantLock _lock = new ReentrantLock();
 41  
 
 42  
     /**
 43  
      * Pool of Lists (of pooled objects), keyed on arbitrary key.
 44  
      */
 45  0
     private Map _pool = new HashMap();
 46  
         
 47  
     public Object get(Object key)
 48  
     {
 49  0
         List pooled = (List) _pool.get(key);
 50  
         
 51  
         try
 52  
         {
 53  0
             _lock.lock();
 54  
             
 55  0
             if (pooled == null || pooled.isEmpty())
 56  0
                 return null;
 57  
 
 58  0
             _count--;
 59  
 
 60  0
             return pooled.remove(0);
 61  
 
 62  
         } finally
 63  
         {
 64  0
             _lock.unlock();
 65  
         }
 66  
     }
 67  
 
 68  
     public void store(Object key, Object value)
 69  
     {
 70  0
         List pooled = (List) _pool.get(key);
 71  
 
 72  
         try
 73  
         {
 74  0
             _lock.lock();
 75  
             
 76  0
             if (pooled == null)
 77  
             {
 78  0
                 pooled = new LinkedList();
 79  0
                 _pool.put(key, pooled);
 80  
             }
 81  
 
 82  0
             pooled.add(value);
 83  
 
 84  0
             _count++;
 85  
         } finally
 86  
         {
 87  0
             _lock.unlock();
 88  0
         }
 89  0
     }
 90  
 
 91  
     public void resetEventDidOccur()
 92  
     {
 93  0
         _pool.clear();
 94  
 
 95  0
         _count = 0;
 96  0
     }
 97  
 
 98  
     public void reportStatus(ReportStatusEvent event)
 99  
     {
 100  0
         event.title(_serviceId);
 101  
 
 102  0
         event.property("total count", _count);
 103  
 
 104  0
         event.section("Count by Key");
 105  
 
 106  0
         Iterator i = _pool.entrySet().iterator();
 107  
 
 108  0
         while (i.hasNext())
 109  
         {
 110  0
             Map.Entry entry = (Map.Entry) i.next();
 111  
 
 112  0
             String key = entry.getKey().toString();
 113  
 
 114  0
             List pooled = (List) entry.getValue();
 115  
 
 116  0
             event.property(key, pooled.size());
 117  0
         }
 118  0
     }
 119  
 
 120  
     public void setServiceId(String serviceId)
 121  
     {
 122  0
         _serviceId = serviceId;
 123  0
     }
 124  
 }