Skip to content

Commit

Permalink
feat(runner): integrate and extend with OSGi
Browse files Browse the repository at this point in the history
  • Loading branch information
sabberworm committed Oct 2, 2024
1 parent bd44634 commit a2c9f97
Show file tree
Hide file tree
Showing 20 changed files with 204 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.swisscom.aem.tools.impl;

import java.util.List;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.FieldOption;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.component.annotations.ReferencePolicyOption;

import com.swisscom.aem.tools.jcrhopper.Hop;
import com.swisscom.aem.tools.jcrhopper.RunnerBuilder;
import com.swisscom.aem.tools.jcrhopper.osgi.RunnerBuilderExtension;

@Component(service = RunnerBuilderExtension.class)
public class HopProviderExtension implements RunnerBuilderExtension {
@Reference(
service = Hop.class,
cardinality = ReferenceCardinality.MULTIPLE,
policy = ReferencePolicy.DYNAMIC,
policyOption = ReferencePolicyOption.GREEDY,
fieldOption = FieldOption.REPLACE
)
@SuppressWarnings("PMD.AvoidUsingVolatile")
private volatile List<Hop<?>> hops;

@Override
public void extend(RunnerBuilder builder) {
builder.addHops(hops);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
import lombok.With;

import org.apache.commons.lang3.StringUtils;
import org.osgi.service.component.annotations.Component;

import com.swisscom.aem.tools.impl.HopContext;
import com.swisscom.aem.tools.jcrhopper.Hop;
import com.swisscom.aem.tools.jcrhopper.HopConfig;
import com.swisscom.aem.tools.jcrhopper.HopperException;

@Component(service = Hop.class)
public class ChildNodes implements Hop<ChildNodes.Config> {
@Override
public void run(Config config, Node node, HopContext context) throws RepositoryException, HopperException {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/swisscom/aem/tools/impl/hops/CopyNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
import lombok.With;
import lombok.extern.slf4j.Slf4j;

import org.osgi.service.component.annotations.Component;

import com.swisscom.aem.tools.impl.HopContext;
import com.swisscom.aem.tools.jcrhopper.ConflictResolution;
import com.swisscom.aem.tools.jcrhopper.Hop;
import com.swisscom.aem.tools.jcrhopper.HopConfig;
import com.swisscom.aem.tools.jcrhopper.HopperException;

@Slf4j
@Component(service = Hop.class)
public class CopyNode implements Hop<CopyNode.Config> {
@Override
public void run(Config config, Node node, HopContext context) throws RepositoryException, HopperException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
import lombok.With;

import org.apache.jackrabbit.JcrConstants;
import org.osgi.service.component.annotations.Component;

import com.swisscom.aem.tools.impl.HopContext;
import com.swisscom.aem.tools.jcrhopper.ConflictResolution;
import com.swisscom.aem.tools.jcrhopper.Hop;
import com.swisscom.aem.tools.jcrhopper.HopConfig;
import com.swisscom.aem.tools.jcrhopper.HopperException;

@Component(service = Hop.class)
public class CreateChildNode implements Hop<CreateChildNode.Config> {
@Override
public void run(Config config, Node node, HopContext context) throws RepositoryException, HopperException {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/swisscom/aem/tools/impl/hops/Declare.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
import lombok.ToString;
import lombok.With;

import org.osgi.service.component.annotations.Component;

import com.swisscom.aem.tools.impl.HopContext;
import com.swisscom.aem.tools.jcrhopper.Hop;
import com.swisscom.aem.tools.jcrhopper.HopConfig;
import com.swisscom.aem.tools.jcrhopper.HopperException;

@RequiredArgsConstructor
@Component(service = Hop.class)
public class Declare implements Hop<Declare.Config> {
@Override
public void run(Config config, Node node, HopContext context) throws RepositoryException, HopperException {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/swisscom/aem/tools/impl/hops/Each.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
import lombok.ToString;
import lombok.With;

import org.osgi.service.component.annotations.Component;

import com.swisscom.aem.tools.impl.HopContext;
import com.swisscom.aem.tools.jcrhopper.Hop;
import com.swisscom.aem.tools.jcrhopper.HopConfig;
import com.swisscom.aem.tools.jcrhopper.HopperException;

@AllArgsConstructor
@Component(service = Hop.class)
public class Each implements Hop<Each.Config> {
@Override
public void run(Config config, Node node, HopContext context) throws RepositoryException, HopperException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
import lombok.ToString;
import lombok.With;

import org.osgi.service.component.annotations.Component;

import com.swisscom.aem.tools.impl.HopContext;
import com.swisscom.aem.tools.jcrhopper.Hop;
import com.swisscom.aem.tools.jcrhopper.HopConfig;
import com.swisscom.aem.tools.jcrhopper.HopperException;

@Component(service = Hop.class)
public class FilterNode implements Hop<FilterNode.Config> {
@Override
public void run(Config config, Node node, HopContext context) throws RepositoryException, HopperException {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/swisscom/aem/tools/impl/hops/MoveNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import lombok.With;

import org.apache.commons.lang3.StringUtils;
import org.osgi.service.component.annotations.Component;

import com.swisscom.aem.tools.impl.HopContext;
import com.swisscom.aem.tools.jcrhopper.ConflictResolution;
Expand All @@ -27,6 +28,7 @@
import com.swisscom.aem.tools.jcrhopper.HopperException;

@AllArgsConstructor
@Component(service = Hop.class)
public class MoveNode implements Hop<MoveNode.Config> {
/**
* Resolve the path to a new node, creating the parent nodes if necessary.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import lombok.With;

import org.apache.commons.lang3.StringUtils;
import org.osgi.service.component.annotations.Component;

import com.swisscom.aem.tools.impl.HopContext;
import com.swisscom.aem.tools.jcrhopper.Hop;
import com.swisscom.aem.tools.jcrhopper.HopConfig;
import com.swisscom.aem.tools.jcrhopper.HopperException;
import com.swisscom.aem.tools.impl.HopContext;

@Component(service = Hop.class)
public class NodeQuery implements Hop<NodeQuery.Config> {
@Override
public void run(Config config, Node node, HopContext context) throws RepositoryException, HopperException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.spi.commons.conversion.IllegalNameException;
import org.osgi.service.component.annotations.Component;

import com.swisscom.aem.tools.impl.HopContext;
import com.swisscom.aem.tools.jcrhopper.ConflictResolution;
Expand All @@ -22,6 +23,7 @@
import com.swisscom.aem.tools.jcrhopper.HopperException;

@AllArgsConstructor
@Component(service = Hop.class)
public class RenameProperty implements Hop<RenameProperty.Config> {
@Override
public void run(Config config, Node node, HopContext context) throws RepositoryException, HopperException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import lombok.With;

import org.apache.commons.lang3.StringUtils;
import org.osgi.service.component.annotations.Component;

import com.swisscom.aem.tools.impl.HopContext;
import com.swisscom.aem.tools.jcrhopper.ConflictResolution;
Expand All @@ -19,6 +20,7 @@
import com.swisscom.aem.tools.jcrhopper.HopperException;

@AllArgsConstructor
@Component(service = Hop.class)
public class ReorderNode implements Hop<ReorderNode.Config> {
@Override
public void run(Config config, Node node, HopContext context) throws RepositoryException, HopperException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
import lombok.ToString;
import lombok.With;

import org.osgi.service.component.annotations.Component;

import com.swisscom.aem.tools.impl.HopContext;
import com.swisscom.aem.tools.jcrhopper.ConflictResolution;
import com.swisscom.aem.tools.jcrhopper.Hop;
import com.swisscom.aem.tools.jcrhopper.HopConfig;
import com.swisscom.aem.tools.jcrhopper.HopperException;

@Component(service = Hop.class)
public class ResolveNode implements Hop<ResolveNode.Config> {
@Override
public void run(Config config, Node node, HopContext context) throws RepositoryException, HopperException {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/swisscom/aem/tools/impl/hops/RunScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@

import org.apache.commons.jexl3.MapContext;
import org.apache.commons.lang3.StringUtils;
import org.osgi.service.component.annotations.Component;

import com.swisscom.aem.tools.impl.HopContext;
import com.swisscom.aem.tools.jcrhopper.Hop;
import com.swisscom.aem.tools.jcrhopper.HopConfig;
import com.swisscom.aem.tools.jcrhopper.HopperException;

@RequiredArgsConstructor
@Component(service = Hop.class)
public class RunScript implements Hop<RunScript.Config> {
@Override
public void run(Config config, Node node, HopContext context) throws RepositoryException, HopperException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
import lombok.ToString;
import lombok.With;

import org.osgi.service.component.annotations.Component;

import com.swisscom.aem.tools.impl.HopContext;
import com.swisscom.aem.tools.jcrhopper.ConflictResolution;
import com.swisscom.aem.tools.jcrhopper.Hop;
import com.swisscom.aem.tools.jcrhopper.HopConfig;
import com.swisscom.aem.tools.jcrhopper.HopperException;

@AllArgsConstructor
@Component(service = Hop.class)
public class SetProperty implements Hop<SetProperty.Config> {
/**
* Converts a property to a string representation.
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/swisscom/aem/tools/impl/hops/Try.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
import lombok.ToString;
import lombok.With;

import org.osgi.service.component.annotations.Component;

import com.swisscom.aem.tools.impl.HopContext;
import com.swisscom.aem.tools.jcrhopper.Hop;
import com.swisscom.aem.tools.jcrhopper.HopConfig;
import com.swisscom.aem.tools.jcrhopper.HopperException;

@AllArgsConstructor
@Component(service = Hop.class)
public class Try implements Hop<Try.Config> {
@Override
public void run(Config config, Node node, HopContext context) throws RepositoryException, HopperException {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/swisscom/aem/tools/jcrhopper/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.With;

import org.apache.commons.jexl3.JexlBuilder;
import org.apache.commons.jexl3.JexlEngine;
Expand All @@ -20,6 +21,7 @@
import com.swisscom.aem.tools.impl.JcrFunctions;

@Getter
@With
@RequiredArgsConstructor
public class Runner {
private final Script script;
Expand Down Expand Up @@ -58,7 +60,10 @@ public void run(Node node, boolean isDryRun) throws HopperException, RepositoryE
jcrFunctions,
variables
);
final long ts = System.currentTimeMillis();
context.trace("Starting JCR Hopper script on node {}", node.getPath());
context.runHops(node, script.getHops());
context.info("JCR Hopper script finished after {}ms", System.currentTimeMillis() - ts);
if (isDryRun) {
context.warn("Not saving changes as dry run is enabled");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.swisscom.aem.tools.jcrhopper.osgi;

import com.swisscom.aem.tools.jcrhopper.RunnerBuilder;

public interface RunnerBuilderExtension {
void extend(RunnerBuilder builder);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.swisscom.aem.tools.jcrhopper.osgi;

import java.util.List;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.FieldOption;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.component.annotations.ReferencePolicyOption;

import com.swisscom.aem.tools.jcrhopper.Runner;
import com.swisscom.aem.tools.jcrhopper.RunnerBuilder;

@Component(service = RunnerService.class)
public class RunnerService {
@Reference(
service = RunnerBuilderExtension.class,
cardinality = ReferenceCardinality.MULTIPLE,
policy = ReferencePolicy.DYNAMIC,
policyOption = ReferencePolicyOption.GREEDY,
fieldOption = FieldOption.REPLACE
)
@SuppressWarnings("PMD.AvoidUsingVolatile")
private volatile List<RunnerBuilderExtension> extensions;

public RunnerBuilder builder() {
RunnerBuilder runnerBuilder = Runner.builder();

for (RunnerBuilderExtension extension : extensions) {
extension.extend(runnerBuilder);
}

return runnerBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

import org.skyscreamer.jsonassert.JSONAssert;

import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.JcrOakAemContext;

class ScriptTest {
public final AemContext context = new JcrOakAemContext();
@Test
public void fromJson() throws IOException {
final String json = IOUtils.resourceToString("/json/pipeline-config.json", StandardCharsets.UTF_8);
Expand Down
Loading

0 comments on commit a2c9f97

Please sign in to comment.