This commit is contained in:
Jonathan Cook
2019-10-23 15:01:44 +02:00
parent db85c8f275
commit 684ec0d2e3
20486 changed files with 1642483 additions and 0 deletions
+94
View File
@@ -0,0 +1,94 @@
## OSGi
This module contains articles about OSGi.
### Relevant articles:
- [Introduction to OSGi](https://www.baeldung.com/osgi)
Info
---
com.baeldung.osgi
com.baeldung.osgi.sample.activator
Apache Felix
---
### Start
Download Apache Felix Framework Distribution
from <https://felix.apache.org/downloads.cgi>
org.apache.felix.main.distribution-5.6.8
No! The Apache Karaf container is best.
Download it from: <https://karaf.apache.org/download.html>
Download a binary distribution and unzip wherever you prefer.
Then run
bin\karaf.bat start
Unzip, pay attention to the files not being clipped(!).
system:exit
exit!
shutdown -h
or `^D`
### clean start
full clean, remove "data directory "
or...
bin\karaf.bat clean
bin\start.bat clean
### run mode
can be launched in
- the "regular" mode starts Apache Karaf in foreground, including the shell console.
- the "server" mode starts Apache Karaf in foreground, without the shell console.
- the "background" mode starts Apache Karaf in background.
### Logging
https://karaf.apache.org/manual/latest/#_log
can be logged to console
### Bundle deploy
bundle:install mvn:com.baeldung/osgi-intro-sample-activator/1.0-SNAPSHOT
install mvn:com.baeldung/osgi-intro-sample-service/1.0-SNAPSHOT
install mvn:com.baeldung/osgi-intro-sample-client/1.0-SNAPSHOT
Eclipse's Equinox
====
Eclipse's OSGi platform
http://www.eclipse.org/equinox/
http://www.eclipse.org/equinox/documents/quickstart-framework.php
click on "download"
Latest Release
Oxygen.1 Wed, 6 Sep 2017 -- 17:00 (-0400)
org.eclipse.osgi_3.12.1.v20170821-1548.jar
= = NOT GOOD = =
+50
View File
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>
<artifactId>osgi-intro-sample-activator</artifactId>
<name>osgi-intro-sample-activator</name>
<!-- Please, note this is not the usual 'jar'. -->
<packaging>bundle</packaging>
<!-- com.baeldung/osgi-intro-sample-activator/1.0-SNAPSHOT -->
<parent>
<artifactId>osgi</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<!-- Qualified name of the class that exposes the activator iface. -->
<Bundle-Activator>com.baeldung.osgi.sample.activator.HelloWorld</Bundle-Activator>
<!-- One important thing to note: since you are not exporting the package "com.baeldung.osgi.sample.activator", you should at least add it to the Private-Package
instruction. Otherwise, the classes inside the package will not be copied to your bundle, as the default value of this instruction is empty. -->
<Private-Package>com.baeldung.osgi.sample.activator</Private-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,16 @@
package com.baeldung.osgi.sample.activator;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class HelloWorld implements BundleActivator {
public void start(BundleContext ctx) {
System.out.println("Hello World.");
}
public void stop(BundleContext bundleContext) {
System.out.println("Goodbye World.");
}
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
+52
View File
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>
<!-- mvn:com.baeldung/osgi-intro-sample-client/1.0-SNAPSHOT -->
<artifactId>osgi-intro-sample-client</artifactId>
<name>osgi-intro-sample-client</name>
<packaging>bundle</packaging>
<parent>
<artifactId>osgi</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.baeldung</groupId>
<artifactId>osgi-intro-sample-service</artifactId>
<version>${osgi-intro-sample-service.version}</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-Activator>com.baeldung.osgi.sample.client.Client</Bundle-Activator>
<Private-Package>com.baeldung.osgi.sample.client</Private-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<osgi-intro-sample-service.version>1.0-SNAPSHOT</osgi-intro-sample-service.version>
</properties>
</project>
@@ -0,0 +1,44 @@
package com.baeldung.osgi.sample.client;
import com.baeldung.osgi.sample.service.definition.Greeter;
import org.osgi.framework.*;
public class Client implements BundleActivator, ServiceListener {
private BundleContext ctx;
private ServiceReference serviceReference;
public void start(BundleContext ctx) {
this.ctx = ctx;
try {
ctx.addServiceListener(this, "(objectclass=" + Greeter.class.getName() + ")");
} catch (InvalidSyntaxException ise) {
ise.printStackTrace();
}
}
public void stop(BundleContext bundleContext) {
if (serviceReference != null) {
ctx.ungetService(serviceReference);
}
this.ctx = null;
}
public void serviceChanged(ServiceEvent serviceEvent) {
int type = serviceEvent.getType();
switch (type) {
case (ServiceEvent.REGISTERED):
System.out.println("Notification of service registered.");
serviceReference = serviceEvent.getServiceReference();
Greeter service = (Greeter) (ctx.getService(serviceReference));
System.out.println(service.sayHiTo("John"));
break;
case (ServiceEvent.UNREGISTERING):
System.out.println("Notification of service unregistered.");
ctx.ungetService(serviceEvent.getServiceReference());
break;
default:
break;
}
}
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
+44
View File
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>
<artifactId>osgi-intro-sample-service</artifactId>
<name>osgi-intro-sample-service</name>
<!-- Please, note this is not the usual 'jar'. -->
<packaging>bundle</packaging>
<!-- mvn:com.baeldung/osgi-intro-sample-service/1.0-SNAPSHOT -->
<parent>
<artifactId>osgi</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-Activator>com.baeldung.osgi.sample.service.implementation.GreeterImpl</Bundle-Activator>
<Private-Package>com.baeldung.osgi.sample.service.implementation</Private-Package>
<Export-Package>com.baeldung.osgi.sample.service.definition</Export-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,7 @@
package com.baeldung.osgi.sample.service.definition;
public interface Greeter {
public String sayHiTo(String name);
}
@@ -0,0 +1,30 @@
package com.baeldung.osgi.sample.service.implementation;
import com.baeldung.osgi.sample.service.definition.Greeter;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import java.util.Hashtable;
public class GreeterImpl implements Greeter, BundleActivator {
private ServiceReference<Greeter> reference;
private ServiceRegistration<Greeter> registration;
@Override public String sayHiTo(String name) {
return "Hello " + name;
}
@Override public void start(BundleContext context) throws Exception {
System.out.println("Registering service.");
registration = context.registerService(Greeter.class, new GreeterImpl(), new Hashtable<String, String>());
reference = registration.getReference();
}
@Override public void stop(BundleContext context) throws Exception {
System.out.println("Unregistering service.");
registration.unregister();
}
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
+89
View File
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>
<artifactId>osgi</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>osgi</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<modules>
<module>osgi-intro-sample-activator</module>
<module>osgi-intro-sample-service</module>
<module>osgi-intro-sample-client</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>osgi-intro-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>osgi-intro-service</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>osgi-intro-gxyz</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>osgi-intro-mapquest</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>${javax.json.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>${javax.json.version}</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>${osgi.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>${maven-bundle-plugin.version}</version>
<extensions>true</extensions>
</plugin>
</plugins>
</pluginManagement>
</build>
<properties>
<okhttp.version>3.9.0</okhttp.version>
<javax.json.version>1.1</javax.json.version>
<osgi.version>6.0.0</osgi.version>
<maven-bundle-plugin.version>3.3.0</maven-bundle-plugin.version>
</properties>
</project>