001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.applemenu;
043:
044: import java.awt.Font;
045: import java.lang.ref.Reference;
046: import java.lang.ref.ReferenceQueue;
047: import java.lang.reflect.Field;
048:
049: // #57664: Random crash on Mac OS X. Apple JDK apparently has a bug in
050: // the native part of the font handling code. Font instances seems to share
051: // native data structures which are released when the instances are finalized.
052: // Because the native data are shared, their premature release causes random
053: // JVM crashes.
054: //
055: // This evil hack forces Font.finalize() not to run. The native font data
056: // are leaked but it's still better than total crash
057:
058: class FontReferenceQueue extends ReferenceQueue {
059: /**
060: * Polls this queue to see if a reference object is available. If one is
061: * available without further delay then it is removed from the queue and
062: * returned. Otherwise this method immediately returns <tt>null</tt>.
063: *
064: * @return A reference object, if one was immediately available,
065: * otherwise <code>null</code>
066: */
067: public Reference poll() {
068: Reference ref;
069:
070: for (;;) {
071: ref = super .poll();
072: if (ref == null)
073: break;
074:
075: Object obj = ref.get();
076: if (!(obj instanceof Font))
077: break;
078: }
079: return ref;
080: }
081:
082: /**
083: * Removes the next reference object in this queue, blocking until either
084: * one becomes available or the given timeout period expires.
085: *
086: * <p> This method does not offer real-time guarantees: It schedules the
087: * timeout as if by invoking the {@link Object#wait(long)} method.
088: *
089: * @param timeout If positive, block for up <code>timeout</code>
090: * milliseconds while waiting for a reference to be
091: * added to this queue. If zero, block indefinitely.
092: *
093: * @return A reference object, if one was available within the specified
094: * timeout period, otherwise <code>null</code>
095: *
096: * @throws IllegalArgumentException
097: * If the value of the timeout argument is negative
098: *
099: * @throws InterruptedException
100: * If the timeout wait is interrupted
101: */
102: public Reference remove(long timeout)
103: throws IllegalArgumentException, InterruptedException {
104: // timeout is not handled correctly, but good enough for our purposes
105:
106: Reference ref;
107:
108: for (;;) {
109: ref = super .remove(timeout);
110: if (ref == null)
111: break;
112:
113: Object obj = ref.get();
114: if (!(obj instanceof Font))
115: break;
116: }
117: return ref;
118: }
119:
120: static void install() {
121: try {
122: Class clzz = Class.forName("java.lang.ref.Finalizer"); // NOI18N
123: Field fld = clzz.getDeclaredField("queue"); // NOI18N
124: fld.setAccessible(true);
125: fld.set(null, new FontReferenceQueue());
126: } catch (NoClassDefFoundError ex) {
127: // ex.printStackTrace();
128: } catch (ClassNotFoundException ex) {
129: // ex.printStackTrace();
130: } catch (Exception ex) {
131: // ex.printStackTrace();
132: }
133:
134: }
135: }
|