-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve trace logging: More compact output of context objects like re…
…source, value map etc. (#53)
- Loading branch information
1 parent
852f0a2
commit dcacef7
Showing
9 changed files
with
225 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/main/java/io/wcm/handler/media/impl/AemObjectsReflectionToStringBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* #%L | ||
* wcm.io | ||
* %% | ||
* Copyright (C) 2024 wcm.io | ||
* %% | ||
* Licensed 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. | ||
* #L% | ||
*/ | ||
package io.wcm.handler.media.impl; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import java.util.stream.Collectors; | ||
|
||
import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | ||
import org.apache.commons.lang3.builder.ToStringStyle; | ||
import org.apache.sling.api.resource.Resource; | ||
import org.apache.sling.api.resource.ValueMap; | ||
|
||
import com.day.cq.wcm.api.Page; | ||
|
||
/** | ||
* Extends ReflectionToStringBuilder to provide custom handling for AEM-related objects | ||
* (Resource, ValueMap, Page) for a more compact log output. | ||
*/ | ||
public class AemObjectsReflectionToStringBuilder extends ReflectionToStringBuilder { | ||
|
||
/** | ||
* @param object Object to output | ||
*/ | ||
public AemObjectsReflectionToStringBuilder(Object object) { | ||
super(object); | ||
} | ||
|
||
/** | ||
* @param object Object to output | ||
* @param style Style | ||
*/ | ||
public AemObjectsReflectionToStringBuilder(Object object, ToStringStyle style) { | ||
super(object, style); | ||
} | ||
|
||
@Override | ||
protected Object getValue(Field field) throws IllegalAccessException { | ||
if (Resource.class.isAssignableFrom(field.getType())) { | ||
Resource resource = (Resource)field.get(this.getObject()); | ||
if (resource != null) { | ||
return resource.getPath(); | ||
} | ||
} | ||
else if (ValueMap.class.isAssignableFrom(field.getType())) { | ||
ValueMap valueMap = (ValueMap)field.get(this.getObject()); | ||
if (valueMap != null) { | ||
return filteredValueMap(valueMap); | ||
} | ||
} | ||
else if (Page.class.isAssignableFrom(field.getType())) { | ||
Page page = (Page)field.get(this.getObject()); | ||
if (page != null) { | ||
return page.getPath(); | ||
} | ||
} | ||
return super.getValue(field); | ||
} | ||
|
||
/** | ||
* Filter value map to exclude jcr:* properties and null values. | ||
* @param props Value map | ||
* @return Filtered value map, sorted by key | ||
*/ | ||
public static Map<String, Object> filteredValueMap(ValueMap props) { | ||
return props.entrySet().stream() | ||
.filter(entry -> !entry.getKey().startsWith("jcr:") && entry.getValue() != null) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (o1, o2) -> o1, TreeMap::new)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/test/java/io/wcm/handler/media/impl/AemObjectsReflectionToStringBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* #%L | ||
* wcm.io | ||
* %% | ||
* Copyright (C) 2024 wcm.io | ||
* %% | ||
* Licensed 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. | ||
* #L% | ||
*/ | ||
package io.wcm.handler.media.impl; | ||
|
||
import static com.day.cq.commons.jcr.JcrConstants.JCR_CREATED; | ||
import static com.day.cq.commons.jcr.JcrConstants.JCR_PRIMARYTYPE; | ||
import static com.day.cq.commons.jcr.JcrConstants.NT_UNSTRUCTURED; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
import java.util.Date; | ||
import java.util.Map; | ||
|
||
import org.apache.commons.lang3.builder.ToStringStyle; | ||
import org.apache.sling.api.resource.Resource; | ||
import org.apache.sling.api.resource.ValueMap; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import com.day.cq.wcm.api.Page; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import io.wcm.handler.media.testcontext.AppAemContext; | ||
import io.wcm.sling.commons.resource.ImmutableValueMap; | ||
import io.wcm.testing.mock.aem.junit5.AemContext; | ||
import io.wcm.testing.mock.aem.junit5.AemContextExtension; | ||
|
||
@ExtendWith(AemContextExtension.class) | ||
class AemObjectsReflectionToStringBuilderTest { | ||
|
||
private final AemContext context = AppAemContext.newAemContext(); | ||
|
||
@SuppressWarnings("null") | ||
private static final ValueMap VALUEMAP_SAMPLE = ImmutableValueMap.of( | ||
"prop1", "value1", | ||
JCR_CREATED, new Date(), | ||
JCR_PRIMARYTYPE, NT_UNSTRUCTURED, | ||
"prop2", 5, | ||
"prop3", null); | ||
|
||
@Test | ||
void testBuild() { | ||
ClassWithFields obj = new ClassWithFields(); | ||
obj.prop1 = "value1"; | ||
obj.resource = context.create().resource("/content/resource1", | ||
"prop2", "value2"); | ||
obj.page = context.create().page("/content/page1"); | ||
obj.props = VALUEMAP_SAMPLE; | ||
|
||
assertEquals("[page=/content/page1,prop1=value1,props={prop1=value1, prop2=5},resource=/content/resource1]", | ||
new AemObjectsReflectionToStringBuilder(obj, ToStringStyle.NO_CLASS_NAME_STYLE).build()); | ||
} | ||
|
||
@Test | ||
void testBuild_NullObjects() { | ||
ClassWithFields obj = new ClassWithFields(); | ||
|
||
assertNotNull(new AemObjectsReflectionToStringBuilder(obj).build()); | ||
} | ||
|
||
@Test | ||
void testFilteredValueMap() { | ||
Map<String, Object> filtered = AemObjectsReflectionToStringBuilder.filteredValueMap(VALUEMAP_SAMPLE); | ||
|
||
assertEquals(Map.of("prop1", "value1", "prop2", 5), filtered); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
@SuppressFBWarnings("URF_UNREAD_FIELD") | ||
private static final class ClassWithFields { | ||
String prop1; | ||
Resource resource; | ||
Page page; | ||
ValueMap props; | ||
} | ||
|
||
} |