Repository: cxf Updated Branches: refs/heads/3.1.x-fixes 810063b93 -> e4cab6471
[CXF-6826] Cache MediaTypeHeaderProvider.valueOf results (with automatic cache cleanup when reaching a threshold) Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/8a9f6cda Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/8a9f6cda Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/8a9f6cda Branch: refs/heads/3.1.x-fixes Commit: 8a9f6cdafc87a22314c805e995fc5d286fe8beed Parents: 810063b Author: Alessio Soldano <[email protected]> Authored: Thu Mar 10 20:39:49 2016 +0100 Committer: Alessio Soldano <[email protected]> Committed: Fri Mar 11 10:38:08 2016 +0100 ---------------------------------------------------------------------- .../cxf/jaxrs/impl/MediaTypeHeaderProvider.java | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/8a9f6cda/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java index 39f484e..5d297b6 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java @@ -25,6 +25,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.StringTokenizer; +import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -45,17 +46,34 @@ public class MediaTypeHeaderProvider implements HeaderDelegate<MediaType> { private static final Pattern COMPLEX_PARAMETERS = Pattern.compile("(([\\w-]+=\"[^\"]*\")|([\\w-]+=[\\w-/\\+]+))"); + private static Map<String, MediaType> map = new ConcurrentHashMap<String, MediaType>(); + private static final int MAX_MT_CACHE_SIZE = + Integer.getInteger("org.apache.cxf.jaxrs.max_mediatype_cache_size", 200); + public MediaType fromString(String mType) { return valueOf(mType); } public static MediaType valueOf(String mType) { - if (mType == null) { throw new IllegalArgumentException("Media type value can not be null"); } + MediaType result = map.get(mType); + if (result == null) { + result = internalValueOf(mType); + final int size = map.size(); + if (size >= MAX_MT_CACHE_SIZE) { + map.clear(); + } + map.put(mType, result); + } + return result; + } + + public static MediaType internalValueOf(String mType) { + int i = mType.indexOf('/'); if (i == -1) { return handleMediaTypeWithoutSubtype(mType.trim());
