JavaFXSafeProxy.java

  1. /*
  2.  * #%L
  3.  * *********************************************************************************************************************
  4.  *
  5.  * SteelBlue
  6.  * http://steelblue.tidalwave.it - git clone git@bitbucket.org:tidalwave/steelblue-src.git
  7.  * %%
  8.  * Copyright (C) 2015 - 2015 Tidalwave s.a.s. (http://tidalwave.it)
  9.  * %%
  10.  *
  11.  * *********************************************************************************************************************
  12.  *
  13.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  14.  * the License. You may obtain a copy of the License at
  15.  *
  16.  *     http://www.apache.org/licenses/LICENSE-2.0
  17.  *
  18.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  19.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  20.  * specific language governing permissions and limitations under the License.
  21.  *
  22.  * *********************************************************************************************************************
  23.  *
  24.  *
  25.  *
  26.  * *********************************************************************************************************************
  27.  * #L%
  28.  */
  29. package it.tidalwave.role.ui.javafx.impl.util;

  30. import javax.annotation.Nonnull;
  31. import java.lang.reflect.InvocationHandler;
  32. import java.lang.reflect.Method;
  33. import java.util.concurrent.CountDownLatch;
  34. import java.util.concurrent.atomic.AtomicReference;
  35. import lombok.Getter;
  36. import lombok.RequiredArgsConstructor;
  37. import lombok.Setter;
  38. import lombok.extern.slf4j.Slf4j;

  39. /***********************************************************************************************************************
  40.  *
  41.  * An {@link InvocationHandler} that safely wraps all method calls with {@link #Platform.runLater(Runnable)}. The caller
  42.  * is not blocked if the method is declared as {@code void}; it is blocked otherwise, so it can immediately retrieve
  43.  * the result.
  44.  *
  45.  * This behaviour is required by {@link JavaFXSafeProxyCreator.#createNodeAndDelegate()}.
  46.  *
  47.  * TODO: add support for aysnc returning a Future.
  48.  *
  49.  * @author  Fabrizio Giudici
  50.  *
  51.  **********************************************************************************************************************/
  52. @RequiredArgsConstructor @Slf4j
  53. public class JavaFXSafeProxy<T> implements InvocationHandler
  54.   {
  55.     @Nonnull @Getter @Setter
  56.     private T delegate;

  57.     @Override
  58.     public Object invoke (@Nonnull final Object proxy, @Nonnull final Method method, @Nonnull final Object[] args)
  59.       throws Throwable
  60.       {
  61.         final AtomicReference<Object> result = new AtomicReference<>();
  62.         final AtomicReference<Throwable> throwable = new AtomicReference<>();
  63.         final CountDownLatch waitForReturn = new CountDownLatch(1);

  64.         JavaFXSafeRunner.runSafely(() ->
  65.           {
  66.             try
  67.               {
  68.                 log.trace(">>>> safely invoking {}", method);
  69.                 result.set(method.invoke(delegate, args));
  70.               }
  71.             catch (Throwable t)
  72.               {
  73.                 throwable.set(t);
  74.                 log.warn("Exception while calling JavaFX", t);
  75.               }
  76.             finally
  77.               {
  78.                 waitForReturn.countDown();
  79.               }
  80.           });

  81.         if (method.getReturnType().equals(void.class))
  82.           {
  83.             return null;
  84.           }

  85.         log.trace(">>>> waiting for method completion");
  86.         waitForReturn.await();

  87.         // This is probably useless - void methods return asynchronously
  88.         if (throwable.get() != null)
  89.           {
  90.             throw throwable.get();
  91.           }

  92.         return result.get();
  93.       }
  94.   }