You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2015/01/26 17:25:37 UTC

svn commit: r1654840 - in /sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly: impl/engine/extension/use/PojoUseProvider.java impl/engine/extension/use/UseRuntimeExtension.java use/ProviderOutcome.java

Author: fmeschbe
Date: Mon Jan 26 16:25:36 2015
New Revision: 1654840

URL: http://svn.apache.org/r1654840
Log:
SLING-4337 Sightly should only log errors from use providers if none of the use providers succeeded

Applying patch by Justin Edelson and Radu Cotescu (Thanks a lot)

One slight modification, though: I made the three fields of ProviderOutcome final
and add the cause to the private constructor. Also renamed the field t to cause
and the method to getCause -- along the lines of how Throwable does it

Modified:
    sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/PojoUseProvider.java
    sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/UseRuntimeExtension.java
    sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/use/ProviderOutcome.java

Modified: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/PojoUseProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/PojoUseProvider.java?rev=1654840&r1=1654839&r2=1654840&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/PojoUseProvider.java (original)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/PojoUseProvider.java Mon Jan 26 16:25:36 2015
@@ -89,8 +89,7 @@ public class PojoUseProvider implements
             }
             return ProviderOutcome.notNullOrFailure(result);
         } catch (Exception e) {
-            LOG.error(String.format("Can't instantiate %s POJO.", identifier), e);
-            return ProviderOutcome.failure();
+            return ProviderOutcome.failure(e);
         }
     }
 }

Modified: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/UseRuntimeExtension.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/UseRuntimeExtension.java?rev=1654840&r1=1654839&r2=1654840&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/UseRuntimeExtension.java (original)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/UseRuntimeExtension.java Mon Jan 26 16:25:36 2015
@@ -77,14 +77,17 @@ public class UseRuntimeExtension impleme
         Bindings useArguments = new SimpleBindings(Collections.unmodifiableMap(useArgumentsMap));
         ArrayList<UseProvider> providers = new ArrayList<UseProvider>(providersMap.values());
         ListIterator<UseProvider> iterator = providers.listIterator(providers.size());
+        Throwable failureCause = null;
         while (iterator.hasPrevious()) {
             UseProvider provider = iterator.previous();
             ProviderOutcome outcome = provider.provide(identifier, renderContext, useArguments);
             if (outcome.isSuccess()) {
                 return outcome.getResult();
+            } else if (outcome.getCause() != null) {
+                failureCause = outcome.getCause();
             }
         }
-        throw new SightlyException("No use provider could resolve identifier: " + identifier);
+        throw new SightlyException("No use provider could resolve identifier: " + identifier, failureCause);
     }
 
     // OSGi ################################################################################################################################

Modified: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/use/ProviderOutcome.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/use/ProviderOutcome.java?rev=1654840&r1=1654839&r2=1654840&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/use/ProviderOutcome.java (original)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/use/ProviderOutcome.java Mon Jan 26 16:25:36 2015
@@ -24,31 +24,52 @@ package org.apache.sling.scripting.sight
  */
 public final class ProviderOutcome {
 
-    private boolean success;
-    private Object result;
+    // whether this is a success or failure
+    private final boolean success;
 
-    private static final ProviderOutcome FAILURE = new ProviderOutcome(false, null);
+    // the result value in case of success (may be null)
+    private final Object result;
+
+    // the reason for failure in case of failure (may be null)
+    private final Throwable cause;
+
+    // a generic failure without a cause returned by #failure()
+    private static final ProviderOutcome GENERIC_FAILURE = new ProviderOutcome(false, null, null);
 
     /**
      * Create a successful outcome
+     *
      * @param result the result
      * @return a successful result
      */
     public static ProviderOutcome success(Object result) {
-        return new ProviderOutcome(true, result);
+        return new ProviderOutcome(true, result, null);
     }
 
     /**
-     * Create a failed outcome
+     * Create a failed outcome without a specific {@link #getCause() cause}
      * @return a failed outcome
      */
     public static ProviderOutcome failure() {
-        return FAILURE;
+        return GENERIC_FAILURE;
     }
 
     /**
-     * If the given obj is not null return a successful outcome, with the given result.
-     * Otherwise, return failure
+     * Create a failed outcome with the given {@link #getCause() cause}
+     *
+     * @param cause The reason for this failure, which may be {@code null}
+     *
+     * @return a failed outcome
+     */
+    public static ProviderOutcome failure(Throwable cause) {
+        return new ProviderOutcome(false, null, cause);
+    }
+
+    /**
+     * If the given obj is not {@code null} return a {@link #success(Object)
+     * successful outcome}, with the given result. Otherwise, return
+     * {@link #failure()}
+     *
      * @param obj the result
      * @return an outcome based on whether the parameter is null or not
      */
@@ -56,13 +77,24 @@ public final class ProviderOutcome {
         return (obj == null) ? failure() : success(obj);
     }
 
-    private ProviderOutcome(boolean success, Object result) {
+    /**
+     * Creates an outcome instance
+     *
+     * @param success {@code true} to indicate success or {@code false} to
+     *            indicate failure
+     * @param result optional result value in case of success, may be
+     *            {@code null}
+     * @param cause optional cause in case of failure, may be {@code null}
+     */
+    private ProviderOutcome(boolean success, Object result, Throwable cause) {
         this.success = success;
         this.result = result;
+        this.cause = cause;
     }
 
     /**
      * Check if the outcome has been successful
+     *
      * @return the outcome success status
      */
     public boolean isSuccess() {
@@ -71,6 +103,7 @@ public final class ProviderOutcome {
 
     /**
      * Check whether the outcome is a failure
+     *
      * @return the outcome failure status
      */
     public boolean isFailure() {
@@ -81,12 +114,23 @@ public final class ProviderOutcome {
      * Get the result in this outcome
      *
      * @return the result of the container
-     * @throws java.lang.UnsupportedOperationException if the outcome is a failure
+     * @throws IllegalStateException if the outcome is a failure
      */
     public Object getResult() {
         if (!success) {
-            throw new UnsupportedOperationException("Outcome has not been successful");
+            throw new IllegalStateException("Outcome has not been successful");
         }
         return result;
     }
+
+    /**
+     * Returns the cause for this failure outcome or {@code null} if this
+     * outcome is a success or no cause has been defined with the
+     * {@link #failure(Throwable)} method.
+     *
+     * @return the cause for this failure outcome.
+     */
+    public Throwable getCause() {
+        return cause;
+    }
 }