Added starting point of plugin system and example plugin

This commit is contained in:
Brendan Robert 2016-06-26 19:55:26 -05:00
parent de0135b3c6
commit 485776902e
6 changed files with 237 additions and 2 deletions

1
.gitignore vendored
View File

@ -45,3 +45,4 @@ mg
# Packer sometimes produces an error text file.
error_stack.txt
/OutlawEditor/OutlawPluginExample/target/

View File

@ -0,0 +1,166 @@
<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>org.badvision</groupId>
<artifactId>OutlawPluginExample</artifactId>
<version>0.1</version>
<packaging>bundle</packaging>
<name>OutlawPluginExample OSGi Bundle</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.0.1</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>org.badvision.outlaw.plugin.example.*</Export-Package>
<Bundle-Activator>org.badvision.outlaw.plugin.example.Activator</Bundle-Activator>
</instructions>
</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>5.4.0</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.8</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.6</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>5.4.0</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.8</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>
<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>5.4.0</version>
<type>jar</type>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,19 @@
package org.badvision.outlaw.plugin.example;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
/**
*
* @author blurry
*/
public class Activator implements BundleActivator {
public void start(BundleContext bc) throws Exception {
System.out.println("Hello, world!");
}
public void stop(BundleContext bc) throws Exception {
}
}

View File

@ -31,6 +31,7 @@
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<excludes>META-INF/*.SF,META-INF/*.DSA,META-INF/*.RSA</excludes>
<excludeScope>system</excludeScope>
<excludeGroupIds>junit,org.mockito,org.hamcrest</excludeGroupIds>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
@ -130,5 +131,20 @@
<artifactId>jaxb2-basics</artifactId>
<version>0.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>5.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.main</artifactId>
<version>5.4.0</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.0</version>
</dependency>
</dependencies>
</project>

View File

@ -7,19 +7,24 @@
* ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.badvision.outlaweditor;
import java.io.IOException;
import javafx.event.EventHandler;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import org.apache.felix.framework.Felix;
import org.apache.felix.main.AutoProcessor;
import org.badvision.outlaweditor.data.xml.GameData;
import org.badvision.outlaweditor.ui.ApplicationUIController;
import org.osgi.framework.BundleException;
/**
*
@ -35,7 +40,17 @@ public class Application extends javafx.application.Application {
return instance;
}
public static void shutdown() {
try {
instance.pluginContainer.stop();
instance.pluginContainer.waitForStop(0L);
} catch (BundleException | InterruptedException ex) {
Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
}
}
private ApplicationUIController controller;
private Felix pluginContainer;
public ApplicationUIController getController() {
return controller;
@ -53,6 +68,13 @@ public class Application extends javafx.application.Application {
this.primaryStage = primaryStage;
javafx.application.Platform.setImplicitExit(true);
try {
startPluginContainer();
} catch (BundleException ex) {
Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException(ex);
}
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/ApplicationUI.fxml"));
fxmlLoader.setResources(null);
try {
@ -82,4 +104,14 @@ public class Application extends javafx.application.Application {
public static void main(String[] args) {
launch(args);
}
private void startPluginContainer() throws BundleException {
Map<String, String> pluginConfiguration = new HashMap<>();
pluginConfiguration.put("felix.cache.locking", "false");
pluginConfiguration.put("felix.auto.deploy.action", "install,start");
pluginConfiguration.put("felix.auto.deploy.dir", "install");
pluginContainer = new Felix(pluginConfiguration);
pluginContainer.start();
AutoProcessor.process(pluginConfiguration, pluginContainer.getBundleContext());
}
}

View File

@ -188,6 +188,7 @@ public class UIAction {
}
public static void quitWithoutConfirming() {
Application.shutdown();
Platform.runLater(Platform::exit);
}