From d38c09c572d8f066486684504092bee7b6c11af2 Mon Sep 17 00:00:00 2001 From: hollingsworthd Date: Fri, 22 Jan 2016 02:02:17 -0500 Subject: [PATCH] Only initialize JavaFX in child procs. Fixes #22 --- .../jbrowserdriver/JBrowserDriverServer.java | 55 +++++++++++++++++-- .../jbrowserdriver/Settings.java | 48 ---------------- 2 files changed, 51 insertions(+), 52 deletions(-) diff --git a/src/com/machinepublishers/jbrowserdriver/JBrowserDriverServer.java b/src/com/machinepublishers/jbrowserdriver/JBrowserDriverServer.java index 379a66e4..3dc48f59 100644 --- a/src/com/machinepublishers/jbrowserdriver/JBrowserDriverServer.java +++ b/src/com/machinepublishers/jbrowserdriver/JBrowserDriverServer.java @@ -20,6 +20,9 @@ package com.machinepublishers.jbrowserdriver; import java.io.File; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.net.URL; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; @@ -52,7 +55,7 @@ import com.sun.javafx.webkit.Accessor; import com.sun.webkit.WebPage; -import javafx.application.Platform; +import javafx.embed.swing.JFXPanel; class JBrowserDriverServer extends UnicastRemoteObject implements JBrowserDriverRemote, WebDriver, JavascriptExecutor, FindsById, FindsByClassName, FindsByLinkText, FindsByName, @@ -65,6 +68,53 @@ class JBrowserDriverServer extends UnicastRemoteObject implements JBrowserDriver * RMI entry point. */ public static void main(String[] args) { + if (Settings.headless()) { + System.setProperty("glass.platform", "Monocle"); + System.setProperty("monocle.platform", "Headless"); + System.setProperty("prism.order", "sw"); + System.setProperty("prism.subpixeltext", "false"); + System.setProperty("prism.allowhidpi", "false"); + System.setProperty("prism.text", "t2k"); + try { + Class platformFactory = Class.forName("com.sun.glass.ui.PlatformFactory"); + Field field = platformFactory.getDeclaredField("instance"); + field.setAccessible(true); + field.set(platformFactory, Class.forName( + "com.sun.glass.ui.monocle.MonoclePlatformFactory").newInstance()); + + platformFactory = Class.forName("com.sun.glass.ui.monocle.NativePlatformFactory"); + field = platformFactory.getDeclaredField("platform"); + field.setAccessible(true); + Constructor headlessPlatform = Class.forName("com.sun.glass.ui.monocle.HeadlessPlatform").getDeclaredConstructor(); + headlessPlatform.setAccessible(true); + field.set(platformFactory, headlessPlatform.newInstance()); + } catch (Throwable t) { + Logs.fatal(t); + } + } else { + new JFXPanel(); + } + try { + URL.setURLStreamHandlerFactory(new StreamHandler()); + } catch (Throwable t) { + Field factory = null; + try { + factory = URL.class.getDeclaredField("factory"); + factory.setAccessible(true); + Object curFac = factory.get(null); + + //assume we're in the Eclipse jar-in-jar loader + Field chainedFactory = curFac.getClass().getDeclaredField("chainFac"); + chainedFactory.setAccessible(true); + chainedFactory.set(curFac, new StreamHandler()); + } catch (Throwable t2) { + try { + //this should work regardless + factory.set(null, new StreamHandler()); + } catch (Throwable t3) {} + } + } + final int port = Integer.parseInt(args[0]); Registry registryTmp = null; try { @@ -500,9 +550,6 @@ public Object perform() { Accessor.getPageFor(item.engine.get()).stop(); } SettingsManager.register(null); - if (Settings.headless()) { - Platform.exit(); - } StatusMonitor.instance().clearStatusMonitor(); } diff --git a/src/com/machinepublishers/jbrowserdriver/Settings.java b/src/com/machinepublishers/jbrowserdriver/Settings.java index cb17a784..d022b347 100644 --- a/src/com/machinepublishers/jbrowserdriver/Settings.java +++ b/src/com/machinepublishers/jbrowserdriver/Settings.java @@ -21,9 +21,6 @@ import java.io.File; import java.io.Serializable; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.net.URL; import java.nio.file.Files; import java.util.Random; import java.util.regex.Matcher; @@ -34,8 +31,6 @@ import com.machinepublishers.jbrowserdriver.StreamInjectors.Injector; -import javafx.embed.swing.JFXPanel; - /** * An immutable class which contains settings for the browser. * @@ -88,51 +83,8 @@ public class Settings implements Serializable { static { if (!"true".equals(System.getProperty("jbd.browsergui"))) { headless = true; - System.setProperty("glass.platform", "Monocle"); - System.setProperty("monocle.platform", "Headless"); - System.setProperty("prism.order", "sw"); - System.setProperty("prism.subpixeltext", "false"); - System.setProperty("prism.allowhidpi", "false"); - System.setProperty("prism.text", "t2k"); - try { - Class platformFactory = Class.forName("com.sun.glass.ui.PlatformFactory"); - Field field = platformFactory.getDeclaredField("instance"); - field.setAccessible(true); - field.set(platformFactory, Class.forName( - "com.sun.glass.ui.monocle.MonoclePlatformFactory").newInstance()); - - platformFactory = Class.forName("com.sun.glass.ui.monocle.NativePlatformFactory"); - field = platformFactory.getDeclaredField("platform"); - field.setAccessible(true); - Constructor headlessPlatform = Class.forName("com.sun.glass.ui.monocle.HeadlessPlatform").getDeclaredConstructor(); - headlessPlatform.setAccessible(true); - field.set(platformFactory, headlessPlatform.newInstance()); - } catch (Throwable t) { - Logs.fatal(t); - } } else { headless = false; - new JFXPanel(); - } - try { - URL.setURLStreamHandlerFactory(new StreamHandler()); - } catch (Throwable t) { - Field factory = null; - try { - factory = URL.class.getDeclaredField("factory"); - factory.setAccessible(true); - Object curFac = factory.get(null); - - //assume we're in the Eclipse jar-in-jar loader - Field chainedFactory = curFac.getClass().getDeclaredField("chainFac"); - chainedFactory.setAccessible(true); - chainedFactory.set(curFac, new StreamHandler()); - } catch (Throwable t2) { - try { - //this should work regardless - factory.set(null, new StreamHandler()); - } catch (Throwable t3) {} - } } final Pattern head = Pattern.compile("]*>", Pattern.CASE_INSENSITIVE); final Pattern html = Pattern.compile("]*>", Pattern.CASE_INSENSITIVE);