Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replaced Sling Query by Resource Visitor #319

Merged
merged 3 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/aem/common.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,5 @@ plugins.withId("com.cognifide.aem.bundle") {
"implementation"("javax.servlet:jsp-api:2.0")
"implementation"("javax.servlet:servlet-api:2.4")
"implementation"("org.slf4j:slf4j-log4j12:1.7.7")
"implementation"("org.apache.sling:org.apache.sling.query:3.0.0")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import static com.day.crx.JcrConstants.NT_UNSTRUCTURED;
import static org.apache.jackrabbit.commons.JcrUtils.getOrCreateByPath;
import static org.apache.jackrabbit.commons.JcrUtils.getOrCreateUniqueByPath;
import static org.apache.sling.query.SlingQuery.$;

import com.cognifide.apm.api.scripts.Script;
import com.cognifide.apm.api.services.ExecutionMode;
Expand All @@ -40,22 +39,22 @@
import com.day.cq.replication.ReplicationAction;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Spliterator;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.AbstractResourceVisitor;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.query.api.SearchStrategy;
import org.apache.sling.api.resource.ValueMap;
import org.jetbrains.annotations.NotNull;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand Down Expand Up @@ -137,11 +136,19 @@ private HistoryEntryWriterBuilder createBuilder(ResourceResolver resolver, Scrip

@Override
public List<Resource> findAllResources(ResourceResolver resourceResolver) {
List<Resource> resources = new ArrayList<>();
AbstractResourceVisitor visitor = new AbstractResourceVisitor() {
@Override
protected void visit(Resource resource) {
ValueMap valueMap = resource.getValueMap();
if (valueMap.containsKey("executionTime") && "entry".equals(valueMap.get("apmHistory", String.class))) {
resources.add(resource);
}
}
};
Resource rootResource = resourceResolver.getResource(HistoryImpl.HISTORY_FOLDER);
Spliterator<Resource> spliterator = $(rootResource).searchStrategy(SearchStrategy.BFS)
.find("[executionTime][apmHistory=entry]")
.spliterator();
return StreamSupport.stream(spliterator, false)
visitor.accept(rootResource);
return resources.stream()
.sorted(this::compareExecutionTime)
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,16 @@
*/
package com.cognifide.apm.core.scripts;

import static org.apache.sling.query.SlingQuery.$;

import com.cognifide.apm.api.scripts.Script;
import com.cognifide.apm.api.services.ScriptFinder;
import com.cognifide.apm.core.Property;
import java.util.ArrayList;
import java.util.List;
import java.util.Spliterator;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.AbstractResourceVisitor;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.query.api.SearchStrategy;
import org.osgi.service.component.annotations.Component;

@Component(
Expand All @@ -50,13 +45,26 @@ public class ScriptFinderImpl implements ScriptFinder {

@Override
public List<Script> findAll(Predicate<Script> filter, ResourceResolver resolver) {
return findScripts(resolver).filter(filter)
.collect(Collectors.toList());
List<Script> scripts = new ArrayList<>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If no of scripts is unknown probably LinkedList will be more accurate

AbstractResourceVisitor visitor = new AbstractResourceVisitor() {
@Override
protected void visit(Resource resource) {
if (ScriptModel.isScript(resource)) {
Script script = resource.adaptTo(ScriptModel.class);
if (filter.test(script)) {
scripts.add(script);
}
}
}
};
Resource rootResource = resolver.getResource(SCRIPT_PATH);
visitor.accept(rootResource);
return scripts;
}

@Override
public List<Script> findAll(ResourceResolver resolver) {
return findScripts(resolver).collect(Collectors.toList());
return findAll(filter -> true, resolver);
}

@Override
Expand All @@ -76,15 +84,6 @@ public Script find(String scriptPath, ResourceResolver resolver) {
return result;
}

private Stream<Script> findScripts(ResourceResolver resolver) {
Resource rootResource = resolver.getResource(SCRIPT_PATH);
Spliterator<Resource> spliterator = $(rootResource).searchStrategy(SearchStrategy.BFS)
.find(ScriptNode.APM_SCRIPT)
.spliterator();
return StreamSupport.stream(spliterator, false)
.map(resource -> resource.adaptTo(ScriptModel.class));
}

private boolean isAbsolute(String path) {
return StringUtils.startsWith(path, "/");
}
Expand Down