Class AbstractInvoker<S>

java.lang.Object
ch.tocco.nice2.util.invokables.AbstractInvoker<S>
All Implemented Interfaces:
Invoker
Direct Known Subclasses:
EmptyInvoker, PredefinedAnswersInvoker, RemoteContextInvoker, UpdatePublishStatusContext.Invoker, UriConversionEntityInterceptorContext.Invoker

public abstract class AbstractInvoker<S> extends Object implements Invoker
An abstract implementation of invoker. Extending classes should only override preInvoke() and postInvoke(). This class takes care of all exception handling.
  • Constructor Details

    • AbstractInvoker

      public AbstractInvoker()
  • Method Details

    • invoke

      public void invoke(Runnable runnable)
      Description copied from interface: Invoker
      Invokes the specified Runnable.
      Specified by:
      invoke in interface Invoker
      Parameters:
      runnable - The Runnable to invoke.
    • invoke

      public <T> T invoke(Callable<T> callable) throws Exception
      Description copied from interface: Invoker
      Invokes the specified Callable.
      Specified by:
      invoke in interface Invoker
      Parameters:
      callable - The Callable to invoke.
      Returns:
      The return value of the Callable.
      Throws:
      Exception - If the callable throws.
    • invokeRTE

      public <T> T invokeRTE(Callable<T> callable)
      Description copied from interface: Invoker
      Invokes the specified Callable not expecting any checked exceptions.
      Specified by:
      invokeRTE in interface Invoker
      Parameters:
      callable - The Callable to invoke.
      Returns:
      The return value of the Callable.
    • invoke

      public <T, E1 extends Exception> T invoke(Callable<T> callable, Class<E1> e1) throws E1
      Description copied from interface: Invoker
      Invokes the specified Callable expecting the specified exception.
      Specified by:
      invoke in interface Invoker
      Parameters:
      callable - The Callable to invoke.
      e1 - The exception to expect.
      Returns:
      The return value of the Callable.
      Throws:
      E1
    • invoke

      public <T, E1 extends Exception, E2 extends Exception> T invoke(Callable<T> callable, Class<E1> e1, Class<E2> e2) throws E1, E2
      Description copied from interface: Invoker
      Invokes the specified Callable expecting the specified exceptions.
      Specified by:
      invoke in interface Invoker
      Parameters:
      callable - The Callable to invoke.
      e1 - The first exception to expect.
      e2 - The second exception to expect.
      Returns:
      The return value of the Callable.
      Throws:
      E1
      E2
    • invoke

      public <T, E1 extends Exception, E2 extends Exception, E3 extends Exception> T invoke(Callable<T> callable, Class<E1> e1, Class<E2> e2, Class<E3> e3) throws E1, E2, E3
      Description copied from interface: Invoker
      Invokes the specified Callable expecting the specified exceptions.
      Specified by:
      invoke in interface Invoker
      Parameters:
      callable - The Callable to invoke.
      e1 - The first exception to expect.
      e2 - The second exception to expect.
      e3 - The third exception to expect.
      Returns:
      The return value of the Callable.
      Throws:
      E1
      E2
      E3
    • invoke

      public <F, T> T invoke(Function<F,T> function, F input)
      Description copied from interface: Invoker
      Invokes the specified Function with the specified input.
      Specified by:
      invoke in interface Invoker
      Parameters:
      function - The Function to invoke.
      input - The input value.
      Returns:
      The result of the function.
    • andThen

      public Invoker andThen(Invoker other)
      Description copied from interface: Invoker
      Combines `this` and the given Invoker by calling `other` inside `this` invoker. Thus, ```java SecurityManager sm = ... BusinessUnitManager bm = ... sm.privileged().andThen(bm.withBusinessUnit("m2")).invoke(new MyCallable...) ``` is equivalent to ```java sm.privileged().invoke(new Callable { public T call() throws Exception { bm.withBusinessUnit("m2").invoke(new MyCallable { }) } } ``` Which will both invoke `MyCallable` inside the BU-Invoker, which in turn is invoked within the Security-Manager-Invoker. This is to avoid too deep nesting of anonymous classes. The former variant takes also care of exception propagation, when the inner invoker expects different throwables to be thrown. You can also create an invoker from a list using the utility function from(Invoker...)
      Specified by:
      andThen in interface Invoker
    • preInvoke

      protected abstract S preInvoke()
      Called before the given invokable is invoked.
      Returns:
      An optional state which will be passed to postInvoke().
    • postInvoke

      protected abstract void postInvoke(S state, @Nullable @Nullable Object result, @Nullable @Nullable Throwable thrown)
      Called after the given invokable has been invoked. This will be called in a finally block regardless of whether the invokable throw an exception or not. If the invokable threw an exception and this method throws again, the exception thrown will be added as suppressed exception to the exception originally thrown by the invokable.
      Parameters:
      state - The state as returned previously by preInvoke().
      result - The return value of the invokable or `null`.
      thrown - The exception thrown by the invokable or `null`.
      See Also:
    • from

      public static Invoker from(Invoker... invokers)
      Creates a new invoker that is a combination of the given ones, where the first invoker in the list is the outer-most invoker. If the list is empty, the empty invoker is returned.
      Parameters:
      invokers - the invokers to combine
      Returns:
      the combined invoker