Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8273581: Change the mechanism by which JDK loads the platform-specific FontManager class #5517

Closed
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/java.desktop/macosx/classes/sun/font/PlatformFontInfo.java
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package sun.font;

class PlatformFontInfo {

/**
* The method is only to be called via the
* {@code FontManagerFactory.getInstance()} factory method.
*/
static FontManager createFontManager() {
return new CFontManager();
}
}
63 changes: 12 additions & 51 deletions src/java.desktop/share/classes/sun/font/FontManagerFactory.java
Expand Up @@ -25,72 +25,33 @@

package sun.font;

import java.awt.AWTError;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.security.AccessController;
import java.security.PrivilegedAction;

import sun.security.action.GetPropertyAction;


/**
* Factory class used to retrieve a valid FontManager instance for the current
* platform.
*
* A default implementation is given for Linux, Solaris and Windows.
* You can alter the behaviour of the {@link #getInstance()} method by setting
* the {@code sun.font.fontmanager} property. For example:
* {@code sun.font.fontmanager=sun.awt.X11FontManager}
* A default implementation is given for Linux, Mac OS and Windows.
*/
public final class FontManagerFactory {

/** Our singleton instance. */
private static FontManager instance = null;

private static final String DEFAULT_CLASS;
static {
if (FontUtilities.isWindows) {
DEFAULT_CLASS = "sun.awt.Win32FontManager";
} else if (FontUtilities.isMacOSX) {
DEFAULT_CLASS = "sun.font.CFontManager";
} else {
DEFAULT_CLASS = "sun.awt.X11FontManager";
}
}
private static volatile FontManager instance;

/**
* Get a valid FontManager implementation for the current platform.
*
* @return a valid FontManager instance for the current platform
*/
@SuppressWarnings("removal")
public static synchronized FontManager getInstance() {

if (instance != null) {
return instance;
}

AccessController.doPrivileged(new PrivilegedAction<Object>() {

public Object run() {
try {
String fmClassName =
System.getProperty("sun.font.fontmanager",
DEFAULT_CLASS);
ClassLoader cl = ClassLoader.getSystemClassLoader();
Class<?> fmClass = Class.forName(fmClassName, true, cl);
instance =
(FontManager) fmClass.getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException ex) {
throw new InternalError(ex);

public static FontManager getInstance() {

FontManager result = instance;
if (result == null) {
synchronized (FontManagerFactory.class) {
result = instance;
if (result == null) {
instance = result = PlatformFontInfo.createFontManager();
}
return null;
}
});

return instance;
}
return result;
}
}
39 changes: 39 additions & 0 deletions src/java.desktop/unix/classes/sun/font/PlatformFontInfo.java
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package sun.font;

import sun.awt.X11FontManager;

class PlatformFontInfo {

/**
* The method is only to be called via the
* {@code FontManagerFactory.getInstance()} factory method.
*/
static FontManager createFontManager() {
return new X11FontManager();
}
}
28 changes: 2 additions & 26 deletions src/java.desktop/unix/native/libawt/awt/awt_LoadLibrary.c
Expand Up @@ -109,8 +109,6 @@ AWT_OnLoad(JavaVM *vm, void *reserved)
struct utsname name;
JNIEnv *env = (JNIEnv *)JNU_GetEnv(vm, JNI_VERSION_1_2);
void *v;
jstring fmanager = NULL;
jstring fmProp = NULL;

if (awtHandle != NULL) {
/* Avoid several loading attempts */
Expand All @@ -126,29 +124,15 @@ AWT_OnLoad(JavaVM *vm, void *reserved)
p = strrchr(buf, '/');
#endif
/*
* The code below is responsible for:
* 1. Loading appropriate awt library, i.e. libawt_xawt or libawt_headless
* 2. Set the "sun.font.fontmanager" system property.
* The code below is responsible for
* loading appropriate awt library, i.e. libawt_xawt or libawt_headless
*/

fmProp = (*env)->NewStringUTF(env, "sun.font.fontmanager");
CHECK_EXCEPTION_FATAL(env, "Could not allocate font manager property");

#ifdef MACOSX
fmanager = (*env)->NewStringUTF(env, "sun.font.CFontManager");
tk = LWAWT_PATH;
#else
fmanager = (*env)->NewStringUTF(env, "sun.awt.X11FontManager");
tk = XAWT_PATH;
#endif
CHECK_EXCEPTION_FATAL(env, "Could not allocate font manager name");

if (fmanager && fmProp) {
JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "setProperty",
"(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
fmProp, fmanager);
CHECK_EXCEPTION_FATAL(env, "Could not allocate set properties");
}

#ifndef MACOSX
if (AWTIsHeadless()) {
Expand All @@ -161,14 +145,6 @@ AWT_OnLoad(JavaVM *vm, void *reserved)
strncpy(p, tk, MAXPATHLEN-len-1);
#endif

if (fmProp) {
(*env)->DeleteLocalRef(env, fmProp);
}
if (fmanager) {
(*env)->DeleteLocalRef(env, fmanager);
}


#ifndef STATIC_BUILD
jstring jbuf = JNU_NewStringPlatform(env, buf);
CHECK_EXCEPTION_FATAL(env, "Could not allocate library name");
Expand Down
39 changes: 39 additions & 0 deletions src/java.desktop/windows/classes/sun/font/PlatformFontInfo.java
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package sun.font;

import sun.awt.Win32FontManager;

class PlatformFontInfo {

/**
* The method is only to be called via the
* {@code FontManagerFactory.getInstance()} factory method.
*/
static FontManager createFontManager() {
return new Win32FontManager();
}
}
46 changes: 46 additions & 0 deletions test/jdk/sun/awt/font/CheckFontManagerSystemProperty.java
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import java.awt.Toolkit;

/**
* @test
* @bug 8273581
* @summary verify the "sun.font.fontmanager" system property is not set
* @run main/othervm -Djava.awt.headless=true CheckFontManagerSystemProperty
*/

public class CheckFontManagerSystemProperty {

public static void main(String[] args) {
// force AWT library loading
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the headful or headless libraries?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added -Djava.awt.headless=true property to run the test in the headless mode.

Toolkit toolkit = Toolkit.getDefaultToolkit();
if (toolkit == null) {
throw new RuntimeException("Toolkit not found!");
}
String tkProp = System.getProperty("sun.font.fontmanager");
if (tkProp != null) {
throw new RuntimeException("tkProp = " + tkProp);
}
}
}