This is an automated email from the ASF dual-hosted git repository.

sergeyb pushed a commit to branch 3.1.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/3.1.x-fixes by this push:
     new b1de61a  Support for caching UriTemplates, patch from bcluap applied, 
This closes #310
b1de61a is described below

commit b1de61a126688db3842bd6ea743141ff70753996
Author: Sergey Beryozkin <[email protected]>
AuthorDate: Mon Sep 18 16:19:48 2017 +0100

    Support for caching UriTemplates, patch from bcluap applied, This closes 
#310
---
 .../org/apache/cxf/jaxrs/impl/UriBuilderImpl.java  | 16 ++++++-------
 .../org/apache/cxf/jaxrs/model/URITemplate.java    | 28 +++++++++++++++-------
 2 files changed, 28 insertions(+), 16 deletions(-)

diff --git 
a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java 
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
index 0ae1540..62e39b8 100644
--- 
a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
+++ 
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
@@ -117,7 +117,7 @@ public class UriBuilderImpl extends UriBuilder implements 
Cloneable {
             + alreadyResolvedTsPathEnc.size();
         
         String thePath = buildPath();
-        URITemplate pathTempl = new URITemplate(thePath);
+        URITemplate pathTempl = URITemplate.createExactTemplate(thePath);
         thePath = substituteVarargs(pathTempl, alreadyResolvedTs, 
alreadyResolvedTsPathEnc, 
                                     alreadyResolvedEncTs, values, 0, false, 
fromEncoded, 
                                     allowUnresolved, encodePathSlash);
@@ -126,7 +126,7 @@ public class UriBuilderImpl extends UriBuilder implements 
Cloneable {
         String theQuery = buildQuery();
         int queryTemplateVarsSize = 0;
         if (theQuery != null) {
-            URITemplate queryTempl = new URITemplate(theQuery);
+            URITemplate queryTempl = URITemplate.createExactTemplate(theQuery);
             queryTemplateVarsSize = queryTempl.getVariables().size();
             if (queryTemplateVarsSize > 0) {
                 int lengthDiff = values.length + resolvedTsSize 
@@ -140,7 +140,7 @@ public class UriBuilderImpl extends UriBuilder implements 
Cloneable {
         
         String theFragment = fragment;
         if (theFragment != null) {
-            URITemplate fragmentTempl = new URITemplate(theFragment);
+            URITemplate fragmentTempl = 
URITemplate.createExactTemplate(theFragment);
             if (fragmentTempl.getVariables().size() > 0) {
                 int lengthDiff = values.length  + resolvedTsSize 
                     - alreadyResolvedTs.size() - 
alreadyResolvedTsPathEnc.size() - alreadyResolvedEncTs.size()
@@ -338,7 +338,7 @@ public class UriBuilderImpl extends UriBuilder implements 
Cloneable {
                                     boolean fromEncoded,
                                     boolean encodePathSlash) {
     //CHECKSTYLE:ON
-        URITemplate templ = new URITemplate(path);
+        URITemplate templ = URITemplate.createExactTemplate(path);
         
         Set<String> uniqueVars = new HashSet<String>(templ.getVariables());
         if (varValueMap.size() + alreadyResolvedTs.size() + 
alreadyResolvedTsEnc.size()
@@ -522,7 +522,7 @@ public class UriBuilderImpl extends UriBuilder implements 
Cloneable {
                 this.originalPathEmpty = StringUtils.isEmpty(uri.getPath());
                 uri(uri);
             } catch (IllegalArgumentException ex) {
-                if (!new URITemplate(path).getVariables().isEmpty()) {
+                if 
(!URITemplate.createExactTemplate(path).getVariables().isEmpty()) {
                     return uriAsTemplate(path);
                 }
                 String pathEncoded = HttpUtils.pathEncode(path);
@@ -662,7 +662,7 @@ public class UriBuilderImpl extends UriBuilder implements 
Cloneable {
             PathSegment ps = iter.next();
             String p = ps.getPath();
             if (p.length() != 0 || !iter.hasNext()) {
-                p = new URITemplate(p).encodeLiteralCharacters(false);
+                p = 
URITemplate.createExactTemplate(p).encodeLiteralCharacters(false);
                 if (sb.length() == 0 && leadingSlash) {
                     sb.append('/');
                 } else if (!p.startsWith("/") && sb.length() > 0) {
@@ -848,7 +848,7 @@ public class UriBuilderImpl extends UriBuilder implements 
Cloneable {
                         val = val.replaceAll("/", "%2F");
                     }
                 } else {
-                    val = new 
URITemplate(val).encodeLiteralCharacters(isQuery);
+                    val = 
URITemplate.createExactTemplate(val).encodeLiteralCharacters(isQuery);
                 }
                 b.append(entry.getKey());
                 if (val.length() != 0) {
@@ -890,7 +890,7 @@ public class UriBuilderImpl extends UriBuilder implements 
Cloneable {
         try {
             return uri(URI.create(uriTemplate));
         } catch (Exception ex) {
-            if (new URITemplate(uriTemplate).getVariables().isEmpty()) {
+            if 
(URITemplate.createExactTemplate(uriTemplate).getVariables().isEmpty()) {
                 throw new IllegalArgumentException(ex);    
             } else {
                 return uriAsTemplate(uriTemplate);
diff --git 
a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java 
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java
index 3c6afd3..1db14bd 100644
--- 
a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java
+++ 
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java
@@ -25,6 +25,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -44,6 +45,9 @@ public final class URITemplate {
     private static final String CHARACTERS_TO_ESCAPE = ".*+$()";
     private static final String SLASH = "/";
     private static final String SLASH_QUOTE = "/;";
+    private static final int MAX_URI_TEMPLATE_CACHE_SIZE = 
+        Integer.getInteger("org.apache.cxf.jaxrs.max_uri_template_cache_size", 
2000);
+    private static final Map<String, URITemplate> URI_TEMPLATE_CACHE = new 
ConcurrentHashMap<String, URITemplate>();
     
     private final String template;
     private final List<String> variables = new ArrayList<String>();
@@ -327,18 +331,26 @@ public final class URITemplate {
     }
     
     public static URITemplate createTemplate(String pathValue) {
-
         if (pathValue == null) {
-            return new URITemplate("/");
-        }
-
-        if (!pathValue.startsWith("/")) {
+            pathValue = "/";
+        } else if (!pathValue.startsWith("/")) {
             pathValue = "/" + pathValue;
         }
-
-        return new URITemplate(pathValue);
+        return createExactTemplate(pathValue);
     }
-
+    
+    public static URITemplate createExactTemplate(String pathValue) {
+        URITemplate template = URI_TEMPLATE_CACHE.get(pathValue);
+        if (template == null) {
+            template = new URITemplate(pathValue);
+            if (URI_TEMPLATE_CACHE.size() >= MAX_URI_TEMPLATE_CACHE_SIZE) {
+                URI_TEMPLATE_CACHE.clear();
+            }
+            URI_TEMPLATE_CACHE.put(pathValue, template);
+        }
+        return template;
+    }
+    
     public static int compareTemplates(URITemplate t1, URITemplate t2) {
         String l1 = t1.getLiteralChars();
         String l2 = t2.getLiteralChars();

-- 
To stop receiving notification emails like this one, please contact
['"[email protected]" <[email protected]>'].

Reply via email to