Skip to content

Instantly share code, notes, and snippets.

@mojavelinux
Created July 21, 2015 07:25
Show Gist options
  • Save mojavelinux/593f5a56381507bca46f to your computer and use it in GitHub Desktop.
Save mojavelinux/593f5a56381507bca46f to your computer and use it in GitHub Desktop.
A main class wrapper for deck2pdf that runs the application headless using Monocle from jfxtras.org
package me.champeau.deck2pdf;
import java.lang.reflect.Field;
import java.util.Objects;
import javafx.application.Application;
/**
* Adapted from https://gist.github.com/hastebrot/cbca1128dd791966e3a6
*/
public class HeadlessMain {
//---------------------------------------------------------------------------------------------
// CONSTANTS.
//---------------------------------------------------------------------------------------------
private static final String PROPERTY_JAVAFX_MACOSX_EMBEDDED = "javafx.macosx.embedded";
private static final String PROPERTY_TESTFX_HEADLESS = "javafx.monocle.headless";
private static final String PLATFORM_FACTORY_CLASS =
"com.sun.glass.ui.PlatformFactory";
private static final String PLATFORM_FACTORY_MONOCLE_IMPL =
"com.sun.glass.ui.monocle.MonoclePlatformFactory";
private static final String NATIVE_PLATFORM_FACTORY_CLASS =
"com.sun.glass.ui.monocle.NativePlatformFactory";
private static final String NATIVE_PLATFORM_HEADLESS_IMPL =
"com.sun.glass.ui.monocle.headless.HeadlessPlatform";
//---------------------------------------------------------------------------------------------
// METHODS.
//---------------------------------------------------------------------------------------------
public static void main(String[] args) {
new HeadlessMain().launch(Main.class, args);
}
public void launch(Class<? extends Application> appClass,
String... appArgs) {
initMacosxEmbedded();
initMonocleHeadless();
Application.launch(appClass, appArgs);
}
//---------------------------------------------------------------------------------------------
// PRIVATE METHODS.
//---------------------------------------------------------------------------------------------
private void initMacosxEmbedded() {
if (checkSystemPropertyEquals(PROPERTY_JAVAFX_MACOSX_EMBEDDED, null)) {
System.setProperty(PROPERTY_JAVAFX_MACOSX_EMBEDDED, "true");
}
}
private void initMonocleHeadless() {
if (checkSystemPropertyEquals(PROPERTY_TESTFX_HEADLESS, "true")) {
try {
assignMonoclePlatform();
assignHeadlessPlatform();
}
catch (Exception exception) {
throw new RuntimeException(exception);
}
}
}
private boolean checkSystemPropertyEquals(String propertyName,
String valueOrNull) {
return Objects.equals(System.getProperty(propertyName, null), valueOrNull);
}
private void assignMonoclePlatform()
throws Exception {
Class<?> platformFactoryClass = Class.forName(PLATFORM_FACTORY_CLASS);
Object platformFactoryImpl = Class.forName(PLATFORM_FACTORY_MONOCLE_IMPL).newInstance();
assignPrivateStaticField(platformFactoryClass, "instance", platformFactoryImpl);
}
private void assignHeadlessPlatform()
throws Exception {
Class<?> nativePlatformFactoryClass = Class.forName(NATIVE_PLATFORM_FACTORY_CLASS);
Object nativePlatformImpl = Class.forName(NATIVE_PLATFORM_HEADLESS_IMPL).newInstance();
assignPrivateStaticField(nativePlatformFactoryClass, "platform", nativePlatformImpl);
}
private void assignPrivateStaticField(Class<?> cls,
String name,
Object value)
throws Exception {
Field field = cls.getDeclaredField(name);
field.setAccessible(true);
field.set(cls, value);
field.setAccessible(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment