Author: nbubna
Date: Tue Aug 12 16:59:06 2008
New Revision: 685385
URL: http://svn.apache.org/viewvc?rev=685385&view=rev
Log:
use MapFactory to take advantage of JDK 1.5+ concurrency utils when available
(thx to Jarkko Viinamaki in patch for VELOCITY-607)
Added:
velocity/engine/trunk/src/java/org/apache/velocity/util/MapFactory.java
(with props)
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/ResourceCacheImpl.java
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/ResourceCacheImpl.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/ResourceCacheImpl.java?rev=685385&r1=685384&r2=685385&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/ResourceCacheImpl.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/ResourceCacheImpl.java
Tue Aug 12 16:59:06 2008
@@ -20,6 +20,7 @@
*/
import java.util.Collections;
+import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Iterator;
@@ -27,6 +28,7 @@
import org.apache.commons.collections.map.LRUMap;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.RuntimeServices;
+import org.apache.velocity.util.MapFactory;
/**
* Default implementation of the resource cache for the default
@@ -47,7 +49,7 @@
/**
* Cache storage, assumed to be thread-safe.
*/
- protected Map cache = new Hashtable();
+ protected Map cache = MapFactory.create(512, 0.5f, 30, false);
/**
* Runtime services, generally initialized by the
@@ -72,7 +74,8 @@
lruCache.putAll(cache);
cache = lruCache;
}
- rsvc.getLog().debug("ResourceCache: initialized
("+this.getClass()+')');
+ rsvc.getLog().debug("ResourceCache: initialized ("+this.getClass()+")
with "+
+ cache.getClass()+" cache map.");
}
/**
@@ -107,3 +110,4 @@
return cache.keySet().iterator();
}
}
+
Added: velocity/engine/trunk/src/java/org/apache/velocity/util/MapFactory.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/util/MapFactory.java?rev=685385&view=auto
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/util/MapFactory.java
(added)
+++ velocity/engine/trunk/src/java/org/apache/velocity/util/MapFactory.java Tue
Aug 12 16:59:06 2008
@@ -0,0 +1,113 @@
+package org.apache.velocity.util;
+
+/*
+ * 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.
+ */
+
+import java.lang.reflect.Constructor;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+/**
+ * Factory class for creating Maps.
+ *
+ * The main purpose of this class is to take advantage of Java 5
+ * Concurrent classes if they are available. We use reflection to instantiate
+ * java.util.concurrent classes to avoid compile time dependency on Java 5.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Jarkko Viinamaki</a>
+ * @see http://issues.apache.org/jira/browse/VELOCITY-607
+ */
+public class MapFactory
+{
+ private static Constructor concurrentHashMapConstructor;
+ static
+ {
+ try
+ {
+ concurrentHashMapConstructor =
+ Class.forName("java.util.concurrent.ConcurrentHashMap")
+ .getConstructor(new Class[] { int.class, float.class,
int.class } );
+ }
+ catch (Exception ex)
+ {
+ // not running under JRE 1.5+
+ }
+ }
+
+ /**
+ * Creates a new instance of a class that implements Map interface.
+ *
+ * Note that there is a small performance penalty because concurrent
+ * maps are created using reflection.
+ *
+ * @param size initial size of the map
+ * @param loadFactor smaller value = better performance,
+ * larger value = better memory utilization
+ * @param concurrencyLevel estimated number of writer Threads.
+ * If this is smaller than 1, HashMap is always returned which is
not
+ * threadsafe.
+ * @param allowNullKeys if true, the returned Map instance supports null
keys
+ *
+ * @return one of ConcurrentHashMap, HashMap, Hashtable
+ */
+ public static Map create(int size, float loadFactor,
+ int concurrencyLevel, boolean allowNullKeys)
+ {
+ Map map = null;
+ if (concurrencyLevel <= 1)
+ {
+ map = new HashMap(size, loadFactor);
+ }
+ else
+ {
+ if (concurrentHashMapConstructor != null)
+ {
+ // running under JRE 1.5+
+ try
+ {
+ map = (Map)concurrentHashMapConstructor.newInstance(
+ new Object[] { new Integer(size), new
Float(loadFactor), new Integer(concurrencyLevel) });
+ }
+ catch (Exception ex)
+ {
+ throw new IllegalStateException("this should not happen",
ex);
+ }
+ }
+ if (map == null)
+ {
+ /*
+ * Hashtable should be faster than
+ * Collections.synchronizedMap(new HashMap());
+ * so favor it if there is no need for null key support
+ */
+ if (allowNullKeys)
+ {
+ map = Collections.synchronizedMap(new HashMap(size,
loadFactor));
+ }
+ else
+ {
+ map = new Hashtable(size, loadFactor);
+ }
+ }
+ }
+ return map;
+ }
+}
Propchange:
velocity/engine/trunk/src/java/org/apache/velocity/util/MapFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
velocity/engine/trunk/src/java/org/apache/velocity/util/MapFactory.java
------------------------------------------------------------------------------
svn:executable = *
Propchange:
velocity/engine/trunk/src/java/org/apache/velocity/util/MapFactory.java
------------------------------------------------------------------------------
svn:keywords = Revision
Propchange:
velocity/engine/trunk/src/java/org/apache/velocity/util/MapFactory.java
------------------------------------------------------------------------------
svn:mime-type = text/plain