KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > applemenu > FontReferenceQueue


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.applemenu;
21
22 import java.awt.Font JavaDoc;
23 import java.lang.ref.Reference JavaDoc;
24 import java.lang.ref.ReferenceQueue JavaDoc;
25 import java.lang.reflect.Field JavaDoc;
26
27 // #57664: Random crash on Mac OS X. Apple JDK apparently has a bug in
28
// the native part of the font handling code. Font instances seems to share
29
// native data structures which are released when the instances are finalized.
30
// Because the native data are shared, their premature release causes random
31
// JVM crashes.
32
//
33
// This evil hack forces Font.finalize() not to run. The native font data
34
// are leaked but it's still better than total crash
35

36 class FontReferenceQueue extends ReferenceQueue JavaDoc {
37     /**
38      * Polls this queue to see if a reference object is available. If one is
39      * available without further delay then it is removed from the queue and
40      * returned. Otherwise this method immediately returns <tt>null</tt>.
41      *
42      * @return A reference object, if one was immediately available,
43      * otherwise <code>null</code>
44      */

45     public Reference JavaDoc poll() {
46         Reference JavaDoc ref;
47         
48         for (;;) {
49             ref = super.poll();
50             if (ref == null)
51                 break;
52             
53             Object JavaDoc obj = ref.get();
54             if (! (obj instanceof Font JavaDoc))
55                 break;
56         }
57         return ref;
58     }
59
60     /**
61      * Removes the next reference object in this queue, blocking until either
62      * one becomes available or the given timeout period expires.
63      *
64      * <p> This method does not offer real-time guarantees: It schedules the
65      * timeout as if by invoking the {@link Object#wait(long)} method.
66      *
67      * @param timeout If positive, block for up <code>timeout</code>
68      * milliseconds while waiting for a reference to be
69      * added to this queue. If zero, block indefinitely.
70      *
71      * @return A reference object, if one was available within the specified
72      * timeout period, otherwise <code>null</code>
73      *
74      * @throws IllegalArgumentException
75      * If the value of the timeout argument is negative
76      *
77      * @throws InterruptedException
78      * If the timeout wait is interrupted
79      */

80     public Reference JavaDoc remove(long timeout) throws IllegalArgumentException JavaDoc, InterruptedException JavaDoc {
81         // timeout is not handled correctly, but good enough for our purposes
82

83         Reference JavaDoc ref;
84         
85         for (;;) {
86             ref = super.remove(timeout);
87             if (ref == null)
88                 break;
89             
90             Object JavaDoc obj = ref.get();
91             if (! (obj instanceof Font JavaDoc))
92                 break;
93         }
94         return ref;
95     }
96         
97     static void install() {
98         try {
99             Class JavaDoc clzz = Class.forName("java.lang.ref.Finalizer"); // NOI18N
100
Field JavaDoc fld = clzz.getDeclaredField("queue"); // NOI18N
101
fld.setAccessible(true);
102             fld.set(null, new FontReferenceQueue());
103         } catch (NoClassDefFoundError JavaDoc ex) {
104             // ex.printStackTrace();
105
} catch (ClassNotFoundException JavaDoc ex) {
106             // ex.printStackTrace();
107
} catch (Exception JavaDoc ex) {
108             // ex.printStackTrace();
109
}
110         
111     }
112 }
113
Popular Tags