-
-
- Tips and Tricks
Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering
After this lecture you will have created an OSGi Service and a Web Service.
In this example, we create a SOAP based web service for a simple Java Calculator class with operations ‘add’ and ‘subtract’. We then create a web service client which consumes the web service and displays the result of the invoked web service.
package de.unibonn.se.atsc.ws.calc; import javax.jws.WebService; @WebService public class Calculator { public int add(int a, int b) { return (a + b); } public int sub(int a, int b) { return (a - b); } }
wsgen -s src -cp bin -d bin de.unibonn.se.atsc.ws.calc.Calculator
The –cp option specifies the classpath for our Calculator class which is in “bin” folder, the –d option specifies where to place generated output files which is also the ‘bin’ folder in our case and the “-s src” tells the generator to create the source class in the src-folder.
package de.unibonn.se.atsc.ws.calc; import javax.xml.ws.Endpoint; import de.unibonn.se.atsc.ws.calc.Calculator; public class CalcEndpointPublisher { public static void main(String[] args) { Endpoint.publish("http://localhost:8080/CalcWS/Calculator", new Calculator()); } }
http://localhost:8080/CalcWS/Calculator?wsdl
package de.unibonn.se.atsc.ws.calc.client; import de.unibonn.se.atsc.ws.calc.Calculator; import de.unibonn.se.atsc.ws.calc.CalculatorService; public class CalcClient { public static void main(String[] args) { int a = 10; int b = 12; CalculatorService calcService = new CalculatorService(); Calculator calc = calcService.getCalculatorPort(); System.out.println(a + " + " + b + " = " + calc.add(a, b)); System.out.println(a + " - " + b + " = " + calc.sub(a, b)); } }
10 + 12 = 22 10 – 12 = -2
The OSGi components are named bundles. Eclipse offers excellent support for developing OSGi bundles. Not only does it provide wizards for creating OSGi bundles, it also has an embedded Equinox OSGi container that you can use to execute and debug OSGi plugins.Every Eclipse plug-in is essentially an OSGi bundle with some additional Eclipse-specific code. Eclipse also allows you to build standard-compliant OSGi bundles without code specific to Eclipse. In the following you'll learn how to develop a Hello World OSGi bundle using the Eclipse IDE.
Follow the steps below to create a Hello World bundle using OSGi and Eclipse.
Eclipse will take few seconds to generate template code for the Hello World bundle. It will create two files: Activator.java and MANIFEST.MF.
Your Activator.java file should look like:
package edu.bonn.osgi.helloworld; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception { System.out.println("Hello World!!"); } public void stop(BundleContext context) throws Exception { System.out.println("Goodbye World!!"); } }
If your bundle needs to be notified at the time of bundle startup or shutdown then you should create a class implementing the BundleActivator interface. Follow these rules when creating the class:
The MANIFEST.MF file acts as deployment descriptor for your bundle. The format for this file is the same as that of a normal JAR file, so it consists of a set of headers with values. The OSGi specification defines a set of headers that you can use to describe your bundle to the OSGi container. The MANIFEST.MF file for your Hello World bundle should look like:
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: HelloWorld Bundle-SymbolicName: edu.bonn.osgi.HelloWorld Bundle-Version: 1.0.0.qualifier Bundle-Activator: edu.bonn.osgi.helloworld.Activator Import-Package: org.osgi.framework;version="1.3.0" Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Let's take a closer look at what each of these headers is used for:
The Eclipse IDE has an embedded Equinox OSGi container that you can use to execute or debug OSGi bundles. Follow these steps to execute the Hello World bundle:
Due to time constraints of our lecture, we refer to the complete tutorial which also covers creation of an additional bundle with a HelloWorld Service and usage of this bundle in the above edu.bonn.osgi.HelloWorld bundle. The full tutorial can be accessed here: JavaWorld Tutorial: Hello OSGi, Part 1 Bundles for beginners.