You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@johnzon.apache.org by rm...@apache.org on 2019/08/18 15:42:38 UTC

[johnzon] branch master updated: ensure jsonb instance does not track a lot of references cause it is an anonymous class

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

rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git


The following commit(s) were added to refs/heads/master by this push:
     new 9690e45  ensure jsonb instance does not track a lot of references cause it is an anonymous class
9690e45 is described below

commit 9690e4576e637aa9fe8b832c38bb19a585adf142
Author: Romain Manni-Bucau <rm...@apache.org>
AuthorDate: Sun Aug 18 17:42:29 2019 +0200

    ensure jsonb instance does not track a lot of references cause it is an anonymous class
---
 .../org/apache/johnzon/jsonb/JohnzonBuilder.java   | 24 ++++++++--------------
 .../org/apache/johnzon/jsonb/JohnzonJsonb.java     | 13 ++++++++++--
 2 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JohnzonBuilder.java b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JohnzonBuilder.java
index 03410df..ade815e 100644
--- a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JohnzonBuilder.java
+++ b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JohnzonBuilder.java
@@ -346,22 +346,16 @@ public class JohnzonBuilder implements JsonbBuilder {
         }
         final Mapper mapper = builder.build();
 
-        return useCdi ? new JohnzonJsonb(mapper, ijson) {
-            {
-                cdiIntegration.track(this);
-            }
-
-            @Override
-            public void close() {
-                try {
-                    super.close();
-                } finally {
-                    if (cdiIntegration.isCanWrite()) {
-                        cdiIntegration.untrack(this);
-                    }
+        if (useCdi) {
+            final JohnzonJsonb jsonb = new JohnzonJsonb(mapper, ijson, i -> {
+                if (cdiIntegration.isCanWrite()) {
+                    cdiIntegration.untrack(i);
                 }
-            }
-        } : new JohnzonJsonb(mapper, ijson);
+            });
+            cdiIntegration.track(jsonb);
+            return jsonb;
+        }
+        return new JohnzonJsonb(mapper, ijson, null);
     }
 
     private Boolean toBool(final Object v) {
diff --git a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JohnzonJsonb.java b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JohnzonJsonb.java
index 62a055a..7eae65e 100644
--- a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JohnzonJsonb.java
+++ b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JohnzonJsonb.java
@@ -47,15 +47,18 @@ import java.util.Optional;
 import java.util.OptionalDouble;
 import java.util.OptionalInt;
 import java.util.OptionalLong;
+import java.util.function.Consumer;
 
 // TODO: Optional handling for lists (and arrays)?
 public class JohnzonJsonb implements Jsonb, AutoCloseable, JsonbExtension {
     private final Mapper delegate;
     private final boolean ijson;
+    private final Consumer<JohnzonJsonb> onClose;
 
-    public JohnzonJsonb(final Mapper build, final boolean ijson) {
+    public JohnzonJsonb(final Mapper build, final boolean ijson, final Consumer<JohnzonJsonb> onClose) {
         this.delegate = build;
         this.ijson = ijson;
+        this.onClose = onClose;
     }
 
     @Override
@@ -433,7 +436,13 @@ public class JohnzonJsonb implements Jsonb, AutoCloseable, JsonbExtension {
 
     @Override
     public void close() {
-        delegate.close();
+        try {
+            delegate.close();
+        } finally {
+            if (onClose != null) {
+                onClose.accept(this);
+            }
+        }
     }
 
     @Override