Re: [PR] SLING-12279 - Use ResourceResolver#getPropertyMap() for resource/resolver adaptables [sling-org-apache-sling-models-impl]

2024-04-27 Thread via GitHub


sonarcloud[bot] commented on PR #50:
URL: 
https://github.com/apache/sling-org-apache-sling-models-impl/pull/50#issuecomment-2081120262

   ## [![Quality Gate 
Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png
 'Quality Gate 
Passed')](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-models-impl=50)
 **Quality Gate passed**  
   Issues  
   
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png
 '') [0 New 
issues](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-models-impl=50=false=true)
  
   
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png
 '') [0 Accepted 
issues](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-models-impl=50=new_accepted_issues=list)
   
   Measures  
   
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png
 '') [0 Security 
Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-models-impl=50=false=true)
  
   
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png
 '') [87.5% Coverage on New 
Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-models-impl=50=new_coverage=list)
  
   
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png
 '') [0.0% Duplication on New 
Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-models-impl=50=new_duplicated_lines_density=list)
  
 
   [See analysis details on 
SonarCloud](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-models-impl=50)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@sling.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



Re: [PR] SLING-12279 - Use ResourceResolver#getPropertyMap() for resource/resolver adaptables [sling-org-apache-sling-models-impl]

2024-04-27 Thread via GitHub


paul-bjorkstrand commented on code in PR #50:
URL: 
https://github.com/apache/sling-org-apache-sling-models-impl/pull/50#discussion_r1581876101


##
src/test/java/org/apache/sling/models/impl/AdapterCacheHolderTest.java:
##
@@ -0,0 +1,117 @@
+/*
+ * 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.sling.models.impl;
+
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper;
+import org.apache.sling.servlethelpers.MockSlingHttpServletRequest;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.lang.ref.SoftReference;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.WeakHashMap;
+
+@RunWith(MockitoJUnitRunner.class)
+public class AdapterCacheHolderTest {
+
+@Test
+public void testGetMapNonServletRequestAdaptable() {
+Map, SoftReference>> outerMap = spy(new 
HashMap<>());
+Object adaptable1 = new Object();
+Object adaptable2 = new Object();
+
+try (AdapterCacheHolder adapterCacheHolder = new 
AdapterCacheHolder(false, outerMap)) {
+Map, SoftReference> map1 = 
adapterCacheHolder.getCacheMapForAdaptable(adaptable1);
+Map, SoftReference> map2 = 
adapterCacheHolder.getCacheMapForAdaptable(adaptable1);
+Map, SoftReference> map3 = 
adapterCacheHolder.getCacheMapForAdaptable(adaptable2);
+
+assertSame(map1, map2);
+assertNotSame(map1, map3);
+}
+}
+
+@Test
+public void testGetMapServletRequestAdaptable() {
+Map, SoftReference>> outerMap = spy(new 
HashMap<>());
+SlingHttpServletRequest adaptable1 = new 
MockSlingHttpServletRequest(null);
+SlingHttpServletRequest adaptable2 = new 
SlingHttpServletRequestWrapper(adaptable1);
+SlingHttpServletRequest adaptable3 = new 
MockSlingHttpServletRequest(null);
+
+try (AdapterCacheHolder adapterCacheHolder = new 
AdapterCacheHolder(false, outerMap)) {
+Map, SoftReference> map1 = 
adapterCacheHolder.getCacheMapForAdaptable(adaptable1);
+Map, SoftReference> map2 = 
adapterCacheHolder.getCacheMapForAdaptable(adaptable2);
+Map, SoftReference> map3 = 
adapterCacheHolder.getCacheMapForAdaptable(adaptable3);
+
+assertSame(map1, map2);
+assertSame(map1, map3);
+}
+}
+
+@Test
+public void testClose() {
+Object adaptable = new Object();
+Object result = new Object();
+SoftReference ref = new SoftReference<>(result);
+
+Map, SoftReference> innerMap = spy(new HashMap<>());
+innerMap.put(Object.class, ref);
+
+Map, SoftReference>> outerMap = spy(new 
HashMap<>());
+outerMap.put(adaptable, innerMap);
+
+try (AdapterCacheHolder adapterCacheHolder = new 
AdapterCacheHolder(false, outerMap)) {
+// intentionally empty
+}
+
+verify(outerMap).values();
+verify(outerMap).clear();
+verify(innerMap).clear();
+}
+
+@Test
+public void testNewMapSyncMap() {
+// This is a very weak test, but it should be sufficient to at least 
confirm that we are not just using a
+//  WeakHashMap, in case someone changes it without knowing why.
+Map nonSyncMap= AdapterCacheHolder.newMap(false);
+Map syncMap= AdapterCacheHolder.newMap(true);
+
+System.out.println(nonSyncMap.getClass());

Review Comment:
   Indeed they are not. Updated in latest push.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@sling.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



Re: [PR] SLING-12279 - Use ResourceResolver#getPropertyMap() for resource/resolver adaptables [sling-org-apache-sling-models-impl]

2024-04-26 Thread via GitHub


joerghoh commented on code in PR #50:
URL: 
https://github.com/apache/sling-org-apache-sling-models-impl/pull/50#discussion_r1581591547


##
src/test/java/org/apache/sling/models/impl/AdapterCacheHolderTest.java:
##
@@ -0,0 +1,117 @@
+/*
+ * 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.sling.models.impl;
+
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper;
+import org.apache.sling.servlethelpers.MockSlingHttpServletRequest;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.lang.ref.SoftReference;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.WeakHashMap;
+
+@RunWith(MockitoJUnitRunner.class)
+public class AdapterCacheHolderTest {
+
+@Test
+public void testGetMapNonServletRequestAdaptable() {
+Map, SoftReference>> outerMap = spy(new 
HashMap<>());
+Object adaptable1 = new Object();
+Object adaptable2 = new Object();
+
+try (AdapterCacheHolder adapterCacheHolder = new 
AdapterCacheHolder(false, outerMap)) {
+Map, SoftReference> map1 = 
adapterCacheHolder.getCacheMapForAdaptable(adaptable1);
+Map, SoftReference> map2 = 
adapterCacheHolder.getCacheMapForAdaptable(adaptable1);
+Map, SoftReference> map3 = 
adapterCacheHolder.getCacheMapForAdaptable(adaptable2);
+
+assertSame(map1, map2);
+assertNotSame(map1, map3);
+}
+}
+
+@Test
+public void testGetMapServletRequestAdaptable() {
+Map, SoftReference>> outerMap = spy(new 
HashMap<>());
+SlingHttpServletRequest adaptable1 = new 
MockSlingHttpServletRequest(null);
+SlingHttpServletRequest adaptable2 = new 
SlingHttpServletRequestWrapper(adaptable1);
+SlingHttpServletRequest adaptable3 = new 
MockSlingHttpServletRequest(null);
+
+try (AdapterCacheHolder adapterCacheHolder = new 
AdapterCacheHolder(false, outerMap)) {
+Map, SoftReference> map1 = 
adapterCacheHolder.getCacheMapForAdaptable(adaptable1);
+Map, SoftReference> map2 = 
adapterCacheHolder.getCacheMapForAdaptable(adaptable2);
+Map, SoftReference> map3 = 
adapterCacheHolder.getCacheMapForAdaptable(adaptable3);
+
+assertSame(map1, map2);
+assertSame(map1, map3);
+}
+}
+
+@Test
+public void testClose() {
+Object adaptable = new Object();
+Object result = new Object();
+SoftReference ref = new SoftReference<>(result);
+
+Map, SoftReference> innerMap = spy(new HashMap<>());
+innerMap.put(Object.class, ref);
+
+Map, SoftReference>> outerMap = spy(new 
HashMap<>());
+outerMap.put(adaptable, innerMap);
+
+try (AdapterCacheHolder adapterCacheHolder = new 
AdapterCacheHolder(false, outerMap)) {
+// intentionally empty
+}
+
+verify(outerMap).values();
+verify(outerMap).clear();
+verify(innerMap).clear();
+}
+
+@Test
+public void testNewMapSyncMap() {
+// This is a very weak test, but it should be sufficient to at least 
confirm that we are not just using a
+//  WeakHashMap, in case someone changes it without knowing why.
+Map nonSyncMap= AdapterCacheHolder.newMap(false);
+Map syncMap= AdapterCacheHolder.newMap(true);
+
+System.out.println(nonSyncMap.getClass());

Review Comment:
   is this required here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@sling.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



Re: [PR] SLING-12279 - Use ResourceResolver#getPropertyMap() for resource/resolver adaptables [sling-org-apache-sling-models-impl]

2024-03-30 Thread via GitHub


sonarcloud[bot] commented on PR #50:
URL: 
https://github.com/apache/sling-org-apache-sling-models-impl/pull/50#issuecomment-2028474177

   ## [![Quality Gate 
Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png
 'Quality Gate 
Passed')](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-models-impl=50)
 **Quality Gate passed**  
   Issues  
   
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png
 '') [0 New 
issues](https://sonarcloud.io/project/issues?id=apache_sling-org-apache-sling-models-impl=50=false=true)
  
   
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png
 '') [0 Accepted 
issues](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-models-impl=50=new_accepted_issues=list)
   
   Measures  
   
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png
 '') [0 Security 
Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_sling-org-apache-sling-models-impl=50=false=true)
  
   
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png
 '') [87.5% Coverage on New 
Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-models-impl=50=new_coverage=list)
  
   
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png
 '') [0.0% Duplication on New 
Code](https://sonarcloud.io/component_measures?id=apache_sling-org-apache-sling-models-impl=50=new_duplicated_lines_density=list)
  
 
   [See analysis details on 
SonarCloud](https://sonarcloud.io/dashboard?id=apache_sling-org-apache-sling-models-impl=50)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@sling.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



[PR] SLING-12279 - Use ResourceResolver#getPropertyMap() for resource/resolver adaptables [sling-org-apache-sling-models-impl]

2024-03-30 Thread via GitHub


paul-bjorkstrand opened a new pull request, #50:
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/50

   - Update Sling API dependency to 2.24.0 (to be able to use 
`ResourceResolver#getPropertyMap()`).
   - Create a common cache holder object for all types of caches.
   - When the adaptable is a request, continue to keep only a single cache 
mapping adapter types to adaptation results.
   - When the adaptable is a resource or resource resolver, store the cache 
holder in the appropriate resource resolver's property map (this map has the 
same lifecycle as the resource resolver).
   - When the adaptable is anything else, use the "global cache".
   - When the cache holder is not global, remove the use of synchronized maps 
(moderate performance improvement since resources/resolvers should only be used 
single threaded).
   - Ensure the Request cache holder's `close()` is called when the request is 
disposed.
   - Update tests where needed.
   - Add tests for new code.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@sling.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org