Coverage Report - org.apache.commons.ognl.OgnlCache
 
Classes in this File Line Coverage Branch Coverage Complexity
OgnlCache
93%
109/116
50%
4/8
1.5
OgnlCache$1
100%
2/2
N/A
1.5
OgnlCache$2
100%
2/2
N/A
1.5
OgnlCache$3
100%
2/2
N/A
1.5
 
 1  
 package org.apache.commons.ognl;
 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.commons.ognl.internal.*;
 23  
 import org.apache.commons.ognl.internal.entry.*;
 24  
 
 25  
 import java.beans.PropertyDescriptor;
 26  
 import java.lang.reflect.Constructor;
 27  
 import java.lang.reflect.Field;
 28  
 import java.lang.reflect.Method;
 29  
 import java.security.Permission;
 30  
 import java.util.Arrays;
 31  
 import java.util.Collection;
 32  
 import java.util.Enumeration;
 33  
 import java.util.Iterator;
 34  
 import java.util.List;
 35  
 import java.util.Map;
 36  
 import java.util.Set;
 37  
 
 38  
 /**
 39  
  * This class takes care of all the internal caching for OGNL.
 40  
  */
 41  1
 public class OgnlCache {
 42  
 
 43  1
     private final CacheFactory cacheFactory = new HashMapCacheFactory();
 44  
 
 45  1
     private final ClassCache<MethodAccessor> methodAccessors = cacheFactory.createClassCache();
 46  
     {
 47  1
         MethodAccessor methodAccessor = new ObjectMethodAccessor();
 48  1
         setMethodAccessor( Object.class, methodAccessor );
 49  1
         setMethodAccessor( byte[].class, methodAccessor );
 50  1
         setMethodAccessor( short[].class, methodAccessor );
 51  1
         setMethodAccessor( char[].class, methodAccessor );
 52  1
         setMethodAccessor( int[].class, methodAccessor );
 53  1
         setMethodAccessor( long[].class, methodAccessor );
 54  1
         setMethodAccessor( float[].class, methodAccessor );
 55  1
         setMethodAccessor( double[].class, methodAccessor );
 56  1
         setMethodAccessor( Object[].class, methodAccessor );
 57  
     }
 58  
 
 59  1
     private final ClassCache<PropertyAccessor> propertyAccessors = cacheFactory.createClassCache();
 60  
     {
 61  1
         PropertyAccessor propertyAccessor = new ArrayPropertyAccessor();
 62  1
         setPropertyAccessor( Object.class, new ObjectPropertyAccessor() );
 63  1
         setPropertyAccessor( byte[].class, propertyAccessor );
 64  1
         setPropertyAccessor( short[].class, propertyAccessor );
 65  1
         setPropertyAccessor( char[].class, propertyAccessor );
 66  1
         setPropertyAccessor( int[].class, propertyAccessor );
 67  1
         setPropertyAccessor( long[].class, propertyAccessor );
 68  1
         setPropertyAccessor( float[].class, propertyAccessor );
 69  1
         setPropertyAccessor( double[].class, propertyAccessor );
 70  1
         setPropertyAccessor( Object[].class, propertyAccessor );
 71  1
         setPropertyAccessor( List.class, new ListPropertyAccessor() );
 72  1
         setPropertyAccessor( Map.class, new MapPropertyAccessor() );
 73  1
         setPropertyAccessor( Set.class, new SetPropertyAccessor() );
 74  1
         setPropertyAccessor( Iterator.class, new IteratorPropertyAccessor() );
 75  1
         setPropertyAccessor( Enumeration.class, new EnumerationPropertyAccessor() );
 76  
     }
 77  
 
 78  1
     private final ClassCache<ElementsAccessor> elementsAccessors = cacheFactory.createClassCache();
 79  
     {
 80  1
         ElementsAccessor elementsAccessor = new ArrayElementsAccessor();
 81  1
         setElementsAccessor( Object.class, new ObjectElementsAccessor() );
 82  1
         setElementsAccessor( byte[].class, elementsAccessor );
 83  1
         setElementsAccessor( short[].class, elementsAccessor );
 84  1
         setElementsAccessor( char[].class, elementsAccessor );
 85  1
         setElementsAccessor( int[].class, elementsAccessor );
 86  1
         setElementsAccessor( long[].class, elementsAccessor );
 87  1
         setElementsAccessor( float[].class, elementsAccessor );
 88  1
         setElementsAccessor( double[].class, elementsAccessor );
 89  1
         setElementsAccessor( Object[].class, elementsAccessor );
 90  1
         setElementsAccessor( Collection.class, new CollectionElementsAccessor() );
 91  1
         setElementsAccessor( Map.class, new MapElementsAccessor() );
 92  1
         setElementsAccessor( Iterator.class, new IteratorElementsAccessor() );
 93  1
         setElementsAccessor( Enumeration.class, new EnumerationElementsAccessor() );
 94  1
         setElementsAccessor( Number.class, new NumberElementsAccessor() );
 95  
     }
 96  
 
 97  1
     private final ClassCache<NullHandler> nullHandlers = cacheFactory.createClassCache( );
 98  
     {
 99  1
         NullHandler nullHandler = new ObjectNullHandler();
 100  1
         setNullHandler( Object.class, nullHandler );
 101  1
         setNullHandler( byte[].class, nullHandler );
 102  1
         setNullHandler( short[].class, nullHandler );
 103  1
         setNullHandler( char[].class, nullHandler );
 104  1
         setNullHandler( int[].class, nullHandler );
 105  1
         setNullHandler( long[].class, nullHandler );
 106  1
         setNullHandler( float[].class, nullHandler );
 107  1
         setNullHandler( double[].class, nullHandler );
 108  1
         setNullHandler( Object[].class, nullHandler );
 109  
     }
 110  
 
 111  1
     final ClassCache<Map<String, PropertyDescriptor>> propertyDescriptorCache =
 112  
         cacheFactory.createClassCache( new PropertyDescriptorCacheEntryFactory() );
 113  
 
 114  1
     private final ClassCache<List<Constructor<?>>> constructorCache =
 115  
         cacheFactory.createClassCache( new ClassCacheEntryFactory<List<Constructor<?>>>()
 116  76
         {
 117  
             public List<Constructor<?>> create( Class<?> key )
 118  
                 throws CacheException
 119  
             {
 120  75
                 return Arrays.<Constructor<?>>asList( key.getConstructors() );
 121  
             }
 122  
         } );
 123  
 
 124  1
     private final Cache<DeclaredMethodCacheEntry, Map<String, List<Method>>> _methodCache =
 125  
         cacheFactory.createCache( new DeclaredMethodCacheEntryFactory() );
 126  
 
 127  1
     private final Cache<PermissionCacheEntry, Permission> _invokePermissionCache =
 128  
         cacheFactory.createCache( new PermissionCacheEntryFactory() );
 129  
 
 130  1
     private final ClassCache<Map<String, Field>> _fieldCache =
 131  
         cacheFactory.createClassCache( new FieldCacheEntryFactory() );
 132  
 
 133  1
     private final Cache<Method, Class<?>[]> _methodParameterTypesCache =
 134  
         cacheFactory.createCache( new CacheEntryFactory<Method, Class<?>[]>()
 135  1423
         {
 136  
             public Class<?>[] create( Method key )
 137  
                 throws CacheException
 138  
             {
 139  1422
                 return key.getParameterTypes( );
 140  
             }
 141  
         } );
 142  
 
 143  1
     private final Cache<GenericMethodParameterTypeCacheEntry, Class<?>[]> _genericMethodParameterTypesCache =
 144  
         cacheFactory.createCache( new GenericMethodParameterTypeFactory() );
 145  
 
 146  1
     private final Cache<Constructor<?>, Class<?>[]> _ctorParameterTypesCache =
 147  
         cacheFactory.createCache( new CacheEntryFactory<Constructor<?>, Class<?>[]>()
 148  79
         {
 149  
             public Class<?>[] create( Constructor<?> key ) throws CacheException
 150  
             {
 151  78
                 return key.getParameterTypes( );
 152  
             }
 153  
         } );
 154  
 
 155  1
     private final Cache<Method, MethodAccessEntryValue> _methodAccessCache =
 156  
         cacheFactory.createCache( new MethodAccessCacheEntryFactory( ) );
 157  
 
 158  1
     private final MethodPermCacheEntryFactory methodPermCacheEntryFactory =
 159  
         new MethodPermCacheEntryFactory( System.getSecurityManager() );
 160  
 
 161  1
     private final Cache<Method, Boolean> _methodPermCache = cacheFactory.createCache( methodPermCacheEntryFactory );
 162  
 
 163  
     public Class<?>[] getMethodParameterTypes( Method method ) throws CacheException {
 164  47415116
         return _methodParameterTypesCache.get( method );
 165  
     }
 166  
 
 167  
     public Class<?>[] getParameterTypes( Constructor<?> constructor ) throws CacheException {
 168  2950169
         return _ctorParameterTypesCache.get( constructor );
 169  
     }
 170  
 
 171  
     public List<Constructor<?>> getConstructor( Class<?> clazz ) throws CacheException {
 172  165000078
         return constructorCache.get( clazz );
 173  
     }
 174  
 
 175  
     public Map<String, Field> getField( Class<?> clazz ) throws CacheException {
 176  165000254
         return _fieldCache.get( clazz );
 177  
     }
 178  
 
 179  
     public Map<String, List<Method>> getMethod( DeclaredMethodCacheEntry declaredMethodCacheEntry ) throws CacheException {
 180  141903072
         return _methodCache.get( declaredMethodCacheEntry );
 181  
     }
 182  
 
 183  
     public Map<String, PropertyDescriptor> getPropertyDescriptor( Class<?> clazz ) throws CacheException {
 184  1791
         return propertyDescriptorCache.get( clazz );
 185  
     }
 186  
 
 187  
     public Permission getInvokePermission( PermissionCacheEntry permissionCacheEntry ) throws CacheException {
 188  43100001
         return _invokePermissionCache.get( permissionCacheEntry );
 189  
     }
 190  
 
 191  
     public MethodAccessor getMethodAccessor( Class<?> clazz ) throws OgnlException
 192  
     {
 193  278
         MethodAccessor methodAccessor = ClassCacheHandler.getHandler( clazz, methodAccessors );
 194  278
         if ( methodAccessor != null )
 195  
         {
 196  278
             return methodAccessor;
 197  
         }
 198  0
         throw new OgnlException( "No method accessor for " + clazz );
 199  
     }
 200  
 
 201  
     public void setMethodAccessor( Class<?> clazz, MethodAccessor accessor )
 202  
     {
 203  9
         methodAccessors.put( clazz, accessor );
 204  9
     }
 205  
 
 206  
     public void setPropertyAccessor( Class<?> clazz, PropertyAccessor accessor )
 207  
     {
 208  37
         propertyAccessors.put( clazz, accessor );
 209  37
     }
 210  
 
 211  
     public PropertyAccessor getPropertyAccessor( Class<?> clazz )
 212  
         throws OgnlException
 213  
     {
 214  2406
         PropertyAccessor propertyAccessor = ClassCacheHandler.getHandler( clazz, propertyAccessors );
 215  2406
         if ( propertyAccessor != null )
 216  
         {
 217  2406
             return propertyAccessor;
 218  
         }
 219  0
         throw new OgnlException( "No property accessor for class " + clazz );
 220  
     }
 221  
 
 222  
     /**
 223  
      * Registers the specified {@link ClassCacheInspector} with all class reflection based internal caches. This may
 224  
      * have a significant performance impact so be careful using this in production scenarios.
 225  
      *
 226  
      * @param inspector The inspector instance that will be registered with all internal cache instances.
 227  
      */
 228  
     public void setClassCacheInspector( ClassCacheInspector inspector )
 229  
     {
 230  1
         propertyDescriptorCache.setClassInspector( inspector );
 231  1
         constructorCache.setClassInspector( inspector );
 232  
         //TODO: methodCache and invokePC should allow to use classCacheInsecptor
 233  
 //        _methodCache.setClassInspector( inspector );
 234  
 //        _invokePermissionCache.setClassInspector( inspector );
 235  1
         _fieldCache.setClassInspector( inspector );
 236  1
     }
 237  
 
 238  
     public Class<?>[] getGenericMethodParameterTypes( GenericMethodParameterTypeCacheEntry key )
 239  
         throws CacheException
 240  
     {
 241  59
         return _genericMethodParameterTypesCache.get( key );
 242  
     }
 243  
 
 244  
     public boolean getMethodPerm( Method method ) throws CacheException
 245  
     {
 246  0
         return _methodPermCache.get( method );
 247  
     }
 248  
 
 249  
     public MethodAccessEntryValue getMethodAccess( Method method ) throws CacheException
 250  
     {
 251  1440
         return _methodAccessCache.get( method );
 252  
     }
 253  
 
 254  
     public void clear() {
 255  3
         _methodParameterTypesCache.clear();
 256  3
         _ctorParameterTypesCache.clear();
 257  3
         propertyDescriptorCache.clear();
 258  3
         constructorCache.clear();
 259  3
         _methodCache.clear();
 260  3
         _invokePermissionCache.clear();
 261  3
         _fieldCache.clear();
 262  3
         _methodAccessCache.clear();
 263  3
     }
 264  
 
 265  
     public ElementsAccessor getElementsAccessor( Class<?> clazz ) throws OgnlException
 266  
     {
 267  14
         ElementsAccessor answer = ClassCacheHandler.getHandler( clazz, elementsAccessors );
 268  14
         if ( answer != null )
 269  
         {
 270  14
             return answer;
 271  
         }
 272  0
         throw new OgnlException( "No elements accessor for class " + clazz );
 273  
     }
 274  
 
 275  
     public void setElementsAccessor( Class<?> clazz, ElementsAccessor accessor )
 276  
     {
 277  14
         elementsAccessors.put( clazz, accessor );
 278  14
     }
 279  
 
 280  
     public NullHandler getNullHandler( Class<?> clazz ) throws OgnlException
 281  
     {
 282  100
         NullHandler answer = ClassCacheHandler.getHandler( clazz, nullHandlers );
 283  100
         if ( answer != null )
 284  
         {
 285  100
             return answer;
 286  
         }
 287  0
         throw new OgnlException( "No null handler for class " + clazz );
 288  
     }
 289  
 
 290  
     public void setNullHandler( Class<?> clazz, NullHandler handler )
 291  
     {
 292  13
         nullHandlers.put( clazz, handler );
 293  13
     }
 294  
 
 295  
     public void setSecurityManager( SecurityManager securityManager )
 296  
     {
 297  0
         methodPermCacheEntryFactory.setSecurityManager( securityManager );
 298  0
     }
 299  
 }