You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mc...@apache.org on 2011/10/17 00:52:50 UTC
svn commit: r1184952 - in
/commons/proper/ognl/branches/new-cache-approach/src:
main/java/org/apache/commons/ognl/
main/java/org/apache/commons/ognl/internal/entry/
test/java/org/apache/commons/ognl/internal/
Author: mcucchiara
Date: Sun Oct 16 22:52:50 2011
New Revision: 1184952
URL: http://svn.apache.org/viewvc?rev=1184952&view=rev
Log:
New MethodPermCache implementation
Added:
commons/proper/ognl/branches/new-cache-approach/src/main/java/org/apache/commons/ognl/internal/entry/MethodPermCacheEntryFactory.java
commons/proper/ognl/branches/new-cache-approach/src/test/java/org/apache/commons/ognl/internal/MethodPermCacheTest.java
Modified:
commons/proper/ognl/branches/new-cache-approach/src/main/java/org/apache/commons/ognl/OgnlRuntime.java
Modified: commons/proper/ognl/branches/new-cache-approach/src/main/java/org/apache/commons/ognl/OgnlRuntime.java
URL: http://svn.apache.org/viewvc/commons/proper/ognl/branches/new-cache-approach/src/main/java/org/apache/commons/ognl/OgnlRuntime.java?rev=1184952&r1=1184951&r2=1184952&view=diff
==============================================================================
--- commons/proper/ognl/branches/new-cache-approach/src/main/java/org/apache/commons/ognl/OgnlRuntime.java (original)
+++ commons/proper/ognl/branches/new-cache-approach/src/main/java/org/apache/commons/ognl/OgnlRuntime.java Sun Oct 16 22:52:50 2011
@@ -35,6 +35,7 @@ import org.apache.commons.ognl.internal.
import org.apache.commons.ognl.internal.entry.FiedlCacheEntryFactory;
import org.apache.commons.ognl.internal.entry.GenericMethodParameterTypeCacheEntry;
import org.apache.commons.ognl.internal.entry.GenericMethodParameterTypeFactory;
+import org.apache.commons.ognl.internal.entry.MethodPermCacheEntryFactory;
import org.apache.commons.ognl.internal.entry.PermissionCacheEntry;
import org.apache.commons.ognl.internal.entry.PermissionCacheEntryFactory;
import org.apache.commons.ognl.internal.entry.PropertyDescriptorCacheEntryFactory;
@@ -216,7 +217,8 @@ public class OgnlRuntime
static final IntHashMap<Integer, Boolean> _methodAccessCache = new IntHashMap<Integer, Boolean>( );
- static final IntHashMap<Integer, Boolean> _methodPermCache = new IntHashMap<Integer, Boolean>( );
+ static final Cache<Method, Boolean> _methodPermCache =
+ new ConcurrentHashMapCache<Method, Boolean>( new MethodPermCacheEntryFactory( _securityManager ) );
static ClassCacheInspector _cacheInspector;
@@ -757,7 +759,6 @@ public class OgnlRuntime
throws InvocationTargetException, IllegalAccessException, CacheException
{
boolean syncInvoke = false;
- boolean checkPermission = false;
int mHash = method.hashCode( );
// only synchronize method invocation if it actually requires it
@@ -768,12 +769,6 @@ public class OgnlRuntime
{
syncInvoke = true;
}
-
- if ( _securityManager != null && _methodPermCache.get( mHash ) == null
- || _methodPermCache.get( mHash ) == Boolean.FALSE )
- {
- checkPermission = true;
- }
}
Object result;
@@ -783,18 +778,13 @@ public class OgnlRuntime
{
synchronized ( method )
{
- if ( checkPermission )
+ if ( _securityManager != null )
{
- try
- {
- _securityManager.checkPermission( getPermission( method ) );
- _methodPermCache.put( mHash, Boolean.TRUE );
- }
- catch ( SecurityException ex )
+ if ( !_methodPermCache.get( method ) )
{
- _methodPermCache.put( mHash, Boolean.FALSE );
throw new IllegalAccessException( "Method [" + method + "] cannot be accessed." );
}
+
}
if ( !Modifier.isPublic( method.getModifiers( ) ) || !Modifier.isPublic(
@@ -825,16 +815,10 @@ public class OgnlRuntime
}
else
{
- if ( checkPermission )
+ if ( _securityManager!=null )
{
- try
- {
- _securityManager.checkPermission( getPermission( method ) );
- _methodPermCache.put( mHash, Boolean.TRUE );
- }
- catch ( SecurityException ex )
+ if ( !_methodPermCache.get( method ) )
{
- _methodPermCache.put( mHash, Boolean.FALSE );
throw new IllegalAccessException( "Method [" + method + "] cannot be accessed." );
}
}
Added: commons/proper/ognl/branches/new-cache-approach/src/main/java/org/apache/commons/ognl/internal/entry/MethodPermCacheEntryFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/ognl/branches/new-cache-approach/src/main/java/org/apache/commons/ognl/internal/entry/MethodPermCacheEntryFactory.java?rev=1184952&view=auto
==============================================================================
--- commons/proper/ognl/branches/new-cache-approach/src/main/java/org/apache/commons/ognl/internal/entry/MethodPermCacheEntryFactory.java (added)
+++ commons/proper/ognl/branches/new-cache-approach/src/main/java/org/apache/commons/ognl/internal/entry/MethodPermCacheEntryFactory.java Sun Oct 16 22:52:50 2011
@@ -0,0 +1,37 @@
+package org.apache.commons.ognl.internal.entry;
+
+import org.apache.commons.ognl.OgnlRuntime;
+import org.apache.commons.ognl.internal.CacheException;
+
+import java.lang.reflect.Method;
+
+/**
+* User: Maurizio Cucchiara
+* Date: 10/17/11
+* Time: 12:46 AM
+*/
+public class MethodPermCacheEntryFactory
+ implements CacheEntryFactory<Method, Boolean>
+{
+ private SecurityManager securityManager;
+
+ public MethodPermCacheEntryFactory( SecurityManager securityManager )
+ {
+ this.securityManager = securityManager;
+ }
+
+ public Boolean create( Method key )
+ throws CacheException
+ {
+ try
+ {
+ securityManager.checkPermission( OgnlRuntime.getPermission( key ) );
+ return true;
+ }
+ catch ( SecurityException ex )
+ {
+ return false;
+ }
+
+ }
+}
Added: commons/proper/ognl/branches/new-cache-approach/src/test/java/org/apache/commons/ognl/internal/MethodPermCacheTest.java
URL: http://svn.apache.org/viewvc/commons/proper/ognl/branches/new-cache-approach/src/test/java/org/apache/commons/ognl/internal/MethodPermCacheTest.java?rev=1184952&view=auto
==============================================================================
--- commons/proper/ognl/branches/new-cache-approach/src/test/java/org/apache/commons/ognl/internal/MethodPermCacheTest.java (added)
+++ commons/proper/ognl/branches/new-cache-approach/src/test/java/org/apache/commons/ognl/internal/MethodPermCacheTest.java Sun Oct 16 22:52:50 2011
@@ -0,0 +1,38 @@
+package org.apache.commons.ognl.internal;
+
+import org.apache.commons.ognl.internal.entry.MethodPermCacheEntryFactory;
+import org.apache.commons.ognl.test.objects.Root;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.lang.reflect.Method;
+import java.security.Permission;
+
+/**
+ * User: Maurizio Cucchiara
+ * Date: 10/17/11
+ * Time: 12:13 AM
+ */
+public class MethodPermCacheTest
+{
+ private SecurityManager securityManager =new DummySecurityManager();
+
+ Cache<Method, Boolean> cache =
+ new ConcurrentHashMapCache<Method, Boolean>( new MethodPermCacheEntryFactory( securityManager ) );
+ @Test
+ public void testGetPublicMethod( )
+ throws CacheException, NoSuchMethodException
+ {
+ Method method = Root.class.getMethod( "getArray");
+ Assert.assertTrue( cache.get( method ) );
+ }
+
+ private class DummySecurityManager
+ extends SecurityManager
+ {
+ @Override
+ public void checkPermission( Permission perm )
+ {
+ }
+ }
+}