Author: mmillson
Date: 2010-11-09 12:07:08 -0500 (Tue, 09 Nov 2010)
New Revision: 1574
Modified:
branches/JBOSSWEB_2_0_0_GA_CP/src/share/classes/javax/el/BeanELResolver.java
branches/JBOSSWEB_2_0_0_GA_CP/src/share/classes/org/apache/el/lang/ExpressionBuilder.java
branches/JBOSSWEB_2_0_0_GA_CP/src/share/classes/org/apache/el/util/ConcurrentCache.java
Log:
Fix Concurrent access to WeakHashMap in ConcurrentCache causing infinite loop and 100% CPU
for [JBPAPP-5342].
Modified: branches/JBOSSWEB_2_0_0_GA_CP/src/share/classes/javax/el/BeanELResolver.java
===================================================================
---
branches/JBOSSWEB_2_0_0_GA_CP/src/share/classes/javax/el/BeanELResolver.java 2010-11-04
17:30:17 UTC (rev 1573)
+++
branches/JBOSSWEB_2_0_0_GA_CP/src/share/classes/javax/el/BeanELResolver.java 2010-11-09
17:07:08 UTC (rev 1574)
@@ -25,6 +25,8 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
@@ -34,11 +36,30 @@
public class BeanELResolver extends ELResolver {
- private final boolean readOnly;
+ private static final int CACHE_SIZE;
+ private static final String CACHE_SIZE_PROP =
+ "org.apache.el.BeanELResolver.CACHE_SIZE";
- private final ConcurrentCache<String, BeanProperties> cache = new
ConcurrentCache<String, BeanProperties>(
- 1000);
+ static {
+ if (System.getSecurityManager() == null) {
+ CACHE_SIZE = Integer.parseInt(
+ System.getProperty(CACHE_SIZE_PROP, "1000"));
+ } else {
+ CACHE_SIZE = AccessController.doPrivileged(
+ new PrivilegedAction<Integer>() {
+ public Integer run() {
+ return Integer.valueOf(
+ System.getProperty(CACHE_SIZE_PROP, "1000"));
+ }
+ }).intValue();
+ }
+ }
+ private final boolean readOnly;
+
+ private final ConcurrentCache<String, BeanProperties> cache =
+ new ConcurrentCache<String, BeanProperties>(CACHE_SIZE);
+
public BeanELResolver() {
this.readOnly = false;
}
@@ -299,34 +320,37 @@
private final static class ConcurrentCache<K,V> {
- private final int size;
- private final Map<K,V> eden;
- private final Map<K,V> longterm;
-
- public ConcurrentCache(int size) {
- this.size = size;
- this.eden = new ConcurrentHashMap<K,V>(size);
- this.longterm = new WeakHashMap<K,V>(size);
- }
-
- public V get(K key) {
- V value = this.eden.get(key);
- if (value == null) {
- value = this.longterm.get(key);
- if (value != null) {
- this.eden.put(key, value);
- }
- }
- return value;
- }
-
- public void put(K key, V value) {
- if (this.eden.size() >= this.size) {
- this.longterm.putAll(this.eden);
- this.eden.clear();
- }
- this.eden.put(key, value);
- }
-
+ private final int size;
+ private final Map<K,V> eden;
+ private final Map<K,V> longterm;
+
+ public ConcurrentCache(int size) {
+ this.size = size;
+ this.eden = new ConcurrentHashMap<K,V>(size);
+ this.longterm = new WeakHashMap<K,V>(size);
+ }
+
+ public V get(K key) {
+ V value = this.eden.get(key);
+ if (value == null) {
+ synchronized (longterm) {
+ value = this.longterm.get(key);
+ }
+ if (value != null) {
+ this.eden.put(key, value);
+ }
+ }
+ return value;
+ }
+
+ public void put(K key, V value) {
+ if (this.eden.size() >= this.size) {
+ synchronized (longterm) {
+ this.longterm.putAll(this.eden);
+ }
+ this.eden.clear();
+ }
+ this.eden.put(key, value);
+ }
}
}
Modified:
branches/JBOSSWEB_2_0_0_GA_CP/src/share/classes/org/apache/el/lang/ExpressionBuilder.java
===================================================================
---
branches/JBOSSWEB_2_0_0_GA_CP/src/share/classes/org/apache/el/lang/ExpressionBuilder.java 2010-11-04
17:30:17 UTC (rev 1573)
+++
branches/JBOSSWEB_2_0_0_GA_CP/src/share/classes/org/apache/el/lang/ExpressionBuilder.java 2010-11-09
17:07:08 UTC (rev 1574)
@@ -19,6 +19,8 @@
import java.io.StringReader;
import java.lang.reflect.Method;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
import javax.el.ELContext;
import javax.el.ELException;
@@ -50,8 +52,28 @@
*/
public final class ExpressionBuilder implements NodeVisitor {
- private static final ConcurrentCache<String, Node> cache = new
ConcurrentCache<String, Node>(5000);
+ private static final int CACHE_SIZE;
+ private static final String CACHE_SIZE_PROP =
+ "org.apache.el.ExpressionBuilder.CACHE_SIZE";
+ static {
+ if (System.getSecurityManager() == null) {
+ CACHE_SIZE = Integer.parseInt(
+ System.getProperty(CACHE_SIZE_PROP, "5000"));
+ } else {
+ CACHE_SIZE = AccessController.doPrivileged(
+ new PrivilegedAction<Integer>() {
+ public Integer run() {
+ return Integer.valueOf(
+ System.getProperty(CACHE_SIZE_PROP, "5000"));
+ }
+ }).intValue();
+ }
+ }
+
+ private static final ConcurrentCache<String, Node> cache =
+ new ConcurrentCache<String, Node>(CACHE_SIZE);
+
private FunctionMapper fnMapper;
private VariableMapper varMapper;
Modified:
branches/JBOSSWEB_2_0_0_GA_CP/src/share/classes/org/apache/el/util/ConcurrentCache.java
===================================================================
---
branches/JBOSSWEB_2_0_0_GA_CP/src/share/classes/org/apache/el/util/ConcurrentCache.java 2010-11-04
17:30:17 UTC (rev 1573)
+++
branches/JBOSSWEB_2_0_0_GA_CP/src/share/classes/org/apache/el/util/ConcurrentCache.java 2010-11-09
17:07:08 UTC (rev 1574)
@@ -37,7 +37,9 @@
public V get(K k) {
V v = this.eden.get(k);
if (v == null) {
- v = this.longterm.get(k);
+ synchronized (longterm) {
+ v = this.longterm.get(k);
+ }
if (v != null) {
this.eden.put(k, v);
}
@@ -47,7 +49,9 @@
public void put(K k, V v) {
if (this.eden.size() >= size) {
- this.longterm.putAll(this.eden);
+ synchronized (longterm) {
+ this.longterm.putAll(this.eden);
+ }
this.eden.clear();
}
this.eden.put(k, v);