Coverage Report - org.apache.tapestry.services.impl.ExpressionCacheImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ExpressionCacheImpl
0%
0/52
0%
0/18
2.7
 
 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 ognl.ClassCacheInspector;
 19  
 import ognl.Node;
 20  
 import ognl.Ognl;
 21  
 import ognl.OgnlRuntime;
 22  
 import org.apache.hivemind.ApplicationRuntimeException;
 23  
 import org.apache.tapestry.AbstractComponent;
 24  
 import org.apache.tapestry.event.ReportStatusEvent;
 25  
 import org.apache.tapestry.event.ReportStatusListener;
 26  
 import org.apache.tapestry.event.ResetEventListener;
 27  
 import org.apache.tapestry.services.ExpressionCache;
 28  
 import org.apache.tapestry.services.ExpressionEvaluator;
 29  
 
 30  
 import java.beans.Introspector;
 31  
 import java.util.HashMap;
 32  
 import java.util.Map;
 33  
 import java.util.WeakHashMap;
 34  
 
 35  
 /**
 36  
  * @author Howard M. Lewis Ship
 37  
  * @since 4.0
 38  
  */
 39  0
 public class ExpressionCacheImpl implements ExpressionCache, ResetEventListener, ReportStatusListener, ClassCacheInspector {
 40  
 
 41  0
     private final ReentrantLock _lock = new ReentrantLock();
 42  
     
 43  
     private String _serviceId;
 44  
 
 45  0
     private Map _cache = new WeakHashMap();
 46  
     
 47  0
     private Map _objectCache = new WeakHashMap();
 48  
     
 49  
     private ExpressionEvaluator _evaluator;
 50  
 
 51  0
     private final boolean _cachingDisabled = Boolean.getBoolean("org.apache.tapestry.disable-caching");
 52  
 
 53  
     public void initializeService()
 54  
     {
 55  0
         if (_cachingDisabled)
 56  
         {
 57  0
             OgnlRuntime.setClassCacheInspector(this);
 58  
         }
 59  0
     }
 60  
 
 61  
     public void resetEventDidOccur()
 62  
     {
 63  
         try {
 64  
             
 65  0
             _lock.lock();
 66  
             
 67  0
             _cache.clear();
 68  0
             _objectCache.clear();
 69  
 
 70  0
             Introspector.flushCaches();
 71  
 
 72  
         } finally {
 73  
             
 74  0
             _lock.unlock();
 75  0
         }
 76  0
     }
 77  
 
 78  
     public boolean shouldCache(Class type)
 79  
     {
 80  0
         if (!_cachingDisabled || type == null
 81  0
             || AbstractComponent.class.isAssignableFrom(type))
 82  0
             return false;
 83  
 
 84  0
         return true;
 85  
     }
 86  
 
 87  
     public void reportStatus(ReportStatusEvent event)
 88  
     {
 89  0
         event.title(_serviceId);
 90  
 
 91  0
         event.property("cached expression count", _cache.size());
 92  0
         event.collection("cached expressions", _cache.keySet());
 93  
         
 94  0
         event.property("cached object expression count", _objectCache.size());
 95  0
     }
 96  
     
 97  
     public Object getCompiledExpression(Object target, String expression)
 98  
     {
 99  
         try {   
 100  
             
 101  0
             _lock.lock();
 102  
             
 103  0
             Map cached = (Map)_objectCache.get(target.getClass());
 104  
             
 105  0
             if (cached == null)
 106  
             {
 107  0
                 cached = new HashMap();
 108  0
                 _objectCache.put(target.getClass(), cached);
 109  
             }
 110  
             
 111  0
             Node result = (Node)cached.get(expression);
 112  
             
 113  0
             if (result == null || result.getAccessor() == null)
 114  
             {
 115  0
                 result = parse(target, expression);
 116  0
                 cached.put(expression, result);
 117  
             }
 118  
             
 119  0
             return result;
 120  
             
 121  
         } finally {
 122  
 
 123  0
             _lock.unlock();
 124  
         }
 125  
     }
 126  
     
 127  
     public Object getCompiledExpression(String expression)
 128  
     {
 129  
         try {
 130  
             
 131  0
             _lock.lock();
 132  
             
 133  0
             Object result = _cache.get(expression);
 134  
 
 135  0
             if (result == null)
 136  
             {
 137  0
                 result = parse(expression);
 138  0
                 _cache.put(expression, result);
 139  
             }
 140  
             
 141  0
             return result;
 142  
         } finally {
 143  
 
 144  0
             _lock.unlock();
 145  
         }
 146  
     }
 147  
 
 148  
     private Node parse(Object target, String expression)
 149  
     {
 150  
         try
 151  
         {
 152  0
             return Ognl.compileExpression(_evaluator.createContext(target), target, expression);
 153  
         }
 154  0
         catch (Exception ex)
 155  
         {
 156  0
             throw new ApplicationRuntimeException(ImplMessages.unableToParseExpression(expression,ex), ex);
 157  
         }
 158  
     }
 159  
     
 160  
     private Object parse(String expression)
 161  
     {
 162  
         try
 163  
         {
 164  0
             return Ognl.parseExpression(expression);
 165  
         }
 166  0
         catch (Exception ex)
 167  
         {
 168  0
             throw new ApplicationRuntimeException(ImplMessages.unableToParseExpression(expression, ex), ex);
 169  
         }
 170  
     }
 171  
 
 172  
     public void setServiceId(String serviceId)
 173  
     {
 174  0
         _serviceId = serviceId;
 175  0
     }
 176  
     
 177  
     public void setEvaluator(ExpressionEvaluator evaluator)
 178  
     {
 179  0
         _evaluator = evaluator;
 180  0
     }
 181  
     
 182  
 }