Skip to content

Commit fa589fa

Browse files
danidemipivovarit
authored andcommitted
Bael 728 introduction to osgi (eugenp#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
1 parent c6e7203 commit fa589fa

9 files changed

Lines changed: 425 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<!-- com.baeldung/osgi-intro-sample-activator/1.0-SNAPSHOT -->
7+
<parent>
8+
<artifactId>osgi-intro</artifactId>
9+
<groupId>com.baeldung</groupId>
10+
<version>1.0-SNAPSHOT</version>
11+
</parent>
12+
<modelVersion>4.0.0</modelVersion>
13+
14+
<!-- Please, note this is not the usual 'jar'. -->
15+
<packaging>bundle</packaging>
16+
17+
<artifactId>osgi-intro-sample-activator</artifactId>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.osgi</groupId>
22+
<artifactId>org.osgi.core</artifactId>
23+
</dependency>
24+
</dependencies>
25+
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.apache.felix</groupId>
30+
<artifactId>maven-bundle-plugin</artifactId>
31+
<extensions>true</extensions>
32+
<configuration>
33+
<instructions>
34+
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
35+
<Bundle-Name>${project.artifactId}</Bundle-Name>
36+
<Bundle-Version>${project.version}</Bundle-Version>
37+
38+
<!-- Qualified name of the class that exposes the activator iface. -->
39+
<Bundle-Activator>com.baeldung.osgi.sample.activator.HelloWorld</Bundle-Activator>
40+
41+
<!-- One important thing to note:
42+
since you are not exporting the package "com.baeldung.osgi.sample.activator",
43+
you should at least add it to the Private-Package instruction.
44+
Otherwise, the classes inside the package will not be copied to your bundle,
45+
as the default value of this instruction is empty. -->
46+
47+
<Private-Package>com.baeldung.osgi.sample.activator</Private-Package>
48+
49+
</instructions>
50+
</configuration>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
55+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.osgi.sample.activator;
2+
3+
import org.osgi.framework.BundleActivator;
4+
import org.osgi.framework.BundleContext;
5+
6+
public class HelloWorld implements BundleActivator {
7+
8+
public void start(BundleContext ctx) {
9+
System.out.println("Hello World.");
10+
}
11+
12+
public void stop(BundleContext bundleContext) {
13+
System.out.println("Goodbye World.");
14+
}
15+
16+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>osgi-intro</artifactId>
7+
<groupId>com.baeldung</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<!-- mvn:com.baeldung/osgi-intro-sample-client/1.0-SNAPSHOT -->
13+
<artifactId>osgi-intro-sample-client</artifactId>
14+
15+
<packaging>bundle</packaging>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.baeldung</groupId>
20+
<artifactId>osgi-intro-sample-service</artifactId>
21+
<version>1.0-SNAPSHOT</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.osgi</groupId>
25+
<artifactId>org.osgi.core</artifactId>
26+
</dependency>
27+
</dependencies>
28+
29+
<build>
30+
<plugins>
31+
<plugin>
32+
<groupId>org.apache.felix</groupId>
33+
<artifactId>maven-bundle-plugin</artifactId>
34+
<configuration>
35+
<instructions>
36+
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
37+
<Bundle-Name>${project.artifactId}</Bundle-Name>
38+
<Bundle-Version>${project.version}</Bundle-Version>
39+
<Bundle-Activator>com.baeldung.osgi.sample.client.Client</Bundle-Activator>
40+
41+
<Private-Package>com.baeldung.osgi.sample.client</Private-Package>
42+
43+
</instructions>
44+
</configuration>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
49+
</project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.osgi.sample.client;
2+
3+
import com.baeldung.osgi.sample.service.definition.Greeter;
4+
import org.osgi.framework.*;
5+
6+
public class Client implements BundleActivator, ServiceListener {
7+
8+
private BundleContext ctx;
9+
private ServiceReference serviceReference;
10+
11+
public void start(BundleContext ctx) {
12+
this.ctx = ctx;
13+
try {
14+
ctx.addServiceListener(this, "(objectclass=" + Greeter.class.getName() + ")");
15+
} catch (InvalidSyntaxException ise) {
16+
ise.printStackTrace();
17+
}
18+
}
19+
20+
public void stop(BundleContext bundleContext) {
21+
if (serviceReference != null) {
22+
ctx.ungetService(serviceReference);
23+
}
24+
this.ctx = null;
25+
}
26+
27+
public void serviceChanged(ServiceEvent serviceEvent) {
28+
int type = serviceEvent.getType();
29+
switch (type) {
30+
case (ServiceEvent.REGISTERED):
31+
System.out.println("Notification of service registered.");
32+
serviceReference = serviceEvent.getServiceReference();
33+
Greeter service = (Greeter) (ctx.getService(serviceReference));
34+
System.out.println(service.sayHiTo("John"));
35+
break;
36+
case (ServiceEvent.UNREGISTERING):
37+
System.out.println("Notification of service unregistered.");
38+
ctx.ungetService(serviceEvent.getServiceReference());
39+
break;
40+
default:
41+
break;
42+
}
43+
}
44+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<!-- mvn:com.baeldung/osgi-intro-sample-service/1.0-SNAPSHOT -->
7+
<parent>
8+
<artifactId>osgi-intro</artifactId>
9+
<groupId>com.baeldung</groupId>
10+
<version>1.0-SNAPSHOT</version>
11+
</parent>
12+
<modelVersion>4.0.0</modelVersion>
13+
14+
<artifactId>osgi-intro-sample-service</artifactId>
15+
16+
<!-- Please, note this is not the usual 'jar'. -->
17+
<packaging>bundle</packaging>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.osgi</groupId>
22+
<artifactId>org.osgi.core</artifactId>
23+
</dependency>
24+
</dependencies>
25+
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.apache.felix</groupId>
30+
<artifactId>maven-bundle-plugin</artifactId>
31+
<extensions>true</extensions>
32+
<configuration>
33+
<instructions>
34+
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
35+
<Bundle-Name>${project.artifactId}</Bundle-Name>
36+
<Bundle-Version>${project.version}</Bundle-Version>
37+
<Bundle-Activator>com.baeldung.osgi.sample.service.implementation.GreeterImpl</Bundle-Activator>
38+
<Private-Package>com.baeldung.osgi.sample.service.implementation</Private-Package>
39+
<Export-Package>com.baeldung.osgi.sample.service.definition</Export-Package>
40+
</instructions>
41+
</configuration>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
46+
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.osgi.sample.service.definition;
2+
3+
public interface Greeter {
4+
5+
public String sayHiTo(String name);
6+
7+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.osgi.sample.service.implementation;
2+
3+
import com.baeldung.osgi.sample.service.definition.Greeter;
4+
import org.osgi.framework.BundleActivator;
5+
import org.osgi.framework.BundleContext;
6+
import org.osgi.framework.ServiceReference;
7+
import org.osgi.framework.ServiceRegistration;
8+
9+
import java.util.Hashtable;
10+
11+
public class GreeterImpl implements Greeter, BundleActivator {
12+
13+
private ServiceReference<Greeter> reference;
14+
private ServiceRegistration<Greeter> registration;
15+
16+
@Override public String sayHiTo(String name) {
17+
return "Hello " + name;
18+
}
19+
20+
@Override public void start(BundleContext context) throws Exception {
21+
System.out.println("Registering service.");
22+
registration = context.registerService(Greeter.class, new GreeterImpl(), new Hashtable<String, String>());
23+
reference = registration.getReference();
24+
}
25+
26+
@Override public void stop(BundleContext context) throws Exception {
27+
System.out.println("Unregistering service.");
28+
registration.unregister();
29+
}
30+
}

‎osgi/intro/pom.xml‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<artifactId>osgi-intro</artifactId>
8+
<packaging>pom</packaging>
9+
<version>1.0-SNAPSHOT</version>
10+
<modules>
11+
<module>osgi-intro-sample-activator</module>
12+
<module>osgi-intro-sample-service</module>
13+
<module>osgi-intro-sample-client</module>
14+
</modules>
15+
16+
<parent>
17+
<groupId>com.baeldung</groupId>
18+
<artifactId>parent-modules</artifactId>
19+
<version>1.0.0-SNAPSHOT</version>
20+
<relativePath>../..</relativePath>
21+
</parent>
22+
23+
<dependencyManagement>
24+
<dependencies>
25+
26+
<dependency>
27+
<groupId>${project.groupId}</groupId>
28+
<artifactId>osgi-intro-client</artifactId>
29+
<version>${project.version}</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>${project.groupId}</groupId>
34+
<artifactId>osgi-intro-service</artifactId>
35+
<version>${project.version}</version>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>${project.groupId}</groupId>
40+
<artifactId>osgi-intro-gxyz</artifactId>
41+
<version>${project.version}</version>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>${project.groupId}</groupId>
46+
<artifactId>osgi-intro-mapquest</artifactId>
47+
<version>${project.version}</version>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>com.squareup.okhttp3</groupId>
52+
<artifactId>okhttp</artifactId>
53+
<version>3.9.0</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>javax.json</groupId>
57+
<artifactId>javax.json-api</artifactId>
58+
<version>1.1</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.glassfish</groupId>
62+
<artifactId>javax.json</artifactId>
63+
<version>1.1</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.osgi</groupId>
67+
<artifactId>org.osgi.core</artifactId>
68+
<version>5.0.0</version>
69+
<scope>provided</scope>
70+
</dependency>
71+
</dependencies>
72+
</dependencyManagement>
73+
74+
<build>
75+
<pluginManagement>
76+
<plugins>
77+
<plugin>
78+
<groupId>org.apache.felix</groupId>
79+
<artifactId>maven-bundle-plugin</artifactId>
80+
<version>1.4.0</version>
81+
<extensions>true</extensions>
82+
</plugin>
83+
</plugins>
84+
</pluginManagement>
85+
</build>
86+
87+
</project>

0 commit comments

Comments
 (0)