[java-opensaml] 03/03: OSJ-298: Replace use of finalize() in X509Certificate and X509CRL impls

Brent Putman putmanb at georgetown.edu
Wed Feb 19 17:18:36 EST 2020


This is an automated email from the git hooks/post-receive script.

putmanb pushed a commit to branch master
in repository java-opensaml.

View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=2677c2bda7abf824458752e071a8adc2df19f16d

commit 2677c2bda7abf824458752e071a8adc2df19f16d
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Fri Feb 7 22:36:35 2020 -0500

    OSJ-298: Replace use of finalize() in X509Certificate and X509CRL impls
---
 .../xmlsec/signature/impl/X509CRLImpl.java         | 55 +++++++++++++++++----
 .../xmlsec/signature/impl/X509CertificateImpl.java | 56 ++++++++++++++++++----
 2 files changed, 91 insertions(+), 20 deletions(-)

diff --git a/opensaml-xmlsec-impl/src/main/java/org/opensaml/xmlsec/signature/impl/X509CRLImpl.java b/opensaml-xmlsec-impl/src/main/java/org/opensaml/xmlsec/signature/impl/X509CRLImpl.java
index f9a02b8..10c9074 100644
--- a/opensaml-xmlsec-impl/src/main/java/org/opensaml/xmlsec/signature/impl/X509CRLImpl.java
+++ b/opensaml-xmlsec-impl/src/main/java/org/opensaml/xmlsec/signature/impl/X509CRLImpl.java
@@ -17,11 +17,16 @@
 
 package org.opensaml.xmlsec.signature.impl;
 
+import java.lang.ref.Cleaner;
+import java.lang.ref.Cleaner.Cleanable;
 import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
 
+import javax.annotation.Nonnull;
+
 import net.shibboleth.utilities.java.support.collection.IndexingObjectStore;
+import net.shibboleth.utilities.java.support.primitive.CleanerSupport;
 
 import org.opensaml.core.xml.AbstractXMLObject;
 import org.opensaml.core.xml.XMLObject;
@@ -33,6 +38,13 @@ public class X509CRLImpl extends AbstractXMLObject implements X509CRL {
     /** Class-level index of Base64 encoded CRL values. */
     private static final IndexingObjectStore<String> B64_CRL_STORE = new IndexingObjectStore<>();
 
+    /** The {@link Cleaner} instance to use. */
+    private static final Cleaner CLEANER = CleanerSupport.getInstance(X509CRLImpl.class);
+
+    /** The {@link Cleanable} representing the current instance's CRL value, as represented by the
+     * current <code>b64CRLIndex</code> field value. */
+    private Cleaner.Cleanable cleanable;
+
     /** Index to a stored Base64 encoded CRL. */
     private String b64CRLIndex;
 
@@ -55,13 +67,19 @@ public class X509CRLImpl extends AbstractXMLObject implements X509CRL {
     /** {@inheritDoc} */
     public void setValue(final String newValue) {
         // Dump our cached DOM if the new value really is new
-        final String currentCert = B64_CRL_STORE.get(b64CRLIndex);
-        final String b64Cert = prepareForAssignment(currentCert, newValue);
+        final String currentCRL = B64_CRL_STORE.get(b64CRLIndex);
+        final String newCRL = prepareForAssignment(currentCRL, newValue);
 
         // This is a new value, remove the old one, add the new one
-        if (!Objects.equals(currentCert, b64Cert)) {
-            B64_CRL_STORE.remove(b64CRLIndex);
-            b64CRLIndex = B64_CRL_STORE.put(b64Cert);
+        if (!Objects.equals(currentCRL, newCRL)) {
+            if (cleanable != null) {
+                cleanable.clean();
+                cleanable = null;
+            }
+            b64CRLIndex = B64_CRL_STORE.put(newCRL);
+            if (b64CRLIndex != null) {
+                cleanable = CLEANER.register(this, new CleanerState(b64CRLIndex));
+            }
         }
     }
 
@@ -71,10 +89,27 @@ public class X509CRLImpl extends AbstractXMLObject implements X509CRL {
         return Collections.emptyList();
     }
 
-    /** {@inheritDoc} */
-    @Override
-    protected void finalize() throws Throwable {
-        super.finalize();
-        B64_CRL_STORE.remove(b64CRLIndex);
+    /**
+     * The action to be taken when the current state must be cleaned.
+     */
+    static class CleanerState implements Runnable {
+
+        /** The index to remove from the store. */
+        private String index;
+
+        /**
+         * Constructor.
+         *
+         * @param idx the index in the {@link X509CertificateImpl#B64_CERT_STORE}.
+         */
+        public CleanerState(@Nonnull final String idx) {
+            index = idx;
+        }
+
+        /** {@inheritDoc} */
+        public void run() {
+            X509CRLImpl.B64_CRL_STORE.remove(index);
+        }
+
     }
 }
\ No newline at end of file
diff --git a/opensaml-xmlsec-impl/src/main/java/org/opensaml/xmlsec/signature/impl/X509CertificateImpl.java b/opensaml-xmlsec-impl/src/main/java/org/opensaml/xmlsec/signature/impl/X509CertificateImpl.java
index 9f65e36..2ae6ce8 100644
--- a/opensaml-xmlsec-impl/src/main/java/org/opensaml/xmlsec/signature/impl/X509CertificateImpl.java
+++ b/opensaml-xmlsec-impl/src/main/java/org/opensaml/xmlsec/signature/impl/X509CertificateImpl.java
@@ -17,22 +17,34 @@
 
 package org.opensaml.xmlsec.signature.impl;
 
+import java.lang.ref.Cleaner;
+import java.lang.ref.Cleaner.Cleanable;
 import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
 
-import net.shibboleth.utilities.java.support.collection.IndexingObjectStore;
+import javax.annotation.Nonnull;
 
 import org.opensaml.core.xml.AbstractXMLObject;
 import org.opensaml.core.xml.XMLObject;
 import org.opensaml.xmlsec.signature.X509Certificate;
 
+import net.shibboleth.utilities.java.support.collection.IndexingObjectStore;
+import net.shibboleth.utilities.java.support.primitive.CleanerSupport;
+
 /** Concrete implementation of {@link X509Certificate}. */
 public class X509CertificateImpl extends AbstractXMLObject implements X509Certificate {
 
     /** Class-level index of Base64 encoded cert values. */
     private static final IndexingObjectStore<String> B64_CERT_STORE = new IndexingObjectStore<>();
 
+    /** The {@link Cleaner} instance to use. */
+    private static final Cleaner CLEANER = CleanerSupport.getInstance(X509CertificateImpl.class);
+
+    /** The {@link Cleanable} representing the current instance's cert value, as represented by the
+     * current <code>b64CertIndex</code> field value. */
+    private Cleaner.Cleanable cleanable;
+
     /** Index to a stored Base64 encoded cert. */
     private String b64CertIndex;
 
@@ -59,12 +71,18 @@ public class X509CertificateImpl extends AbstractXMLObject implements X509Certif
     public void setValue(final String newValue) {
         // Dump our cached DOM if the new value really is new
         final String currentCert = B64_CERT_STORE.get(b64CertIndex);
-        final String b64Cert = prepareForAssignment(currentCert, newValue);
+        final String newCert = prepareForAssignment(currentCert, newValue);
 
         // This is a new value, remove the old one, add the new one
-        if (!Objects.equals(currentCert, b64Cert)) {
-            B64_CERT_STORE.remove(b64CertIndex);
-            b64CertIndex = B64_CERT_STORE.put(b64Cert);
+        if (!Objects.equals(currentCert, newCert)) {
+            if (cleanable != null) {
+                cleanable.clean();
+                cleanable = null;
+            }
+            b64CertIndex = B64_CERT_STORE.put(newCert);
+            if (b64CertIndex != null) {
+                cleanable = CLEANER.register(this, new CleanerState(b64CertIndex));
+            }
         }
     }
 
@@ -74,10 +92,28 @@ public class X509CertificateImpl extends AbstractXMLObject implements X509Certif
         return Collections.emptyList();
     }
     
-    /** {@inheritDoc} */
-    @Override
-    protected void finalize() throws Throwable {
-        super.finalize();
-        B64_CERT_STORE.remove(b64CertIndex);
+    /**
+     * The action to be taken when the current state must be cleaned.
+     */
+    static class CleanerState implements Runnable {
+
+        /** The index to remove from the store. */
+        private String index;
+
+        /**
+         * Constructor.
+         *
+         * @param idx the index in the {@link X509CertificateImpl#B64_CERT_STORE}.
+         */
+        public CleanerState(@Nonnull final String idx) {
+            index = idx;
+        }
+
+        /** {@inheritDoc} */
+        public void run() {
+            X509CertificateImpl.B64_CERT_STORE.remove(index);
+        }
+
     }
+
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list