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

jtulach pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans-html4j.git

commit c5449e128067ed7416248697907e29f6736145ff
Author: Jaroslav Tulach <jaroslav.tul...@apidesign.org>
AuthorDate: Tue Mar 31 19:24:31 2020 +0200

    Record booting sequence and print the log in case of timeout
---
 .../org/netbeans/html/presenters/spi/Generic.java  | 98 ++++++++++++----------
 .../html/presenters/spi/ProtoPresenterBuilder.java |  2 +-
 .../netbeans/html/presenters/spi/ValueOfTest.java  |  5 +-
 .../html/presenters/spi/test/GenericTest.java      |  2 +-
 4 files changed, 56 insertions(+), 51 deletions(-)

diff --git 
a/generic/src/main/java/org/netbeans/html/presenters/spi/Generic.java 
b/generic/src/main/java/org/netbeans/html/presenters/spi/Generic.java
index df12a8b..1481efd 100644
--- a/generic/src/main/java/org/netbeans/html/presenters/spi/Generic.java
+++ b/generic/src/main/java/org/netbeans/html/presenters/spi/Generic.java
@@ -40,11 +40,12 @@ import java.util.NavigableSet;
 import java.util.Set;
 import java.util.TreeSet;
 import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 import org.netbeans.html.boot.spi.Fn;
 
 abstract class Generic implements Fn.Presenter, Fn.KeepAlive, Flushable {
-    private String msg;
+    private StringBuilder msg;
     private Item call;
     private final NavigableSet<Exported> exported;
     private final int key;
@@ -69,7 +70,22 @@ abstract class Generic implements Fn.Presenter, 
Fn.KeepAlive, Flushable {
         return initialized;
     }
     
-    abstract void log(Level level, String msg, Object... args);
+    final void log(Level level, String msg, Object... args) {
+        StringBuilder sb = this.msg;
+        if (sb != null) {
+            for (int i = 0; i < args.length; i++) {
+                String txt = args[i] == null ? "null" : args[i].toString();
+                msg = msg.replace("{" + i + "}", txt);
+            }
+            synchronized (lock()) {
+                sb.append('[').append(level).append("] ");
+                sb.append(msg);
+                sb.append('\n');
+            }
+        }
+        handleLog(level, msg, args);
+    }
+    abstract void handleLog(Level level, String msg, Object... args);
     
     @Texts({
         "begin=try {\n"
@@ -165,43 +181,43 @@ abstract class Generic implements Fn.Presenter, 
Fn.KeepAlive, Flushable {
             + "\n  impl.toVM = toVM;"
             + "\n  impl.toVM('r', 'OK', 'Initialized', null, null);"
             + "\n})(this);",
-
+        "initializationProtocol=--- Initialization protocol ---\n",
         "error=Cannot initialize DukeScript: @1",
         "version=$version"
     })
     final void init() {
-        if (msg != null) {
-            for (;;) {
-                try {
-                    log(Level.FINE, "Awaiting as of {0}", msg);
-                    initialized.await();
-                    log(Level.FINE, "Waiting is over");
-                    return;
-                } catch (InterruptedException ex) {
-                    log(Level.INFO, "Interrupt", ex);
-                }
+        if (initialized.getCount() == 0) {
+            return;
+        }
+        synchronized (lock()) {
+            if (initialized.getCount() == 0) {
+                return;
+            }
+            if (msg == null) {
+                this.msg = new StringBuilder(Strings.initializationProtocol());
+                callbackFn(new ProtoPresenterBuilder.OnPrepared() {
+                    @Override
+                    public void callbackIsPrepared(String clbk) {
+                        log(Level.FINE, "callbackReady with {0}", clbk);
+                        loadJS(Strings.begin(clbk).toString());
+                        log(Level.FINE, "checking OK state");
+                        loadJS(Strings.init(key, clbk).toString());
+                    }
+                });
             }
         }
-        this.msg = "";
-        callbackFn(new ProtoPresenterBuilder.OnPrepared() {
-            @Override
-            public void callbackIsPrepared(String clbk) {
-                log(Level.FINE, "callbackReady with {0}", clbk);
-                loadJS(Strings.begin(clbk).toString());
-                log(Level.FINE, "checking OK state");
-                if (!assertOK()) {
-                    final CharSequence err = Strings.error(msg);
-                    log(Level.WARNING, "no OK: {0}", err);
-                    throw new IllegalStateException(err.toString());
+        for (int counter = 0;; counter++) {
+            try {
+                handleLog(Level.FINE, "Awaiting as of {0}", counter);
+                if (initialized.await(10, TimeUnit.SECONDS)) {
+                    handleLog(Level.FINE, "Waiting is over");
+                    return;
                 }
-                log(Level.FINE, "assertOK");
-
-                loadJS(Strings.init(key, clbk).toString());
-
-                log(Level.FINE, "callbackReady: countingDown");
-                initialized.countDown();
+                handleLog(Level.INFO, msg.toString());
+            } catch (InterruptedException ex) {
+                handleLog(Level.INFO, "Interrupt", ex);
             }
-        });
+        }
     }
 
     /** @return the name of the callback function */
@@ -589,8 +605,13 @@ abstract class Generic implements Fn.Presenter, 
Fn.KeepAlive, Flushable {
         synchronized (lock()) {
             if ("OK".equals(typeof)) {
                 log(Level.FINE, "init: {0}", res);
-                this.msg = res;
                 lock().notifyAll();
+                if ("Initialized".equals(res)) {
+                    log(Level.FINE, "callbackReady: countingDown");
+                    handleLog(Level.FINE, msg.toString());
+                    msg = null;
+                    initialized.countDown();
+                }
                 return;
             }
             call.result(typeof, res);
@@ -746,19 +767,6 @@ abstract class Generic implements Fn.Presenter, 
Fn.KeepAlive, Flushable {
         }
         return ret;
     }
-
-    final boolean assertOK() {
-        synchronized (lock()) {
-            if (msg == null || msg.length() == 0) {
-                try {
-                    lock().wait(10000);
-                } catch (InterruptedException ex) {
-                    // OK, go on and check
-                }
-            }
-            return "OK".equals(msg) || "Initialized".equals(msg);
-        }
-    }
     
     private static Object[] adaptParams(Method toCall, List<Object> args) {
         final Object[] arr = new Object[args.size()];
diff --git 
a/generic/src/main/java/org/netbeans/html/presenters/spi/ProtoPresenterBuilder.java
 
b/generic/src/main/java/org/netbeans/html/presenters/spi/ProtoPresenterBuilder.java
index c510b86..f56b56b 100644
--- 
a/generic/src/main/java/org/netbeans/html/presenters/spi/ProtoPresenterBuilder.java
+++ 
b/generic/src/main/java/org/netbeans/html/presenters/spi/ProtoPresenterBuilder.java
@@ -244,7 +244,7 @@ public final class ProtoPresenterBuilder {
         }
 
         @Override
-        void log(Level level, String msg, Object... args) {
+        void handleLog(Level level, String msg, Object... args) {
             if (logger != null) {
                 logger.log(level.intValue(), msg, args);
             }
diff --git 
a/generic/src/test/java/org/netbeans/html/presenters/spi/ValueOfTest.java 
b/generic/src/test/java/org/netbeans/html/presenters/spi/ValueOfTest.java
index 8f0338c..a19f983 100644
--- a/generic/src/test/java/org/netbeans/html/presenters/spi/ValueOfTest.java
+++ b/generic/src/test/java/org/netbeans/html/presenters/spi/ValueOfTest.java
@@ -18,9 +18,6 @@
  */
 package org.netbeans.html.presenters.spi;
 
-import org.netbeans.html.presenters.spi.Level;
-import org.netbeans.html.presenters.spi.Generic;
-import org.netbeans.html.presenters.spi.ProtoPresenterBuilder;
 import java.net.URL;
 import static org.testng.Assert.*;
 import org.testng.annotations.BeforeMethod;
@@ -31,7 +28,7 @@ public class ValueOfTest {
     @BeforeMethod public void initInstance() {
         p = new Generic(true, true, "type", "app") {
             @Override
-            void log(Level level, String msg, Object... args) {
+            void handleLog(Level level, String msg, Object... args) {
             }
 
             @Override
diff --git 
a/generic/src/test/java/org/netbeans/html/presenters/spi/test/GenericTest.java 
b/generic/src/test/java/org/netbeans/html/presenters/spi/test/GenericTest.java
index 180b538..9df6444 100644
--- 
a/generic/src/test/java/org/netbeans/html/presenters/spi/test/GenericTest.java
+++ 
b/generic/src/test/java/org/netbeans/html/presenters/spi/test/GenericTest.java
@@ -53,7 +53,7 @@ public class GenericTest extends JavaScriptTCK {
             }
         });
 
-        List<Object> res = new ArrayList<Object>();
+        List<Object> res = new ArrayList<>();
         Class<? extends Annotation> test = 
             loadClass().getClassLoader().loadClass(KOTest.class.getName()).
             asSubclass(Annotation.class);


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org
For additional commands, e-mail: commits-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to