< prev index next >

modules/web/src/main/java/com/sun/webkit/Disposer.java

Print this page
rev 9728 : 8139317: [Mac] SecurityException when constructing WebView from JFXPanel application
Reviewed-by:


  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * To change this template, choose Tools | Templates
  28  * and open the template in the editor.
  29  */
  30 
  31 package com.sun.webkit;
  32 
  33 import java.lang.ref.ReferenceQueue;
  34 import java.lang.ref.WeakReference;

  35 import java.util.Arrays;
  36 import java.util.Collection;
  37 import java.util.HashSet;
  38 import java.util.Set;
  39 import java.util.concurrent.LinkedBlockingQueue;
  40 
  41 /**
  42  * This class is used for registering and disposing the native
  43  * data associated with java objects.
  44  *
  45  * The object can register itself by calling the addRecord method and
  46  * providing a descendant of the DisposerRecord class with overridden
  47  * dispose() method.
  48  *
  49  * When the object becomes unreachable, the dispose() method
  50  * of the associated DisposerRecord object will be called.
  51  *
  52  * @see DisposerRecord
  53  */
  54 public final class Disposer implements Runnable {
  55     private static final ReferenceQueue queue = new ReferenceQueue();
  56     private static final Disposer disposerInstance = new Disposer();
  57     private static final Set<WeakDisposerRecord> records =
  58             new HashSet<WeakDisposerRecord>();
  59 
  60     static {
  61         Thread t = new Thread(disposerInstance, "Disposer");











  62         t.setDaemon(true);
  63         t.setPriority(Thread.MAX_PRIORITY);
  64         t.start();


  65     }
  66 
  67     /**
  68      * Registers the object and the native data for later disposal.
  69      * @param target Object to be registered
  70      * @param rec the associated DisposerRecord object
  71      * @see DisposerRecord
  72      */
  73     public static void addRecord(Object target, DisposerRecord rec) {
  74         disposerInstance.add(target, rec);
  75     }
  76 
  77     /**
  78      * Performs the actual registration of the target object to be disposed.
  79      * @param target Object to be registered
  80      * @param rec the associated DisposerRecord object
  81      * @see DisposerRecord
  82      */
  83     private synchronized void add(Object target, DisposerRecord rec) {
  84         records.add(new WeakDisposerRecord(target, rec));




  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * To change this template, choose Tools | Templates
  28  * and open the template in the editor.
  29  */
  30 
  31 package com.sun.webkit;
  32 
  33 import java.lang.ref.ReferenceQueue;
  34 import java.lang.ref.WeakReference;
  35 import java.security.PrivilegedAction;
  36 import java.util.Arrays;
  37 import java.util.Collection;
  38 import java.util.HashSet;
  39 import java.util.Set;
  40 import java.util.concurrent.LinkedBlockingQueue;
  41 
  42 /**
  43  * This class is used for registering and disposing the native
  44  * data associated with java objects.
  45  *
  46  * The object can register itself by calling the addRecord method and
  47  * providing a descendant of the DisposerRecord class with overridden
  48  * dispose() method.
  49  *
  50  * When the object becomes unreachable, the dispose() method
  51  * of the associated DisposerRecord object will be called.
  52  *
  53  * @see DisposerRecord
  54  */
  55 public final class Disposer implements Runnable {
  56     private static final ReferenceQueue queue = new ReferenceQueue();
  57     private static final Disposer disposerInstance = new Disposer();
  58     private static final Set<WeakDisposerRecord> records =
  59             new HashSet<WeakDisposerRecord>();
  60 
  61     static {
  62         java.security.AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
  63             /*
  64              * The thread must be a member of a thread group
  65              * which will not get GCed before VM exit.
  66              * Make its parent the top-level thread group.
  67              */
  68             ThreadGroup tg = Thread.currentThread().getThreadGroup();
  69             for (ThreadGroup tgn = tg;
  70                     tgn != null;
  71                     tg = tgn, tgn = tg.getParent());
  72 
  73             Thread t = new Thread(tg, disposerInstance, "Disposer");
  74             t.setDaemon(true);
  75             t.setPriority(Thread.MAX_PRIORITY);
  76             t.start();
  77             return null;
  78         });
  79     }
  80 
  81     /**
  82      * Registers the object and the native data for later disposal.
  83      * @param target Object to be registered
  84      * @param rec the associated DisposerRecord object
  85      * @see DisposerRecord
  86      */
  87     public static void addRecord(Object target, DisposerRecord rec) {
  88         disposerInstance.add(target, rec);
  89     }
  90 
  91     /**
  92      * Performs the actual registration of the target object to be disposed.
  93      * @param target Object to be registered
  94      * @param rec the associated DisposerRecord object
  95      * @see DisposerRecord
  96      */
  97     private synchronized void add(Object target, DisposerRecord rec) {
  98         records.add(new WeakDisposerRecord(target, rec));


< prev index next >