Skip to content

Commit

Permalink
Only initialize JavaFX in child procs. Fixes #22
Browse files Browse the repository at this point in the history
  • Loading branch information
hollingsworthd committed Jan 22, 2016
1 parent cb62a70 commit d38c09c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 52 deletions.
55 changes: 51 additions & 4 deletions src/com/machinepublishers/jbrowserdriver/JBrowserDriverServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -500,9 +550,6 @@ public Object perform() {
Accessor.getPageFor(item.engine.get()).stop();
}
SettingsManager.register(null);
if (Settings.headless()) {
Platform.exit();
}
StatusMonitor.instance().clearStatusMonitor();
}

Expand Down
48 changes: 0 additions & 48 deletions src/com/machinepublishers/jbrowserdriver/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,8 +31,6 @@

import com.machinepublishers.jbrowserdriver.StreamInjectors.Injector;

import javafx.embed.swing.JFXPanel;

/**
* An immutable class which contains settings for the browser.
*
Expand Down Expand Up @@ -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("<head\\b[^>]*>", Pattern.CASE_INSENSITIVE);
final Pattern html = Pattern.compile("<html\\b[^>]*>", Pattern.CASE_INSENSITIVE);
Expand Down

0 comments on commit d38c09c

Please sign in to comment.