Skip to content

Commit

Permalink
bug-fix plugin loader
Browse files Browse the repository at this point in the history
  • Loading branch information
olir committed Mar 30, 2018
1 parent f577c6a commit 6574736
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 67 deletions.
80 changes: 34 additions & 46 deletions app/src/main/java/de/serviceflow/frankenstein/fxml/FxMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,38 @@ public List<SegmentVideoFilter> getLocalFilters() {
return segmentFilters;
}


private SegmentVideoFilter loadExternalFilterInstance(String filterClassName, String ref) {
try {
// use dynamic loading and reflection when loading jni proxy class
// from jar, so app do not depend on it.
System.out.println("loading " + filterClassName + " from " + ref + " ...");
URLClassLoader childLoader = getLoader(ref);
Class filterClass = Class.forName(filterClassName, true, childLoader);
SegmentVideoFilter filter = (SegmentVideoFilter) filterClass.newInstance();
return filter;
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | MalformedURLException
| SecurityException | IllegalArgumentException /* | InvocationTargetException | NoSuchMethodException */ e) {
throw new RuntimeException(
"Failed loading filter " + filterClassName + " from " + ref,
e);
}
}

static Map<String, URLClassLoader> loaders = new HashMap<String, URLClassLoader>();

static synchronized URLClassLoader getLoader(String ref) throws MalformedURLException {
ref = ref.intern();
URLClassLoader loader = loaders.get(ref);
if (loader == null) {
URL[] urls = new URL[] { new URL(ref) };
loader = new URLClassLoader(urls, FxMain.class.getClassLoader());
loaders.put(ref, loader);
}
return loader;
}


public void createSegmentFilters() {
segmentFilters = new ArrayList<SegmentVideoFilter>();

Expand Down Expand Up @@ -246,8 +278,8 @@ public void createSegmentFilters() {
// "de.serviceflow.frankenstein.plugin.opencv.NativeExampleFilter", pluginOpenCVRef));
segmentFilters.add(loadExternalFilterInstance(
"de.serviceflow.frankenstein.plugin.opencv.VideoEqualizerFilter", pluginOpenCVRef));
segmentFilters.add(loadExternalFilterInstance(
"de.serviceflow.frankenstein.plugin.opencv.ExternalSampleFilter", pluginOpenCVRef));
// segmentFilters.add(loadExternalFilterInstance(
// "de.serviceflow.frankenstein.plugin.opencv.ExternalSampleFilter", pluginOpenCVRef));
segmentFilters.add(loadExternalFilterInstance("de.serviceflow.frankenstein.plugin.jogamp.GLExampleFilter",
pluginJogAmpRef));
} catch (Throwable t) {
Expand All @@ -256,48 +288,4 @@ public void createSegmentFilters() {

}

private SegmentVideoFilter loadExternalFilterInstance(String filterClassName, String ref) {
try {
// use dynamic loading and reflection when loading jni proxy class
// from jar, so app do not depend on it.
System.out.println("loading " + filterClassName + " from " + ref + " ...");
URLClassLoader childLoader = getLoader(ref);
Class filterClass = Class.forName(filterClassName, true, childLoader);
SegmentVideoFilter filter = (SegmentVideoFilter) filterClass.newInstance();
// Method filterInitMethod = filterClass.getMethod("init");
// filterInitMethod.invoke(filter);
return filter;
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | MalformedURLException
| SecurityException | IllegalArgumentException /* | InvocationTargetException | NoSuchMethodException */ e) {
throw new RuntimeException(
"Failed loading filter " + filterClassName + " from " + ref,
e);
}
}

static Map<String, URLClassLoader> loaders = new HashMap<String, URLClassLoader>();

static synchronized URLClassLoader getLoader(String ref) throws MalformedURLException {
ref = ref.intern();
URLClassLoader loader = loaders.get(ref);
if (loader == null) {
URL[] urls = new URL[] { new URL(ref) };
loader = new URLClassLoader(urls, FxMain.class.getClassLoader());
loaders.put(ref, loader);
}
return loader;
}

/*
* static URLClassLoader loader = null;
*
* static synchronized URLClassLoader getLoader() throws
* MalformedURLException { if (loader == null) { final String
* RELEATIVE_TO_MAVEN_EXEC_CWD = "../../../target"; String pluginpath =
* System.getProperty("pluginpath", RELEATIVE_TO_MAVEN_EXEC_CWD); File myJar
* = new File(new File(pluginpath), "plugin-opencv-0.3.1-SNAPSHOT.jar");
* URL[] urls = new URL[] { myJar.toURI().toURL() }; loader = new
* URLClassLoader(urls, NativeSegmentFilter.class.getClassLoader()); }
* return loader; }
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import de.serviceflow.frankenstein.plugin.api.DefaultSegmentFilter;
import de.serviceflow.frankenstein.plugin.api.FilterContext;
import de.serviceflow.frankenstein.plugin.api.SegmentConfigController;

public class BWFilter extends DefaultSegmentFilter {

Expand All @@ -44,4 +45,9 @@ public Mat process(Mat sourceFrame, int frameId, FilterContext context) {
protected void initializeController() {
// getConfigController(). ...
}

@Override
protected SegmentConfigController instantiateController() {
return new BWConfigController();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import de.serviceflow.frankenstein.plugin.api.DefaultSegmentFilter;
import de.serviceflow.frankenstein.plugin.api.FilterContext;
import de.serviceflow.frankenstein.plugin.api.SegmentConfigController;

public class StereoDistanceFilter extends DefaultSegmentFilter {

Expand Down Expand Up @@ -77,4 +78,9 @@ protected void initializeController() {
((StereoDistanceConfigController)getConfigController()).initialize();
}

@Override
protected SegmentConfigController instantiateController() {
return new StereoDistanceConfigController();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.serviceflow.frankenstein.vf.segment.BWConfigController">
<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<center>
</center>
<center>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.serviceflow.frankenstein.vf.segment.StereoDistanceConfigController">
<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<center>
</center>
<center>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,23 @@ public final SegmentVideoFilter createInstance() {

@Override
public final Scene createConfigurationScene(String stylesheet) {
FXMLLoader loader = new FXMLLoader(getClass().getResource(identifier + ".fxml"), bundleConfiguration);
FXMLLoader loader = new FXMLLoader(this.getClass().getResource(identifier + ".fxml"), bundleConfiguration);
configController = instantiateController();
loader.setController(configController);
try {
loader.load();
} catch (IOException e) {
throw new RuntimeException("Failed to create configuration scene for video filter '" + this + "'", e);
}
Scene scene = new Scene(loader.getRoot());
scene.getStylesheets().add(stylesheet);
configController = loader.getController();
// configController = loader.getController();
initializeController(); // custom initialization possible here
return scene;
}

abstract protected SegmentConfigController instantiateController();

abstract protected void initializeController();

public SegmentConfigController getConfigController() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,36 @@
import java.util.Set;

public abstract class NativeFilter {
private static boolean loaderCalled = false;
private static UnsatisfiedLinkError error = null;
// private static boolean loaderCalled = false;
// private static UnsatisfiedLinkError error = null;
private static final Set<String> loadedLibraries = Collections.synchronizedSet(new HashSet<String>());

public static void prepareLoadLibrary(NativeFilter invoker) throws UnsatisfiedLinkError {
if (!loaderCalled) {
loaderCalled = true;
// if (!loaderCalled) {
// loaderCalled = true;
System.out.println("Working Directory = " + new File(".").getAbsolutePath());
try {
String packageName = invoker.getClass().getPackage().getName();
String pluginName = packageName.substring(packageName.lastIndexOf(".plugin.")+1);
pluginName = pluginName.substring(0, pluginName.indexOf('.', pluginName.indexOf('.')+1)).replace('.', '-');
if (System.getProperty("os.arch").contains("64")
&& System.getProperty("sun.arch.data.model").contains("64")) {
// load 64-bit lib
prepareLoadLibrary(invoker, "jniplugin-64");
prepareLoadLibrary(invoker, pluginName+"-64");
} else {
// load 32-bit lib
prepareLoadLibrary(invoker, "jniplugin-32");
prepareLoadLibrary(invoker, pluginName+"-32");
}
} catch (UnsatisfiedLinkError t) {
System.out.println("sun.arch.data.model=" + System.getProperty("sun.arch.data.model"));
System.out.println("os.arch=" + System.getProperty("os.arch"));

error = t;
// error = t;
throw t;
}
if (error != null)
throw error; // throw again
}
// if (error != null)
// throw error; // throw again
// }
}

private static void prepareLoadLibrary(NativeFilter invoker, String libraryName) throws UnsatisfiedLinkError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import de.serviceflow.frankenstein.plugin.api.FilterContext;
import de.serviceflow.frankenstein.plugin.api.NativeSegmentFilter;
import de.serviceflow.frankenstein.plugin.api.SegmentConfigController;


public class GLExampleFilter extends NativeSegmentFilter<GLExampleConfigController> {

Expand Down Expand Up @@ -52,6 +54,11 @@ public GLExampleFilter() {
*/
}

@Override
protected SegmentConfigController instantiateController() {
return new GLExampleConfigController();
}

@Override
public Mat process(Mat sourceFrame, int frameId, FilterContext context) {
// System.out.println("c = "+sourceFrame.cols()+" r =
Expand Down Expand Up @@ -121,4 +128,6 @@ private BufferedImage render(GLAutoDrawable drawable, int width, int height, int
return im;
}
*/


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.serviceflow.frankenstein.vf.segment.GLExampleConfigController">
<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<center>
</center>
<center>
Expand Down
4 changes: 2 additions & 2 deletions plugin-jogamp/native/win64/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>

<groupId>de.serviceflow.frankenstein.plugin.jogamp</groupId>
<artifactId>jniplugin-64</artifactId>
<artifactId>plugin-jogamp-64</artifactId>
<version>0.3.4-SNAPSHOT</version>
<packaging>dll</packaging>

Expand Down Expand Up @@ -52,7 +52,7 @@
<compilerStartOption>-m64 -I..\include</compilerStartOption>
</compilerStartOptions>
<linkerStartOptions>
<linkerStartOption>-shared -m64 -Wl,--kill-at,--add-stdcall-alias -static-libgcc -static-libstdc++</linkerStartOption>
<linkerStartOption>-shared -m64 -Wl,--kill-at,--add-stdcall-alias -static</linkerStartOption><!-- -static-libgcc -static-libstdc++ -lwinpthread -->
</linkerStartOptions>

</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import de.serviceflow.frankenstein.plugin.api.FilterContext;
import de.serviceflow.frankenstein.plugin.api.NativeSegmentFilter;
import de.serviceflow.frankenstein.plugin.api.SegmentConfigController;

public class ExternalSampleFilter extends NativeSegmentFilter<ExternalSampleConfigController> {

Expand All @@ -41,6 +42,11 @@ public ExternalSampleFilter() throws UnsatisfiedLinkError {
}
}

@Override
protected SegmentConfigController instantiateController() {
return new ExternalSampleConfigController();
}

@Override
public Mat process(Mat rgbaImage, int frameId, FilterContext context) {
Imgproc.cvtColor(rgbaImage, mHsvMat, Imgproc.COLOR_RGB2HSV_FULL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import de.serviceflow.frankenstein.plugin.api.FilterContext;
import de.serviceflow.frankenstein.plugin.api.NativeSegmentFilter;
import de.serviceflow.frankenstein.plugin.api.SegmentConfigController;

public class NativeExampleFilter extends NativeSegmentFilter<NativeExampleConfigController> {

Expand All @@ -39,6 +40,11 @@ public NativeExampleFilter() throws UnsatisfiedLinkError {
}
}

@Override
protected SegmentConfigController instantiateController() {
return new NativeExampleConfigController();
}

@Override
public Mat process(Mat sourceFrame, int frameId, FilterContext context) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import de.serviceflow.frankenstein.plugin.api.FilterContext;
import de.serviceflow.frankenstein.plugin.api.NativeSegmentFilter;
import de.serviceflow.frankenstein.plugin.api.SegmentConfigController;

public class VideoEqualizerFilter extends NativeSegmentFilter<VideoEqualizerConfigController> {

Expand All @@ -42,6 +43,11 @@ public VideoEqualizerFilter() throws UnsatisfiedLinkError {
}
}

@Override
protected SegmentConfigController instantiateController() {
return new VideoEqualizerConfigController();
}

@Override
public Mat process(Mat rgbaImage, int frameId, FilterContext context) {
Imgproc.cvtColor(rgbaImage, mHsvMat, Imgproc.COLOR_RGB2HSV_FULL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.serviceflow.frankenstein.vf.external.ExternalSampleConfigController">
<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">

</BorderPane>
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.serviceflow.frankenstein.vf.segment.NativeExampleConfigController">
<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
</BorderPane>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.serviceflow.frankenstein.vf.segment.VideoEqualizerConfigController">
<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<top>
<GridPane BorderPane.alignment="CENTER">
<columnConstraints>
Expand Down
2 changes: 1 addition & 1 deletion plugin-opencv/native/win64/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>

<groupId>de.serviceflow.frankenstein.plugin.opencv</groupId>
<artifactId>jniplugin-64</artifactId>
<artifactId>plugin-opencv-64</artifactId>
<version>0.3.4-SNAPSHOT</version>
<packaging>dll</packaging>

Expand Down

0 comments on commit 6574736

Please sign in to comment.