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

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


The following commit(s) were added to refs/heads/master by this push:
     new b1bb68c  SLING-7690 make reference dynamically computed
     new 4ce29a9  Merge branch 'master' of 
github.com:apache/sling-org-apache-sling-pipes
b1bb68c is described below

commit b1bb68cb8d12e989ae25c54886c3b39a010edc68
Author: Nicolas Peltier <[email protected]>
AuthorDate: Tue May 29 16:21:04 2018 +0200

    SLING-7690 make reference dynamically computed
---
 .../java/org/apache/sling/pipes/ReferencePipe.java | 59 +++++++++++++++-------
 .../org/apache/sling/pipes/internal/NotPipe.java   |  2 +-
 2 files changed, 43 insertions(+), 18 deletions(-)

diff --git a/src/main/java/org/apache/sling/pipes/ReferencePipe.java 
b/src/main/java/org/apache/sling/pipes/ReferencePipe.java
index 798e688..5d329b4 100644
--- a/src/main/java/org/apache/sling/pipes/ReferencePipe.java
+++ b/src/main/java/org/apache/sling/pipes/ReferencePipe.java
@@ -16,10 +16,12 @@
  */
 package org.apache.sling.pipes;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.api.resource.Resource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.script.ScriptException;
 import java.util.Iterator;
 
 /**
@@ -32,38 +34,61 @@ public class ReferencePipe extends BasePipe {
 
     protected Pipe reference;
 
+    /**
+     * path of the reference pipe
+     */
+    String referencePath;
+
     public ReferencePipe(Plumber plumber, Resource resource) throws Exception {
         super(plumber, resource);
-        Resource pipeResource = resolver.getResource(getExpr());
-        if (pipeResource == null){
+    }
+
+    @Override
+    public PipeBindings getBindings() {
+        return reference.getBindings();
+    }
+
+    /**
+     * Computing the pipe this pipe refers to, and make necessary bindings
+     * @throws Exception
+     */
+    protected void computeReference() throws Exception {
+        Resource pipeResource = resolver.getResource(referencePath);
+        if (pipeResource == null) {
             throw new Exception("Reference configuration error: There is no 
resource at " + getExpr());
         }
         reference = plumber.getPipe(pipeResource);
-        if (reference == null){
+        if (reference == null) {
             throw new Exception("Unable to build out pipe out of " + 
getPath());
         }
         reference.setReferrer(this);
         log.info("set reference to {}", reference);
-    }
 
-    @Override
-    public void setParent(ContainerPipe parent) {
-        super.setParent(parent);
-        reference.setParent(parent);
-    }
-
-    @Override
-    public void setBindings(PipeBindings bindings) {
-        reference.setBindings(bindings);
+        //bind parent to the reference
+        if (parent != null) {
+            reference.setParent(parent);
+        }
+        //set reference's bindings
+        if (bindings != null) {
+            reference.setBindings(bindings);
+        }
     }
 
     @Override
-    public PipeBindings getBindings() {
-        return reference.getBindings();
+    protected Iterator<Resource> computeOutput() throws Exception {
+        String expression = getExpr();
+        if (StringUtils.isNotBlank(expression) && 
!expression.equals(referencePath)){
+            referencePath = expression;
+            computeReference();
+        }
+        return computeReferenceOutput();
     }
 
-    @Override
-    protected Iterator<Resource> computeOutput() throws Exception {
+    /**
+     * @return referenced pipe output
+     * @throws Exception sent by reference piped execution
+     */
+    protected Iterator<Resource> computeReferenceOutput() throws Exception {
         log.debug("getting {} output", reference);
         return reference.getOutput();
     }
diff --git a/src/main/java/org/apache/sling/pipes/internal/NotPipe.java 
b/src/main/java/org/apache/sling/pipes/internal/NotPipe.java
index 18b710a..02a5355 100644
--- a/src/main/java/org/apache/sling/pipes/internal/NotPipe.java
+++ b/src/main/java/org/apache/sling/pipes/internal/NotPipe.java
@@ -36,7 +36,7 @@ public class NotPipe extends ReferencePipe {
     }
 
     @Override
-    protected Iterator<Resource> computeOutput() throws Exception {
+    protected Iterator<Resource> computeReferenceOutput() throws Exception {
         if (reference.getOutput().hasNext()){
             return EMPTY_ITERATOR;
         }

-- 
To stop receiving notification emails like this one, please contact
[email protected].

Reply via email to