Bael 728 introduction to osgi (#3004)

* BAEL-728: first working geocoder example

* First steps with different geocoder

* Working plaing app

* Small step

* Maven project subdivided into components.

* <Export-Package> added

* CLient

* small step fwd

* Committing before resetting idea

* It does not compile!

* some other mod

* Works

* step

* Examples works

* removed unused code
This commit is contained in:
Daniele Demichelis 2017-11-11 06:20:23 +01:00 committed by Grzegorz Piwowarek
parent c6e7203f99
commit fa589fa7bf
9 changed files with 425 additions and 0 deletions

View File

@ -0,0 +1,55 @@
<?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">
<!-- com.baeldung/osgi-intro-sample-activator/1.0-SNAPSHOT -->
<parent>
<artifactId>osgi-intro</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<!-- Please, note this is not the usual 'jar'. -->
<packaging>bundle</packaging>
<artifactId>osgi-intro-sample-activator</artifactId>
<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>

View File

@ -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.");
}
}

View File

@ -0,0 +1,49 @@
<?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">
<parent>
<artifactId>osgi-intro</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<!-- mvn:com.baeldung/osgi-intro-sample-client/1.0-SNAPSHOT -->
<artifactId>osgi-intro-sample-client</artifactId>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>com.baeldung</groupId>
<artifactId>osgi-intro-sample-service</artifactId>
<version>1.0-SNAPSHOT</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>
</project>

View File

@ -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;
}
}
}

View File

@ -0,0 +1,46 @@
<?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">
<!-- mvn:com.baeldung/osgi-intro-sample-service/1.0-SNAPSHOT -->
<parent>
<artifactId>osgi-intro</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>osgi-intro-sample-service</artifactId>
<!-- Please, note this is not the usual 'jar'. -->
<packaging>bundle</packaging>
<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>

View File

@ -0,0 +1,7 @@
package com.baeldung.osgi.sample.service.definition;
public interface Greeter {
public String sayHiTo(String name);
}

View File

@ -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();
}
}

87
osgi/intro/pom.xml Normal file
View File

@ -0,0 +1,87 @@
<?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</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>osgi-intro-sample-activator</module>
<module>osgi-intro-sample-service</module>
<module>osgi-intro-sample-client</module>
</modules>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<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>3.9.0</version>
</dependency>
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>1.4.0</version>
<extensions>true</extensions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

91
osgi/intro/readme.md Normal file
View File

@ -0,0 +1,91 @@
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 = =