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

dklco pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git


The following commit(s) were added to refs/heads/master by this push:
     new f6c636c  Adding initial crack at a batch operations Commons library
f6c636c is described below

commit f6c636cebf60b4510af674f6e339c0546f3bec0c
Author: Dan Klco <[email protected]>
AuthorDate: Thu Jul 15 17:35:30 2021 -0400

    Adding initial crack at a batch operations Commons library
---
 org.apache.sling.commons.batch/pom.xml             | 138 ++++++++++++++++++++
 .../java/org/apache/sling/commons/batch/Batch.java | 144 +++++++++++++++++++++
 .../sling/commons/batch/ExecutionStrategy.java     |  42 ++++++
 .../org/apache/sling/commons/batch/Operation.java  |  52 ++++++++
 .../org/apache/sling/commons/batch/Result.java     |  78 +++++++++++
 .../batch/provided/FinalizeOnceStrategy.java       |  72 +++++++++++
 .../provided/FinalizePerOperationStrategy.java     |  80 ++++++++++++
 .../batch/provided/FunctionalOperation.java        |  82 ++++++++++++
 .../commons/batch/sling/CommitOnceStrategy.java    |  47 +++++++
 .../batch/sling/CommitPerOperationStrategy.java    |  47 +++++++
 .../org/apache/sling/commons/batch/BatchTest.java  | 112 ++++++++++++++++
 11 files changed, 894 insertions(+)

diff --git a/org.apache.sling.commons.batch/pom.xml 
b/org.apache.sling.commons.batch/pom.xml
new file mode 100644
index 0000000..418ffe2
--- /dev/null
+++ b/org.apache.sling.commons.batch/pom.xml
@@ -0,0 +1,138 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!-- 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. -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <artifactId>sling-bundle-parent</artifactId>
+        <groupId>org.apache.sling</groupId>
+        <version>43</version>
+    </parent>
+    <artifactId>org.apache.sling.commons.batch</artifactId>
+    <name>Apache Sling Commons Batch Processing</name>
+    <description>An API and Service for creating and transforming images and 
documents into thumbnails</description>
+    <version>1.0.0-SNAPSHOT</version>
+
+    <properties>
+        <sling.java.version>8</sling.java.version>
+    </properties>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>biz.aQute.bnd</groupId>
+                <artifactId>bnd-maven-plugin</artifactId>
+                <version>5.3.0</version>
+            </plugin>
+            <plugin>
+                <groupId>biz.aQute.bnd</groupId>
+                <artifactId>bnd-baseline-maven-plugin</artifactId>
+                <configuration>
+                    <failOnMissing>false</failOnMissing>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>11</source>
+                    <target>11</target>
+                </configuration>
+            </plugin>
+            <!-- Code Coverage report generation -->
+            <plugin>
+                <groupId>org.jacoco</groupId>
+                <artifactId>jacoco-maven-plugin</artifactId>
+                <version>0.8.7</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>prepare-agent</goal>
+                        </goals>
+                    </execution>
+                    <execution>
+                        <id>generate-code-coverage-report</id>
+                        <phase>test</phase>
+                        <goals>
+                            <goal>report</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+
+        </plugins>
+    </build>
+
+    <dependencies>
+
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <version>2.5</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.jetbrains</groupId>
+            <artifactId>annotations</artifactId>
+            <version>21.0.1</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>osgi.annotation</artifactId>
+            <version>7.0.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.service.component.annotations</artifactId>
+            <version>1.4.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.service.metatype.annotations</artifactId>
+            <version>1.4.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>osgi.core</artifactId>
+            <version>6.0.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>osgi.cmpn</artifactId>
+            <version>6.0.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+        </dependency>
+
+        <!-- Sling Dependencies -->
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.api</artifactId>
+            <version>2.18.4</version>
+            <scope>provided</scope>
+            <optional>true</optional>
+        </dependency>
+
+        <!-- Testing dependencies -->
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+        </dependency>
+
+    </dependencies>
+</project>
\ No newline at end of file
diff --git 
a/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/Batch.java
 
b/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/Batch.java
new file mode 100644
index 0000000..71009da
--- /dev/null
+++ 
b/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/Batch.java
@@ -0,0 +1,144 @@
+/*
+ * 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.commons.batch;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class Batch {
+
+    private Logger log = LoggerFactory.getLogger(Batch.class);
+
+    public static class Builder {
+
+        private List<Operation> operations;
+
+        private List<ExecutionStrategy> strategies;
+
+        private Builder() {
+        }
+
+        public static Builder getInstance() {
+            return new Builder();
+        }
+
+        public Builder addOperation(Operation operation) {
+            if (this.operations == null) {
+                this.operations = new ArrayList<>();
+            }
+            this.operations.add(operation);
+            return this;
+        }
+
+        public Builder setOperations(List<Operation> operations) {
+            this.operations = operations;
+            return this;
+        }
+
+        public Builder addStrategy(ExecutionStrategy strategy) {
+            if (this.strategies == null) {
+                this.strategies = new ArrayList<>();
+            }
+            this.strategies.add(strategy);
+            return this;
+        }
+
+        public Builder setStrategies(List<ExecutionStrategy> strategies) {
+            this.strategies = strategies;
+            return this;
+        }
+
+        public Batch build() {
+            return new Batch(operations, strategies);
+        }
+
+    }
+
+    enum STATUS {
+        COMPLETE, IN_PROGRESS, NOT_STARTED, FAILED;
+    }
+
+    private final List<Operation> operations;
+
+    private final List<ExecutionStrategy> strategies;
+
+    private STATUS status;
+
+    private Batch(List<Operation> operations, List<ExecutionStrategy> 
strategies) {
+        this.operations = operations;
+        this.strategies = strategies;
+        this.status = STATUS.NOT_STARTED;
+    }
+
+    public void execute() {
+        this.status = STATUS.IN_PROGRESS;
+        for (ExecutionStrategy strategy : strategies) {
+            try {
+                log.debug("Executing strategy: {}", strategy);
+                Result result = strategy.execute(this);
+                log.debug("Recieved result: {}", result);
+                if (result.getStatus() == Result.STATUS.SUCCEEDED) {
+                    log.info("Strategy {} executed successfully!", strategy);
+                    this.status = STATUS.COMPLETE;
+                    return;
+                } else {
+                    log.warn("Recieved failed response {} from strategy {}", 
result, strategy);
+                    strategy.reset(this);
+                }
+            } catch (Exception e) {
+                log.error("Uncaught exception from strategy: {}", strategy, e);
+                strategy.reset(this);
+            }
+        }
+
+        log.info("No more strategies to try, batch failed");
+        this.status = STATUS.FAILED;
+    }
+
+    public boolean allOperationsSucceeded() {
+        return operations.stream().allMatch(op -> op.getStatus() == 
Operation.STATUS.SUCCEEDED);
+    }
+
+    public List<Operation> getFailedOperations() {
+        return operations.stream().filter(op -> op.getStatus() != 
Operation.STATUS.SUCCEEDED)
+                .collect(Collectors.toList());
+    }
+
+    public List<Operation> getSucceededOperations() {
+        return operations.stream().filter(op -> op.getStatus() == 
Operation.STATUS.SUCCEEDED)
+                .collect(Collectors.toList());
+    }
+
+    public STATUS getStatus() {
+        return status;
+    }
+
+    public List<Operation> getOperations() {
+        return this.operations;
+    }
+
+    public List<ExecutionStrategy> getStrategies() {
+        return this.strategies;
+    }
+
+}
diff --git 
a/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/ExecutionStrategy.java
 
b/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/ExecutionStrategy.java
new file mode 100644
index 0000000..d977e63
--- /dev/null
+++ 
b/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/ExecutionStrategy.java
@@ -0,0 +1,42 @@
+/*
+ * 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.commons.batch;
+
+/**
+ * Execution strategies define how a batch gets executed.
+ */
+public interface ExecutionStrategy {
+
+    /**
+     * Executes the batch of operations and returns a result indicating the 
status
+     * of the batch
+     * 
+     * @param batch the batch of operations to execute
+     * @return the result of executing the batch
+     */
+    Result execute(Batch batch);
+
+    /**
+     * Reset the batch and / or underlying system to it's initial state. Note 
the
+     * meaning of resetting may vary depending on the strategy being executed.
+     * 
+     * @param batch the batch to reset
+     */
+    void reset(Batch batch);
+}
diff --git 
a/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/Operation.java
 
b/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/Operation.java
new file mode 100644
index 0000000..9afb5bf
--- /dev/null
+++ 
b/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/Operation.java
@@ -0,0 +1,52 @@
+/*
+ * 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.commons.batch;
+
+/**
+ * An atomic operation to be executed as a part of the batch.
+ */
+public interface Operation {
+    enum STATUS {
+        SUCCEEDED, FAILED, IN_PROGRESS, NOT_STARTED
+    }
+
+    /**
+     * Resets the operation
+     */
+    void reset();
+
+    /**
+     * Gets the status of this operation.
+     * 
+     * @return the status
+     */
+    STATUS getStatus();
+
+    /**
+     * Executes the operation and returns the result
+     */
+    Result execute();
+
+    /**
+     * Sets the status of this operation.
+     * 
+     * @return the status
+     */
+    void setStatus(STATUS status);
+}
diff --git 
a/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/Result.java
 
b/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/Result.java
new file mode 100644
index 0000000..4ba637e
--- /dev/null
+++ 
b/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/Result.java
@@ -0,0 +1,78 @@
+/*
+ * 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.commons.batch;
+
+/**
+ * Represents a result of an operation of an atomic operation.
+ */
+public class Result {
+
+    public enum STATUS {
+        SUCCEEDED, FAILED;
+    }
+
+    private final Throwable cause;
+    private final String message;
+    private final STATUS status;
+
+    private Result(STATUS status, String message, Throwable cause) {
+        this.cause = cause;
+        this.message = message;
+        this.status = status;
+    }
+
+    public static Result succeeded() {
+        return new Result(STATUS.SUCCEEDED, null, null);
+    }
+
+    public static Result succeeded(String message) {
+        return new Result(STATUS.SUCCEEDED, message, null);
+    }
+
+    public static Result failed(String message) {
+        return new Result(STATUS.FAILED, message, null);
+    }
+
+    public static Result failed(String message, Throwable cause) {
+        return new Result(STATUS.FAILED, message, cause);
+    }
+
+    public Throwable getCause() {
+        return this.cause;
+    }
+
+    public String getMessage() {
+        return this.message;
+    }
+
+    public STATUS getStatus() {
+        return this.status;
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see java.lang.Object#toString()
+     */
+    @Override
+    public String toString() {
+        return "Result [cause=" + cause + ", message=" + message + ", status=" 
+ status + "]";
+    }
+
+}
\ No newline at end of file
diff --git 
a/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/provided/FinalizeOnceStrategy.java
 
b/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/provided/FinalizeOnceStrategy.java
new file mode 100644
index 0000000..916ef0b
--- /dev/null
+++ 
b/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/provided/FinalizeOnceStrategy.java
@@ -0,0 +1,72 @@
+/*
+ * 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.commons.batch.provided;
+
+import java.util.function.Supplier;
+
+import org.apache.sling.commons.batch.Batch;
+import org.apache.sling.commons.batch.ExecutionStrategy;
+import org.apache.sling.commons.batch.Operation;
+import org.apache.sling.commons.batch.Result;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class FinalizeOnceStrategy implements ExecutionStrategy {
+
+    private static final Logger log = 
LoggerFactory.getLogger(FinalizeOnceStrategy.class);
+
+    private final Supplier<Result> finalizer;
+    private final Runnable resetter;
+
+    public FinalizeOnceStrategy(Supplier<Result> finalizer, Runnable resetter) 
{
+        this.finalizer = finalizer;
+        this.resetter = resetter;
+    }
+
+    @Override
+    public Result execute(Batch batch) {
+        boolean succeeded = batch.getOperations().stream().filter(op -> 
op.getStatus() == Operation.STATUS.NOT_STARTED)
+                .map(op -> {
+                    try {
+                        return op.execute();
+                    } catch (Exception e) {
+                        log.error("Uncaught exception executing operation: 
{}", op, e);
+                        return Result.failed("Uncaught exception executing 
operation: " + op, e);
+                    }
+                }).allMatch(r -> r.getStatus() == Result.STATUS.SUCCEEDED);
+        if (succeeded) {
+            try {
+                return finalizer.get();
+            } catch (Exception e) {
+                log.error("Uncaught exception calling finalizer", e);
+                return Result.failed("Uncaught exception calling finalizer", 
e);
+            }
+        } else {
+            log.warn("Not all operations succeeded, strategy failed");
+            return Result.failed("Not all operations succeeded");
+        }
+    }
+
+    @Override
+    public void reset(Batch batch) {
+        batch.getOperations().stream().forEach(Operation::reset);
+        resetter.run();
+    }
+
+}
diff --git 
a/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/provided/FinalizePerOperationStrategy.java
 
b/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/provided/FinalizePerOperationStrategy.java
new file mode 100644
index 0000000..5ea1cd9
--- /dev/null
+++ 
b/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/provided/FinalizePerOperationStrategy.java
@@ -0,0 +1,80 @@
+/*
+ * 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.commons.batch.provided;
+
+import java.util.function.Supplier;
+
+import org.apache.sling.commons.batch.Batch;
+import org.apache.sling.commons.batch.ExecutionStrategy;
+import org.apache.sling.commons.batch.Operation;
+import org.apache.sling.commons.batch.Result;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class FinalizePerOperationStrategy implements ExecutionStrategy {
+
+    private static final Logger log = 
LoggerFactory.getLogger(FinalizePerOperationStrategy.class);
+
+    private final Supplier<Result> finalizer;
+    private final Runnable resetter;
+
+    public FinalizePerOperationStrategy(Supplier<Result> finalizer, Runnable 
resetter) {
+        this.finalizer = finalizer;
+        this.resetter = resetter;
+    }
+
+    @Override
+    public Result execute(Batch batch) {
+        batch.getOperations().stream().filter(op -> op.getStatus() == 
Operation.STATUS.NOT_STARTED).forEach(op -> {
+            try {
+                executeOperation(op);
+            } catch (Exception e) {
+                log.error("Uncaught exception executing operation: {}", op, e);
+                op.setStatus(Operation.STATUS.FAILED);
+                this.resetter.run();
+            }
+        });
+        return Result.succeeded();
+    }
+
+    private void executeOperation(Operation op) {
+        Result opResult = op.execute();
+        log.debug("Retrived result {} from operation: {}", opResult, op);
+        if (opResult.getStatus() == Result.STATUS.SUCCEEDED) {
+            log.debug("Finalizing operation");
+            Result result = finalizer.get();
+            if (result.getStatus() != Result.STATUS.SUCCEEDED) {
+                log.warn("Finalizing operation failed: {}", result);
+                op.setStatus(Operation.STATUS.FAILED);
+                this.resetter.run();
+            } else {
+                log.debug("Finalizing operation succeeded: {}", result);
+            }
+        } else {
+            log.warn("Executing operation {} failed: {}", op, opResult);
+            this.resetter.run();
+        }
+    }
+
+    @Override
+    public void reset(Batch batch) {
+        resetter.run();
+    }
+
+}
diff --git 
a/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/provided/FunctionalOperation.java
 
b/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/provided/FunctionalOperation.java
new file mode 100644
index 0000000..a21c855
--- /dev/null
+++ 
b/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/provided/FunctionalOperation.java
@@ -0,0 +1,82 @@
+/*
+ * 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.commons.batch.provided;
+
+import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import org.apache.sling.commons.batch.Operation;
+import org.apache.sling.commons.batch.Result;
+
+/**
+ * An Operation implementation which executes the provided function with the
+ * provided value.
+ */
+public class FunctionalOperation<T> implements Operation {
+
+    private final Function<T, Result> function;
+    private final T value;
+    private Operation.STATUS status;
+
+    public FunctionalOperation(T value, Function<T, Result> function) {
+        this.value = value;
+        this.function = function;
+        this.status = Operation.STATUS.NOT_STARTED;
+    }
+
+    public static <T> List<FunctionalOperation<T>> newOperations(List<T> 
value, Function<T, Result> function) {
+        return value.stream().map(v -> new FunctionalOperation<>(v, 
function)).collect(Collectors.toList());
+    }
+
+    @Override
+    public void reset() {
+        this.setStatus(Operation.STATUS.NOT_STARTED);
+    }
+
+    @Override
+    public STATUS getStatus() {
+        return this.status;
+    }
+
+    @Override
+    public Result execute() {
+        this.status = STATUS.IN_PROGRESS;
+        Result result = function.apply(value);
+        this.status = result.getStatus() == Result.STATUS.SUCCEEDED ? 
STATUS.SUCCEEDED : STATUS.FAILED;
+        return result;
+    }
+
+    @Override
+    public void setStatus(STATUS status) {
+        this.status = status;
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see java.lang.Object#toString()
+     */
+
+    @Override
+    public String toString() {
+        return "FunctionalOperation [status=" + status + ", value=" + value + 
"]";
+    }
+
+}
diff --git 
a/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/sling/CommitOnceStrategy.java
 
b/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/sling/CommitOnceStrategy.java
new file mode 100644
index 0000000..ecb1d44
--- /dev/null
+++ 
b/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/sling/CommitOnceStrategy.java
@@ -0,0 +1,47 @@
+/*
+ * 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.commons.batch.sling;
+
+import org.apache.sling.api.resource.PersistenceException;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.commons.batch.Result;
+import org.apache.sling.commons.batch.provided.FinalizeOnceStrategy;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class CommitOnceStrategy extends FinalizeOnceStrategy {
+
+    private static final Logger log = 
LoggerFactory.getLogger(CommitOnceStrategy.class);
+
+    public CommitOnceStrategy(final ResourceResolver resolver) {
+        super(() -> {
+            try {
+                resolver.commit();
+                return Result.succeeded();
+            } catch (PersistenceException pe) {
+                log.warn("Failed to commit operations", pe);
+                return Result.failed("Failed to commit operations", pe);
+            }
+        }, () -> {
+            resolver.revert();
+            resolver.refresh();
+        });
+    }
+
+}
diff --git 
a/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/sling/CommitPerOperationStrategy.java
 
b/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/sling/CommitPerOperationStrategy.java
new file mode 100644
index 0000000..cc6c789
--- /dev/null
+++ 
b/org.apache.sling.commons.batch/src/main/java/org/apache/sling/commons/batch/sling/CommitPerOperationStrategy.java
@@ -0,0 +1,47 @@
+/*
+ * 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.commons.batch.sling;
+
+import org.apache.sling.api.resource.PersistenceException;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.commons.batch.Result;
+import org.apache.sling.commons.batch.provided.FinalizePerOperationStrategy;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class CommitPerOperationStrategy extends FinalizePerOperationStrategy {
+
+    private static final Logger log = 
LoggerFactory.getLogger(CommitPerOperationStrategy.class);
+
+    public CommitPerOperationStrategy(final ResourceResolver resolver) {
+        super(() -> {
+            try {
+                resolver.commit();
+                return Result.succeeded();
+            } catch (PersistenceException pe) {
+                log.warn("Failed to commit operation", pe);
+                return Result.failed("Failed to commit operation", pe);
+            }
+        }, () -> {
+            resolver.revert();
+            resolver.refresh();
+        });
+    }
+
+}
diff --git 
a/org.apache.sling.commons.batch/src/test/java/org/apache/sling/commons/batch/BatchTest.java
 
b/org.apache.sling.commons.batch/src/test/java/org/apache/sling/commons/batch/BatchTest.java
new file mode 100644
index 0000000..c9514a4
--- /dev/null
+++ 
b/org.apache.sling.commons.batch/src/test/java/org/apache/sling/commons/batch/BatchTest.java
@@ -0,0 +1,112 @@
+/*
+ * 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.commons.batch;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.sling.commons.batch.Batch.Builder;
+import org.apache.sling.commons.batch.provided.FinalizeOnceStrategy;
+import org.apache.sling.commons.batch.provided.FinalizePerOperationStrategy;
+import org.apache.sling.commons.batch.provided.FunctionalOperation;
+import org.junit.Test;
+
+public class BatchTest {
+
+    @Test
+    public void testBuilder() {
+
+        List<String> messages = new ArrayList<>();
+        Builder bob = Batch.Builder.getInstance();
+        bob.addOperation(new FunctionalOperation<String>("Hello World", (msg) 
-> {
+            messages.add(msg);
+            return Result.succeeded();
+        }));
+
+        bob.addStrategy(new FinalizeOnceStrategy(() -> {
+            assertEquals(1, messages.size());
+            assertEquals("Hello World", messages.get(0));
+            return Result.succeeded();
+        }, () -> {
+        }));
+
+        Batch good = bob.build();
+
+        assertEquals(1, good.getOperations().size());
+        assertEquals(1, good.getStrategies().size());
+
+        good.execute();
+
+        assertSame(Batch.STATUS.COMPLETE, good.getStatus());
+
+        assertTrue(good.allOperationsSucceeded());
+    }
+
+    @Test
+    public void testFallback() {
+
+        List<String> messages = new ArrayList<>();
+        Builder bob = Batch.Builder.getInstance();
+        bob.addOperation(new FunctionalOperation<String>("Hello World", (msg) 
-> {
+            messages.add(msg);
+            return Result.succeeded();
+        }));
+        bob.addOperation(new FunctionalOperation<String>(null, (msg) -> {
+            return Result.failed("Because I feel like it!");
+        }));
+        bob.addOperation(new FunctionalOperation<String>("Goodbye World", 
(msg) -> {
+            messages.add(msg);
+            return Result.succeeded();
+        }));
+
+        // first we'll add a "SAVE ALL THE THINGS!!!! Strategy"
+        bob.addStrategy(new FinalizeOnceStrategy(() -> {
+            return Result.succeeded();
+        }, () -> {
+            messages.clear();
+        }));
+
+        // if that fails, we'll fall back to processing each item individually
+        bob.addStrategy(new FinalizePerOperationStrategy(() -> {
+            return Result.succeeded();
+        }, () -> {
+        }));
+
+        Batch bad = bob.build();
+
+        assertEquals(3, bad.getOperations().size());
+        assertEquals(2, bad.getStrategies().size());
+
+        bad.execute();
+
+        assertSame(Batch.STATUS.COMPLETE, bad.getStatus());
+
+        assertFalse(bad.allOperationsSucceeded());
+
+        assertEquals(2, messages.size());
+        assertEquals(2, bad.getSucceededOperations().size());
+        assertEquals(1, bad.getFailedOperations().size());
+    }
+
+}

Reply via email to