Coverage Report - org.apache.tapestry.services.impl.ExpressionEvaluatorImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ExpressionEvaluatorImpl
0%
0/87
0%
0/20
2.938
 
 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 ognl.*;
 18  
 import ognl.enhance.ExpressionAccessor;
 19  
 import org.apache.commons.pool.impl.GenericObjectPool;
 20  
 import org.apache.hivemind.ApplicationRuntimeException;
 21  
 import org.apache.hivemind.events.RegistryShutdownListener;
 22  
 import org.apache.hivemind.service.ClassFactory;
 23  
 import org.apache.tapestry.Tapestry;
 24  
 import org.apache.tapestry.services.ExpressionCache;
 25  
 import org.apache.tapestry.services.ExpressionEvaluator;
 26  
 import org.apache.tapestry.spec.IApplicationSpecification;
 27  
 
 28  
 import java.beans.Introspector;
 29  
 import java.util.Iterator;
 30  
 import java.util.List;
 31  
 import java.util.Map;
 32  
 
 33  
 /**
 34  
  * @since 4.0
 35  
  */
 36  0
 public class ExpressionEvaluatorImpl implements ExpressionEvaluator, RegistryShutdownListener {
 37  
     
 38  
     private static final long POOL_MIN_IDLE_TIME = 1000 * 60 * 50;
 39  
 
 40  
     private static final long POOL_SLEEP_TIME = 1000 * 60 * 4;
 41  
 
 42  
     // Uses Thread's context class loader
 43  
 
 44  0
     private final ClassResolver _ognlResolver = new OgnlClassResolver();
 45  
 
 46  
     private ExpressionCache _expressionCache;
 47  
 
 48  
     private IApplicationSpecification _applicationSpecification;
 49  
 
 50  
     private TypeConverter _typeConverter;
 51  
 
 52  
     private List _contributions;
 53  
     
 54  
     private List _nullHandlerContributions;
 55  
 
 56  
     // Context, with a root of null, used when evaluating an expression
 57  
     // to see if it is a constant.
 58  
 
 59  
     private Map _defaultContext;
 60  
     
 61  
     private ClassFactory _classFactory;
 62  
 
 63  
     private GenericObjectPool _contextPool;
 64  
 
 65  
     public void setApplicationSpecification(IApplicationSpecification applicationSpecification)
 66  
     {
 67  0
         _applicationSpecification = applicationSpecification;
 68  0
     }
 69  
 
 70  
     public void initializeService()
 71  
     {
 72  0
         if (_applicationSpecification.checkExtension(Tapestry.OGNL_TYPE_CONVERTER))
 73  0
             _typeConverter = (TypeConverter) _applicationSpecification.getExtension(Tapestry.OGNL_TYPE_CONVERTER, TypeConverter.class);
 74  
 
 75  0
         Iterator i = _contributions.iterator();
 76  
 
 77  0
         while (i.hasNext())
 78  
         {
 79  0
             PropertyAccessorContribution c = (PropertyAccessorContribution) i.next();
 80  
             
 81  0
             OgnlRuntime.setPropertyAccessor(c.getSubjectClass(), c.getAccessor());
 82  0
         }
 83  
         
 84  0
         Iterator j = _nullHandlerContributions.iterator();
 85  
         
 86  0
         while (j.hasNext())
 87  
         {
 88  0
             NullHandlerContribution h = (NullHandlerContribution) j.next();
 89  
             
 90  0
             OgnlRuntime.setNullHandler(h.getSubjectClass(), h.getHandler());
 91  0
         }
 92  
         
 93  0
         _defaultContext = Ognl.createDefaultContext(null, _ognlResolver, _typeConverter);
 94  
         
 95  0
         OgnlRuntime.setCompiler(new HiveMindExpressionCompiler(_classFactory));
 96  
         
 97  0
         _contextPool = new GenericObjectPool(new PoolableOgnlContextFactory(_ognlResolver, _typeConverter));
 98  
 
 99  0
         _contextPool.setMaxActive(-1);
 100  0
         _contextPool.setMaxIdle(-1);
 101  0
         _contextPool.setMinEvictableIdleTimeMillis(POOL_MIN_IDLE_TIME);
 102  0
         _contextPool.setTimeBetweenEvictionRunsMillis(POOL_SLEEP_TIME);
 103  0
     }
 104  
 
 105  
     public Object read(Object target, String expression)
 106  
     {
 107  0
         Node node = (Node)_expressionCache.getCompiledExpression(target, expression);
 108  
         
 109  0
         if (node.getAccessor() != null)
 110  0
             return read(target, node.getAccessor());
 111  
         
 112  0
         return readCompiled(target, node);
 113  
     }
 114  
 
 115  
     public Object readCompiled(Object target, Object expression)
 116  
     {
 117  0
         OgnlContext context = null;
 118  
         try
 119  
         {
 120  0
             context = (OgnlContext)_contextPool.borrowObject();
 121  0
             context.setRoot(target);
 122  
 
 123  0
             return Ognl.getValue(expression, context, target);
 124  
         }
 125  0
         catch (Exception ex)
 126  
         {
 127  0
             throw new ApplicationRuntimeException(ImplMessages.unableToReadExpression(ImplMessages
 128  
                     .parsedExpression(), target, ex), target, null, ex);
 129  
         } finally {
 130  0
             try { if (context != null) _contextPool.returnObject(context); } catch (Exception e) {}
 131  
         }
 132  
     }
 133  
     
 134  
     public Object read(Object target, ExpressionAccessor expression)
 135  
     {
 136  0
         OgnlContext context = null;
 137  
         try
 138  
         {
 139  0
             context = (OgnlContext)_contextPool.borrowObject();
 140  
             
 141  0
             return expression.get(context, target);
 142  
         }
 143  0
         catch (Exception ex)
 144  
         {
 145  0
             throw new ApplicationRuntimeException(ImplMessages.unableToReadExpression(ImplMessages.parsedExpression(),
 146  
                                                                                       target, ex), target, null, ex);
 147  
         } finally {
 148  0
             try { if (context != null) _contextPool.returnObject(context); } catch (Exception e) {}
 149  
         }
 150  
     }
 151  
     
 152  
     public OgnlContext createContext(Object target)
 153  
     {
 154  0
         OgnlContext result = (OgnlContext)Ognl.createDefaultContext(target, _ognlResolver);
 155  
 
 156  0
         if (_typeConverter != null)
 157  0
             Ognl.setTypeConverter(result, _typeConverter);
 158  
 
 159  0
         return result;
 160  
     }
 161  
 
 162  
     public void write(Object target, String expression, Object value)
 163  
     {
 164  0
         writeCompiled(target, _expressionCache.getCompiledExpression(target, expression), value);
 165  0
     }
 166  
 
 167  
     public void write(Object target, ExpressionAccessor expression, Object value)
 168  
     {
 169  0
         OgnlContext context = null;
 170  
         try
 171  
         {
 172  0
             context = (OgnlContext)_contextPool.borrowObject();
 173  
 
 174  
             // setup context
 175  
             
 176  0
             context.setRoot(target);
 177  
 
 178  0
             expression.set(context, target, value);
 179  
         }
 180  0
         catch (Exception ex)
 181  
         {
 182  0
             throw new ApplicationRuntimeException(ImplMessages.unableToWriteExpression(ImplMessages
 183  
                     .parsedExpression(), target, value, ex), target, null, ex);
 184  
         } finally {
 185  0
             try { if (context != null) _contextPool.returnObject(context); } catch (Exception e) {}
 186  0
         }
 187  0
     }
 188  
     
 189  
     public void writeCompiled(Object target, Object expression, Object value)
 190  
     {
 191  0
         OgnlContext context = null;
 192  
         try
 193  
         {
 194  0
             context = (OgnlContext)_contextPool.borrowObject();
 195  
 
 196  0
             Ognl.setValue(expression, context, target, value);
 197  
         }
 198  0
         catch (Exception ex)
 199  
         {
 200  0
             throw new ApplicationRuntimeException(ImplMessages.unableToWriteExpression(ImplMessages
 201  
                     .parsedExpression(), target, value, ex), target, null, ex);
 202  
         } finally {
 203  0
             try { if (context != null) _contextPool.returnObject(context); } catch (Exception e) {}
 204  0
         }
 205  0
     }
 206  
     
 207  
     public boolean isConstant(Object target, String expression)
 208  
     {
 209  0
         Object compiled = _expressionCache.getCompiledExpression(target, expression);
 210  
 
 211  
         try
 212  
         {
 213  0
             return Ognl.isConstant(compiled, _defaultContext);
 214  
         }
 215  0
         catch (Exception ex)
 216  
         {
 217  0
             throw new ApplicationRuntimeException(ImplMessages.isConstantExpressionError(
 218  
                     expression,
 219  
                     ex), ex);
 220  
         }
 221  
     }
 222  
     
 223  
     public boolean isConstant(String expression)
 224  
     {
 225  0
         Object compiled = _expressionCache.getCompiledExpression(expression);
 226  
 
 227  
         try
 228  
         {
 229  0
             return Ognl.isConstant(compiled, _defaultContext);
 230  
         }
 231  0
         catch (Exception ex)
 232  
         {
 233  0
             throw new ApplicationRuntimeException(ImplMessages.isConstantExpressionError(
 234  
                     expression,
 235  
                     ex), ex);
 236  
         }
 237  
     }
 238  
 
 239  
     public void registryDidShutdown()
 240  
     {
 241  
         try
 242  
         {
 243  0
             _contextPool.clear();
 244  0
             _contextPool.close();
 245  
 
 246  0
             OgnlRuntime.clearCache();
 247  0
             Introspector.flushCaches();
 248  
             
 249  0
         } catch (Exception et) {
 250  
             // ignore
 251  0
         }
 252  0
     }
 253  
 
 254  
     public void setExpressionCache(ExpressionCache expressionCache)
 255  
     {
 256  0
         _expressionCache = expressionCache;
 257  0
     }
 258  
 
 259  
     public void setContributions(List contributions)
 260  
     {
 261  0
         _contributions = contributions;
 262  0
     }
 263  
     
 264  
     public void setNullHandlerContributions(List nullHandlerContributions)
 265  
     {
 266  0
         _nullHandlerContributions = nullHandlerContributions;
 267  0
     }    
 268  
     
 269  
     public void setClassFactory(ClassFactory classFactory)
 270  
     {
 271  0
         _classFactory = classFactory;
 272  0
     }
 273  
 }