cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From egl...@apache.org
Subject svn commit: r540134 - in /incubator/cxf/trunk: common/common/src/main/java/org/apache/cxf/common/util/TwoStageMap.java rt/core/src/main/java/org/apache/cxf/wsdl11/WSDLManagerImpl.java
Date Mon, 21 May 2007 12:34:04 GMT
Author: eglynn
Date: Mon May 21 05:34:02 2007
New Revision: 540134

URL: http://svn.apache.org/viewvc?view=rev&rev=540134
Log:
Changed WSDLManagerImpl to use a TwoStageMap to avoid over-zealous GC prematurely clearing
out the Definitions cache.

Added:
    incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/TwoStageMap.java
  (with props)
Modified:
    incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/WSDLManagerImpl.java

Added: incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/TwoStageMap.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/TwoStageMap.java?view=auto&rev=540134
==============================================================================
--- incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/TwoStageMap.java
(added)
+++ incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/TwoStageMap.java
Mon May 21 05:34:02 2007
@@ -0,0 +1,133 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.common.util;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.WeakHashMap;
+
+import org.apache.cxf.helpers.CastUtils;
+
+/**
+ * A Map backed by a limted capacity strong HashMap ("eden") containing
+ * the youngest entries, overflowing into an unlimted WeakMapHash ("aged")
+ * containing the older entries.
+ * Used to avoid caching schemes being frustrated by over-agressive GC.
+ * 
+ * @param <K> key type
+ * @param <V> value type
+ */
+public class TwoStageMap<K, V> implements Map<K, V> {
+
+    private static final int DEFAULT_EDEN_CAPACITY = 50;
+
+    private final int edenCapacity;
+    private Map<K, V> eden;
+    private List<K> edenKeys;
+    private Map<K, V> aged;
+    
+    public TwoStageMap() {
+        this(DEFAULT_EDEN_CAPACITY);
+    }
+    
+    public TwoStageMap(int c) {
+        edenCapacity = c;        
+        eden = new HashMap<K, V>(edenCapacity);
+        edenKeys = new ArrayList<K>(edenCapacity);
+        aged = new WeakHashMap<K, V>();
+    }
+    
+    public synchronized int size() {
+        return eden.size() + aged.size();
+    }
+
+    public synchronized boolean isEmpty() {
+        return eden.isEmpty() && aged.isEmpty();
+    }
+
+    public synchronized boolean containsKey(Object key) {
+        return eden.containsKey(key) || aged.containsKey(key);
+    }
+
+    public synchronized boolean containsValue(Object value) {
+        return eden.containsValue(value) || aged.containsValue(value);
+    }
+
+    public synchronized V get(Object key) {
+        V edenValue = eden.get(key);
+        return edenValue != null ? edenValue : aged.get(key);
+    }
+
+    public synchronized V put(K key, V value) {
+        if (eden.size() >= edenCapacity) {
+            K victimKey = edenKeys.remove(0);
+            aged.put(victimKey, eden.remove(victimKey));            
+        }
+        edenKeys.add(key);
+        return eden.put(key, value);
+    }
+
+    public synchronized V remove(Object key) {
+        V victim = null;
+        if (eden.containsKey(key)) {
+            edenKeys.remove(key);
+            victim = eden.remove(key);
+        } else {
+            victim = aged.remove(key);
+        }
+        return victim;
+    }
+
+    public synchronized void putAll(Map<? extends K, ? extends V> t) {
+        Set<Entry<? extends K, ? extends V>> entries =
+            CastUtils.cast(t.entrySet());
+        for (Entry<? extends K, ? extends V> entry : entries) {
+            put(entry.getKey(), entry.getValue());
+        }
+    }
+
+    public synchronized void clear() {
+        eden.clear();
+        edenKeys.clear();
+        aged.clear();
+    }
+
+    public synchronized Set<K> keySet() {
+        Set<K> keys = eden.keySet();
+        keys.addAll(aged.keySet());
+        return keys;
+    }
+
+    public synchronized Collection<V> values() {
+        Collection<V> values = eden.values();
+        values.addAll(aged.values());
+        return values;
+    }
+
+    public synchronized Set<Entry<K, V>> entrySet() {
+        Set<Entry<K, V>> entries = eden.entrySet();
+        entries.addAll(aged.entrySet());
+        return entries;
+    }
+}

Propchange: incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/TwoStageMap.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/WSDLManagerImpl.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/WSDLManagerImpl.java?view=diff&rev=540134&r1=540133&r2=540134
==============================================================================
--- incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/WSDLManagerImpl.java (original)
+++ incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/wsdl11/WSDLManagerImpl.java Mon
May 21 05:34:02 2007
@@ -26,7 +26,6 @@
 import java.util.Map;
 import java.util.Properties;
 import java.util.StringTokenizer;
-import java.util.WeakHashMap;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -48,6 +47,7 @@
 import org.apache.cxf.catalog.OASISCatalogManager;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.PropertiesLoaderUtils;
+import org.apache.cxf.common.util.TwoStageMap;
 import org.apache.cxf.wsdl.JAXBExtensionHelper;
 import org.apache.cxf.wsdl.WSDLConstants;
 import org.apache.cxf.wsdl.WSDLManager;
@@ -65,7 +65,7 @@
 
     final ExtensionRegistry registry;
     final WSDLFactory factory;
-    final WeakHashMap<Object, Definition> definitionsMap;
+    final TwoStageMap<Object, Definition> definitionsMap;
     private Bus bus;
 
     public WSDLManagerImpl() throws BusException {
@@ -78,7 +78,7 @@
         } catch (WSDLException e) {
             throw new BusException(e);
         }
-        definitionsMap = new WeakHashMap<Object, Definition>();
+        definitionsMap = new TwoStageMap<Object, Definition>();
 
         registerInitialExtensions();
     }



Mime
View raw message