diff --git a/osgi/intro/osgi-intro-sample-activator/pom.xml b/osgi/intro/osgi-intro-sample-activator/pom.xml new file mode 100644 index 0000000000..1584913627 --- /dev/null +++ b/osgi/intro/osgi-intro-sample-activator/pom.xml @@ -0,0 +1,55 @@ + + + + + + osgi-intro + com.baeldung + 1.0-SNAPSHOT + + 4.0.0 + + + bundle + + osgi-intro-sample-activator + + + + org.osgi + org.osgi.core + + + + + + + org.apache.felix + maven-bundle-plugin + true + + + ${project.groupId}.${project.artifactId} + ${project.artifactId} + ${project.version} + + + com.baeldung.osgi.sample.activator.HelloWorld + + + + com.baeldung.osgi.sample.activator + + + + + + + + diff --git a/osgi/intro/osgi-intro-sample-activator/src/main/java/com/baeldung/osgi/sample/activator/HelloWorld.java b/osgi/intro/osgi-intro-sample-activator/src/main/java/com/baeldung/osgi/sample/activator/HelloWorld.java new file mode 100644 index 0000000000..72fe624bac --- /dev/null +++ b/osgi/intro/osgi-intro-sample-activator/src/main/java/com/baeldung/osgi/sample/activator/HelloWorld.java @@ -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."); + } + +} \ No newline at end of file diff --git a/osgi/intro/osgi-intro-sample-client/pom.xml b/osgi/intro/osgi-intro-sample-client/pom.xml new file mode 100644 index 0000000000..4096674d7d --- /dev/null +++ b/osgi/intro/osgi-intro-sample-client/pom.xml @@ -0,0 +1,49 @@ + + + + osgi-intro + com.baeldung + 1.0-SNAPSHOT + + 4.0.0 + + + osgi-intro-sample-client + + bundle + + + + com.baeldung + osgi-intro-sample-service + 1.0-SNAPSHOT + + + org.osgi + org.osgi.core + + + + + + + org.apache.felix + maven-bundle-plugin + + + ${project.groupId}.${project.artifactId} + ${project.artifactId} + ${project.version} + com.baeldung.osgi.sample.client.Client + + com.baeldung.osgi.sample.client + + + + + + + + \ No newline at end of file diff --git a/osgi/intro/osgi-intro-sample-client/src/main/java/com/baeldung/osgi/sample/client/Client.java b/osgi/intro/osgi-intro-sample-client/src/main/java/com/baeldung/osgi/sample/client/Client.java new file mode 100644 index 0000000000..a82ed63fa7 --- /dev/null +++ b/osgi/intro/osgi-intro-sample-client/src/main/java/com/baeldung/osgi/sample/client/Client.java @@ -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; + } + } +} diff --git a/osgi/intro/osgi-intro-sample-service/pom.xml b/osgi/intro/osgi-intro-sample-service/pom.xml new file mode 100644 index 0000000000..cbc660bb74 --- /dev/null +++ b/osgi/intro/osgi-intro-sample-service/pom.xml @@ -0,0 +1,46 @@ + + + + + + osgi-intro + com.baeldung + 1.0-SNAPSHOT + + 4.0.0 + + osgi-intro-sample-service + + + bundle + + + + org.osgi + org.osgi.core + + + + + + + org.apache.felix + maven-bundle-plugin + true + + + ${project.groupId}.${project.artifactId} + ${project.artifactId} + ${project.version} + com.baeldung.osgi.sample.service.implementation.GreeterImpl + com.baeldung.osgi.sample.service.implementation + com.baeldung.osgi.sample.service.definition + + + + + + + \ No newline at end of file diff --git a/osgi/intro/osgi-intro-sample-service/src/main/java/com/baeldung/osgi/sample/service/definition/Greeter.java b/osgi/intro/osgi-intro-sample-service/src/main/java/com/baeldung/osgi/sample/service/definition/Greeter.java new file mode 100644 index 0000000000..f1e82a3465 --- /dev/null +++ b/osgi/intro/osgi-intro-sample-service/src/main/java/com/baeldung/osgi/sample/service/definition/Greeter.java @@ -0,0 +1,7 @@ +package com.baeldung.osgi.sample.service.definition; + +public interface Greeter { + + public String sayHiTo(String name); + +} diff --git a/osgi/intro/osgi-intro-sample-service/src/main/java/com/baeldung/osgi/sample/service/implementation/GreeterImpl.java b/osgi/intro/osgi-intro-sample-service/src/main/java/com/baeldung/osgi/sample/service/implementation/GreeterImpl.java new file mode 100644 index 0000000000..48e26e3e6b --- /dev/null +++ b/osgi/intro/osgi-intro-sample-service/src/main/java/com/baeldung/osgi/sample/service/implementation/GreeterImpl.java @@ -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 reference; + private ServiceRegistration 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()); + reference = registration.getReference(); + } + + @Override public void stop(BundleContext context) throws Exception { + System.out.println("Unregistering service."); + registration.unregister(); + } +} diff --git a/osgi/intro/pom.xml b/osgi/intro/pom.xml new file mode 100644 index 0000000000..83db90d19e --- /dev/null +++ b/osgi/intro/pom.xml @@ -0,0 +1,87 @@ + + + 4.0.0 + + osgi-intro + pom + 1.0-SNAPSHOT + + osgi-intro-sample-activator + osgi-intro-sample-service + osgi-intro-sample-client + + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../.. + + + + + + + ${project.groupId} + osgi-intro-client + ${project.version} + + + + ${project.groupId} + osgi-intro-service + ${project.version} + + + + ${project.groupId} + osgi-intro-gxyz + ${project.version} + + + + ${project.groupId} + osgi-intro-mapquest + ${project.version} + + + + com.squareup.okhttp3 + okhttp + 3.9.0 + + + javax.json + javax.json-api + 1.1 + + + org.glassfish + javax.json + 1.1 + + + org.osgi + org.osgi.core + 5.0.0 + provided + + + + + + + + + org.apache.felix + maven-bundle-plugin + 1.4.0 + true + + + + + + \ No newline at end of file diff --git a/osgi/intro/readme.md b/osgi/intro/readme.md new file mode 100644 index 0000000000..ea4a411c7b --- /dev/null +++ b/osgi/intro/readme.md @@ -0,0 +1,91 @@ +OSGi +==== + +Info +--- + +com.baeldung.osgi +com.baeldung.osgi.sample.activator + +Apache Felix +--- + + +### Start + +Download Apache Felix Framework Distribution +from +org.apache.felix.main.distribution-5.6.8 + +No! The Apache Karaf container is best. +Download it from: + +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 = = + + + +