Miles to go …

April 27, 2010

TOTD #130: Invoking a OSGi service from a JAX-WS Endpoint – OSGi and Enterprise Java

Filed under: frameworks, glassfish, javaee, webservices — arungupta @ 6:15 am

Sahoo blogged about JAX-WS Web service in an OSGi bundle. This Tip Of The Day (TOTD) provides complete steps to create such an application from scratch.

We will create an OSGi service, a JAX-WS compliant Web service as hybrid application, and a JAX-WS Web service client.

Lets create an OSGi service first.

  1. Create a simple OSGi service as explained in TOTD #36. Generate the maven project as:

    mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes \
    -DgroupId=org.glassfish.samples.osgi.hello.service -DartifactId=osgi-pure

    The updated directory structure looks like:


    "" is the OSGi service interface and looks like:

    public interface Hello {
        public String sayHello(String name);

    "" is a trivial implementation of the service and looks like:

    public class HelloImpl implements Hello {
        public String sayHello(String name) {
            return "Hello " + name;

    "" is the Bundle Activator and looks like:

    import org.glassfish.samples.osgi.hello.service.impl.HelloImpl;
    import java.util.Properties;
    import org.glassfish.samples.osgi.hello.service.Hello;
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
     * Hello world!
    public class App implements BundleActivator {
        public void start(BundleContext bc) throws Exception {
            bc.registerService(Hello.class.getName(), new HelloImpl(), new Properties());
        public void stop(BundleContext bc) throws Exception {

    The "start" method registers the OSGi service using the name "Hello" and "stop" method un-registers the service.

  2. The updated "maven-bundle-plugin" from TOTD #36 looks like:


    Notice, only the package that contains the service interface, i.e. "org.glassfish.samples.osgi.hello.service", is exported and the activator and service implementation are in a different package.

  3. Create the OSGi service bundle as:

    mvn install

    This also installs the OSGi bundle in the local maven repository. Deploy this bundle in GlassFish v3 by copying to "glassfish/domains/domain1/autodeploy/bundles" directory. Make sure GlassFish is running or start it as:

    asadmin start-domain --verbose

Lets create a hybrid application that consists of a JAX-WS compliant Web service, queries the OSGi service registry, invokes the OSGi service and return a response to the client.

  1. Create the Maven project as:

    mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes \
    -DgroupId=org.glassfish.samples.osgi.jaxws.webservice -DartifactId=helloservice
  2. Change the generated "src/main/java/org/glassfish/samples/osgi/jaxws/webservice/" such that it looks like:

    import javax.jws.WebService;
    import org.glassfish.samples.osgi.hello.service.Hello;
    import org.osgi.framework.BundleContext;
    import org.osgi.framework.BundleReference;
    import org.osgi.framework.ServiceReference;
     * Hello world!
    public class App {
        public String sayHello(String name) {
            Hello service = getService(Hello.class);
            return service.sayHello(name);
         * This method looks up service of given type in OSGi service registry and returns if found.
         * Returns null if no such service is available,
       private static  T getService(Class type) {
           BundleContext ctx = BundleReference.class.cast(
           ServiceReference ref = ctx.getServiceReference(type.getName());
           return ref != null ? type.cast(ctx.getService(ref)) : null;

    The "getService" method queries the OSGI service registry and returns the service reference. The "sayHello" method looks for the "Hello" service and invokes a method on it. The name "Hello" is the same as registered during the OSGi bundle creation earlier.

  3. In the generated "pom.xml":

    1. Change the packaging to "war".
    2. Add the following repository:

          <name>GlassFish Maven Repository</name>

      so that Java EE API dependency can be resolved.

    3. Add the following dependencies in "provided" scope:


      Notice "osgi-pure" bundle is specified as dependency as that is used to invoke the service.

    4. Add the following plugins to "pom.xml" to create the hybrid application:

              <Import-Package>javax.jws; version=2.0, *</Import-Package>
        <plugin> <!-- Need to use this plugin to build war files -->
              <!-- add bundle plugin generated manifest to the war -->
              <!-- For some reason, adding Bundle-ClassPath in maven-bundle-plugin
                   confuses that plugin and it generates wrong Import-Package, etc.
                   So, we generate it here.

      The "maven-bundle-plugin" is used to generate the appropriate OSGi metadata and "maven-war-plugin" is used to bundle the WAR file.

  4. Generate the deployable archive as:

    mvn clean package

    This generates "target/helloservice-1.0-SNAPSHOT.war". The generated manifest in "target/helloservice-1.0-SNAPSHOT/WEB-INF/classes/META-INF/MANIFEST.MF" looks like:

    Manifest-Version: 1.0
    Export-Package: org.glassfish.samples.osgi.jaxws.webservice;uses:="org
    Bundle-Version: 1.0.0.SNAPSHOT
    Tool: Bnd-0.0.357
    Bundle-Name: helloservice
    Bnd-LastModified: 1272315464139
    Created-By: 1.6.0_17 (Apple Inc.)
    Bundle-ManifestVersion: 2
    Bundle-SymbolicName: org.glassfish.samples.osgi.jaxws.webservice.hello
    Web-ContextPath: helloservice
    Import-Package: javax.jws;version="2.0",org.glassfish.samples.osgi.hel
  5. Now this archive is a hybrid application, i.e. its a WAR file and an OSGi bundle. So lets deploy this file by copying the file to "domains/domain1/autodeploy/bundles" and see a message like:

    WS00018: Webservice Endpoint deployed App listening at address at http://localhost:8080/helloservice/AppService
  6. Accessing "http://localhost:8080/helloservice/AppService" in a browser window shows the following page:

Lets create the client project now to invoke this Web service.

  1. Create a new directory "client" and invoke the following command to generate the client-side artifacts:

    wsimport -keep http://localhost:8080/helloservice/AppService?wsdl
  2. Create a new directory "client" and a new file "HelloClient" in that directory as:

    package client;
    import org.glassfish.samples.osgi.jaxws.webservice.*
    public class HelloClient {
            public static void main(String[] args) throws Exception {
                    App port = new AppService().getAppPort();

    This "main" method gets a reference to the generated service class, gets the port from it, and then invokes the method by passing an argument.

  3. Compile the client code as:

    javac -d . -cp . client/
  4. Invoke the client as:

    java -cp . client.HelloClient

    to see the result as:

    Hello Duke

    This result is coming from the OSGi service implementation.

All the three projects explained above are available in this download.

Using similar concept, a pure OSGi client can invoke a pure OSGi service which can then delegate the actual business method implementation to a JAX-WS endpoint and then use all the goodness of the underlying stack. This way, the benefits of JAX-WS are extended to a pure OSGi client and vice versa.

Also see other OSGi entries on this blog.

Technorati: totd jaxws osgi glassfish v3 webservice

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. TOTD #124: OSGi Declarative Services in GlassFish – Accessed from a Java EE client
  2. TOTD #12: Invoking a Java EE 5 Web service endpoint from JRuby
  3. TOTD #131: Dynamic OSGi services in GlassFish – Using ServiceTracker
  4. TOTD #23: JavaFX Client invoking a Metro endpoint
  5. TOTD #125: Creating an OSGi bundles using NetBeans and deploying in GlassFish

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress