View Javadoc
1   /*
2    * *************************************************************************************************************************************************************
3    *
4    * SteelBlue: DCI User Interfaces
5    * http://tidalwave.it/projects/steelblue
6    *
7    * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8    *
9    * *************************************************************************************************************************************************************
10   *
11   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   *
14   *     http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * 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
17   * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
18   *
19   * *************************************************************************************************************************************************************
20   *
21   * git clone https://bitbucket.org/tidalwave/steelblue-src
22   * git clone https://github.com/tidalwave-it/steelblue-src
23   *
24   * *************************************************************************************************************************************************************
25   */
26  package it.tidalwave.ui.javafx.impl.util;
27  
28  import java.lang.reflect.InvocationHandler;
29  import java.lang.reflect.Method;
30  import jakarta.annotation.Nonnull;
31  import java.util.concurrent.CountDownLatch;
32  import java.util.concurrent.atomic.AtomicReference;
33  import javafx.application.Platform;
34  import it.tidalwave.ui.javafx.JavaFXSafeProxyCreator;
35  import lombok.Getter;
36  import lombok.RequiredArgsConstructor;
37  import lombok.Setter;
38  import lombok.extern.slf4j.Slf4j;
39  
40  /***************************************************************************************************************************************************************
41   *
42   * An {@link InvocationHandler} that safely wraps all method calls with {@link Platform#runLater(Runnable)}. The caller
43   * is not blocked if the method is declared as {@code void}; it is blocked otherwise, so it can immediately retrieve
44   * the result.
45   *
46   * This behaviour is required by {@link JavaFXSafeProxyCreator#createNodeAndDelegate(Class)} ()}.
47   *
48   * TODO: add support for aysnc returning a Future.
49   *
50   * @author  Fabrizio Giudici
51   *
52   **************************************************************************************************************************************************************/
53  @RequiredArgsConstructor @Slf4j
54  public class JavaFXSafeProxy<T> implements InvocationHandler
55    {
56      @Nonnull @Getter @Setter
57      private T delegate;
58  
59      @Override
60      public Object invoke (@Nonnull final Object proxy, @Nonnull final Method method, @Nonnull final Object[] args)
61        throws Throwable
62        {
63          final var result = new AtomicReference<>();
64          final var throwable = new AtomicReference<Throwable>();
65          final var waitForReturn = new CountDownLatch(1);
66  
67          JavaFXSafeRunner.runSafely(() ->
68            {
69              try
70                {
71                  log.trace(">>>> safely invoking {}", method);
72                  result.set(method.invoke(delegate, args));
73                }
74              catch (Throwable t)
75                {
76                  throwable.set(t);
77                  log.error("Exception while calling JavaFX", t);
78                }
79              finally
80                {
81                  waitForReturn.countDown();
82                }
83            });
84  
85          log.trace(">>>> waiting for method completion");
86          waitForReturn.await();
87  
88          // This is probably useless - void methods return asynchronously
89          if (throwable.get() != null)
90            {
91              throw throwable.get();
92            }
93  
94          return method.getReturnType().equals(void.class) ? null : result.get();
95        }
96    }
97