Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSGi enabled JavaCPP #332

Merged
merged 11 commits into from
Sep 6, 2019
111 changes: 104 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.build.timestamp.format>yyyyMMddhhmm</maven.build.timestamp.format>
<bnd.version>4.2.0</bnd.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -90,6 +91,16 @@
<version>1.7.25</version>
<optional>true</optional>
</dependency>
<!--
The OSGi R7 Annotations are build time only and not
needed at runtime, therefore they can be provided scope
-->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.annotation</artifactId>
<version>7.0.0</version>
<scope>provided</scope>
</dependency>
saudet marked this conversation as resolved.
Show resolved Hide resolved
</dependencies>

<repositories>
Expand Down Expand Up @@ -157,19 +168,31 @@
<version>3.1.0</version>
<configuration>
<archive>
<!-- Pull in the bnd generated manifest -->
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
<manifest>
<mainClass>org.bytedeco.javacpp.tools.Builder</mainClass>
</manifest>
<!-- Multi-Release: true goes in the main attributes -->
<manifestEntries>
<Name>org/bytedeco/javacpp/</Name>
<Implementation-Title>${project.name}</Implementation-Title>
<Implementation-Vendor>Bytedeco</Implementation-Vendor>
<Implementation-Version>${project.version}</Implementation-Version>
<Specification-Title>${project.name}</Specification-Title>
<Specification-Vendor>Bytedeco</Specification-Vendor>
<Specification-Version>${project.version}</Specification-Version>
<Multi-Release>true</Multi-Release>
</manifestEntries>
<!--
We must use a Manifest Section here so that the
maven-jar-plugin merges the manifest metadata
correctly rather than jumbling things up
-->
<manifestSection>
<name>org/bytedeco/javacpp/</name>
<manifestEntries>
<Implementation-Title>${project.name}</Implementation-Title>
<Implementation-Vendor>Bytedeco</Implementation-Vendor>
<Implementation-Version>${project.version}</Implementation-Version>
<Specification-Title>${project.name}</Specification-Title>
<Specification-Vendor>Bytedeco</Specification-Vendor>
<Specification-Version>${project.version}</Specification-Version>
</manifestEntries>
</manifestSection>
saudet marked this conversation as resolved.
Show resolved Hide resolved
</archive>
</configuration>
</plugin>
Expand Down Expand Up @@ -278,6 +301,80 @@
<skipStagingRepositoryClose>true</skipStagingRepositoryClose>
</configuration>
</plugin>
<!--
Process the OSGi annotations and generate a suitable
MANIFEST.MF file for this bundle. This will be merged
with the entries from the maven-jar-plugin
-->
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-maven-plugin</artifactId>
<version>${bnd.version}</version>
<executions>
<execution>
<goals>
<goal>bnd-process</goal>
</goals>
<configuration>
<bnd><![CDATA[
# Avoid pulling in any imports for Maven packages.
# Their usage is isolated to two types which are
# excluded from export. If this changes in future
# then this Import restriction will no longer be safe
#
# Also mark sun.misc as an optional import. If it is
# unavailable then the indexers will fall back to
# other implementations.

Import-Package: \
!org.apache.maven.*,\
sun.misc;resolution:=optional,\
*
]]></bnd>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-baseline-maven-plugin</artifactId>
<version>${bnd.version}</version>
<executions>
<execution>
<goals>
<goal>baseline</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-invoker-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<cloneProjectsTo>${project.build.directory}/integration-test/projects</cloneProjectsTo>
<cloneClean>true</cloneClean>
<projectsDirectory>src/it/</projectsDirectory>
<streamLogs>true</streamLogs>
<localRepositoryPath>${project.build.directory}/integration-test/repo</localRepositoryPath>
<properties>
<maven.compiler.source>${maven.compiler.source}</maven.compiler.source>
<maven.compiler.target>${maven.compiler.target}</maven.compiler.target>
<org.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener>warn</org.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener>
</properties>
<goals>
<goal>verify</goal>
saudet marked this conversation as resolved.
Show resolved Hide resolved
</goals>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>install</goal>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
163 changes: 163 additions & 0 deletions src/it/osgi/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<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.bytedeco</groupId>
<artifactId>javacpp-tests-osgi</artifactId>
<version>@project.version@</version>

<name>JavaCPP OSGi Tests</name>
<description>Integration Tests for Java CPP running in OSGi</description>

<properties>
<bnd.version>4.2.0</bnd.version>
</properties>

<profiles>
<profile>
<id>macosx</id>
<activation>
<os>
<name>Mac OS X</name>
</os>
</activation>
<properties>
<lib.name>macosx-x86_64/libjniCalc.dylib</lib.name>
<os.name>MacOSX</os.name>
</properties>
</profile>
<profile>
<id>linux</id>
<activation>
<os>
<name>Linux</name>
</os>
</activation>
<properties>
<lib.name>linux-x86_64/libjniCalc.so</lib.name>
<os.name>Linux</os.name>
</properties>
</profile>
</profiles>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>javacpp</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.junit</artifactId>
<version>4.12_1</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>6.0.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>${project.groupId}</groupId>
<artifactId>javacpp</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<id>javacpp.parser</id>
<phase>generate-sources</phase>
<goals>
<goal>build</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/generated-sources/javacpp</outputDirectory>
<classOrPackageName>org.bytedeco.javacpp.test.osgi.*</classOrPackageName>
</configuration>
</execution>
<execution>
<id>javacpp.compiler</id>
<phase>process-classes</phase>
<goals>
<goal>build</goal>
</goals>
<configuration>
<classOrPackageName>org.bytedeco.javacpp.test.osgi.*</classOrPackageName>
<includePaths>
<includePath>${project.basedir}/src/main/c</includePath>
</includePaths>
<copyLibs>true</copyLibs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-maven-plugin</artifactId>
<version>${bnd.version}</version>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>bnd-process</goal>
</goals>
</execution>
</executions>
<configuration>
<bnd><![CDATA[
Test-Cases: ${classes;NAMED;*Test}

Bundle-NativeCode: \
org/bytedeco/javacpp/test/osgi/${lib.name};\
osname=${os.name}

]]></bnd>
</configuration>
</plugin>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-testing-maven-plugin</artifactId>
<version>${bnd.version}</version>
<executions>
<execution>
<goals>
<goal>testing</goal>
</goals>
</execution>
</executions>
<configuration>
<bndruns>
<bndrun>test.bndrun</bndrun>
</bndruns>
</configuration>
</plugin>
</plugins>
</build>
</project>
4 changes: 4 additions & 0 deletions src/it/osgi/src/main/c/calc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

inline int add(int a, int b) {
return a + b;
}
33 changes: 33 additions & 0 deletions src/it/osgi/src/main/java/org/bytedeco/javacpp/test/osgi/Calc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.bytedeco.javacpp.test.osgi;

import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacpp.annotation.Platform;

@Platform(include = "calc.h")
public class Calc {

static {

// This line should be sufficient, but it
// isn't because the library gets loaded by
// the wrong classloader
//
// Loader.load();

// This is what we need to happen in the scope
// of the bundle containing the native code,
// not the JavaCPP bundle. Note that the call
// to the Loader is just to force a wiring to
// the JavaCPP package needed by the JNI code

Loader.getPlatform();

System.loadLibrary("jniCalc");
}

private Calc() {}


public static native int add(int a, int b);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.bytedeco.javacpp.test.osgi;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class JavaCPPOsgiTest {

@Test
public void testJavaCPP() {
assertEquals(3, Calc.add(1, 2));
}

}
9 changes: 9 additions & 0 deletions src/it/osgi/test.bndrun
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-runrequires: bnd.identity;id='javacpp-tests-osgi'
-runfw: org.apache.felix.framework;version='[6.0.2,6.0.2]'
-runee: JavaSE-1.8
-runbundles: \
javacpp;version='0.0.0',\
javacpp-tests-osgi;version='0.0.0',\
org.apache.servicemix.bundles.junit;version='[4.12.0,4.12.1)',\
slf4j.api;version='[1.7.25,1.7.26)',\
slf4j.simple;version='[1.7.25,1.7.26)'
5 changes: 5 additions & 0 deletions src/main/java/org/bytedeco/javacpp/LoadEnabled.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@
package org.bytedeco.javacpp;

import org.bytedeco.javacpp.annotation.Properties;
import org.osgi.annotation.versioning.ConsumerType;

/**
* Classes implementing this interface can access and modify the {@link ClassProperties}
* produced from their {@link Properties} when {@link Loader#load(Class)} gets called on them.
* <p>
* Note that this interface is intended to be implemented by users of JavaCPP and so is marked
* as a {@link ConsumerType} interface. Adding methods will be considered a breaking change.
*
* @see Loader
*
* @author Samuel Audet
*/
@ConsumerType
public interface LoadEnabled {
void init(ClassProperties properties);
}
Loading