Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

osgi - Test Apache Felix with Java 8 and JavaFX

I'm working on example with JavaFX 2.2 and Java 8. I created this simple Apache Felix Activator:

Activator:

import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator
{

    Stage stage;

    @Override
    public void start(BundleContext context) throws Exception
    {

        Platform.runLater(new Runnable()
        {
            @Override
            public void run()
            {
                stage = new Stage();
                BorderPane pane = new BorderPane();
                Scene scene = new Scene(pane, 400, 200);
                pane.setCenter(new Label("This is a JavaFX Scene in a Stage"));
                stage.setScene(scene);
                stage.show();
            }
        });
        System.out.println("Main Module is loaded!");

    }

    @Override
    public void stop(BundleContext context) throws Exception
    {
        Platform.runLater(new Runnable()
        {
            @Override
            public void run()
            {
                stage.close();
            }
        });
        System.out.println("Main Module is unloaded!");
    }
}

POM

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>DX-57</groupId>
    <artifactId>DX-57_Main</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>bundle</packaging>

    <name>DX-57_Main OSGi Bundle</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>5.0.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.3.7</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Import-Package>*</Import-Package>
                        <Bundle-Activator>dx57.dx._main.Activator</Bundle-Activator>
                        <Export-Package>*</Export-Package>
                    </instructions>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>build-for-felix</id>
            <dependencies>
                <dependency>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>org.apache.felix.main</artifactId>
                    <version>4.2.1</version>
                    <scope>provided</scope>
                </dependency>
                <!-- To include a shell:
                <dependency>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>org.apache.felix.gogo.shell</artifactId>
                    <version>0.10.0</version>
                </dependency>
                -->
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.7</version>
                        <executions>
                            <execution>
                                <id>compile</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <target>
                                        <pathconvert property="plugins.jars" pathsep="${path.separator}">
                                            <path refid="maven.runtime.classpath"/>
                                            <map from="${project.build.directory}${file.separator}classes" to=""/>
                                        </pathconvert>
                                        <pathconvert pathsep=" " property="bundles">
                                            <path path="${plugins.jars}"/>
                                            <mapper>
                                                <chainedmapper>
                                                    <flattenmapper/>
                                                    <globmapper from="*" to="file:modules/*" casesensitive="no"/>
                                                </chainedmapper>
                                            </mapper>
                                        </pathconvert>
                                        <propertyfile file="${project.build.directory}/config.properties">
                                            <entry key="felix.auto.start" value="${bundles} file:modules/${project.build.finalName}.jar"/>
                                            <entry key="org.osgi.framework.bootdelegation" value="*"/>
                                        </propertyfile>

                                        <copy file="${maven.dependency.org.apache.felix.org.apache.felix.main.jar.path}" tofile="${project.build.directory}/felix.jar"/>
                                    </target>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <version>2.4</version>
                        <executions>
                            <execution>
                                <id>create-executable-jar</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                                <configuration>
                                    <descriptors>
                                        <descriptor>${basedir}/src/main/assembly/felix.xml</descriptor>
                                    </descriptors>
                                    <finalName>${project.build.finalName}</finalName>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>run-on-felix</id>
            <dependencies>
                <dependency>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>org.apache.felix.main</artifactId>
                    <version>4.2.1</version>
                    <scope>provided</scope>
                </dependency>
                <!-- org.apache.felix:org.apache.felix.gogo.shell:0.6.1 useless from Maven since stdin is swallowed -->
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.7</version>
                        <configuration>
                            <target>
                                <property name="vm.args" value=""/>
                                <pathconvert property="plugins.jars" pathsep="${path.separator}">
                                    <path refid="maven.runtime.classpath"/>
                                    <map from="${project.build.directory}${file.separator}classes" to=""/>
                                </pathconvert>
                                <makeurl property="urls" separator=" ">
                                    <path path="${plugins.jars}"/>
                                    <path location="${project.build.directory}/${project.build.finalName}.jar"/>
                                </makeurl>
                                <propertyfile file="${project.build.directory}/run.properties">
                                    <entry key="felix.auto.start" value="${urls}"/>
                                    <entry key="felix.auto.deploy.action" value="uninstall,install,update,start"/>
                                    <entry key="org.osgi.framework.storage" value="${project.build.directory}${file.separator}felix-cache"/>
                                    <entry key="org.osgi.framework.bootdelegation" value="*"/>
                                </propertyfile>
                                <makeurl property="run.properties.url" file="${project.build.directory}/run.properties"/>
                                <java fork="true" jar="${maven.dependency.org.apache.felix.org.apache.felix.main.jar.path}">
                                    <sysproperty key="felix.config.properties" value="${run.properties.url}"/>
                                    <jvmarg line="${vm.args}"/>
                                </java>
                            </target>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

Error:

[rcbandit@Laptop felix-framework_JavaFX]$ /opt/jdk1.8.0/bin/java -jar bin/felix.jar
ERROR: Bund

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

JavaFX applications depend on packages from the JavaFX APIs, such as javafx.application and several others. Since it looks like you have built your bundle with Maven Bundle Plugin, your bundle already has these dependencies declared. This is a good thing.

In Java 8 the javafx.* packages are provided by the base JRE. However OSGi does not automatically export every package from the JRE, simply because all JREs have a bunch of non-standard packages (e.g. com.sun.* etc) that normal application code should not have access to. Therefore OSGi only makes available the packages that are defined by the relevant JCP Specification for the version of Java that you are using. For example packages such as javax.swing, org.w3c.dom, etc.

Since JavaFX is not a standard, there is no JCP Specification for JavaFX, and OSGi does not export the javafx.* packages. However you can configure OSGi to do this for you by setting the following configuration property when you launch OSGi:

org.osgi.framework.system.packages.extra=javafx.application,...

NB I have shown how to add the javafx.application package to your runtime. It is likely you will need to add several more, i.e. all of the packages from the JavaFX API. I am not sufficiently familar with JavaFX to list these, but they should be easy enough for you to find.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...