Hello, OSGi, Part 2: Introduction to Spring Dynamic Modules

Build a service-oriented application using Spring and OSGi

1 2 3 4 5 6 7 Page 4
Page 4 of 7

Importing and exporting service bundles

The Contact Management application has two bundles. The first bundle, ContactDAO, talks to databases and exports ContactDAO objects as OSGi services. The second bundle is the HelloWorld application you developed earlier, which we'll extend here to import ContactDAO objects (aka OSGi services).

Let's start by creating the ContactDAO bundle. In order to keep things simple we won't add real database interaction logic to the bundle; instead, every method will simply write its method name to the Eclipse console.

The first thing to do is create a com.javaworld.sample.osgi.spring.contact.Contact.java class, which we'll use to carry data from ContactDAO to the HelloWorld bundle, as shown in Listing 3. (Contact.java is a simple class that will represent one contact record in a database.)

Listing 3. Contact.java

package com.javaworld.sample.osgi.spring.contact;
public class Contact {
    int contactId;
    String firstName;
    String lastName;
    public int getContactId() {
            return contactId;
    }
    public void setContactId(int contactId) {
            this.contactId = contactId;
    }
}

Next, we create the ContactDAO.java interface, as shown in Listing 4.

Listing 4. ContactDAO interface

package com.javaworld.sample.osgi.spring.contact;
public interface ContactDAO {
    public List getContactList();
    public Contact getContact(int contactId);
    public void insertContact(Contact contact);
    public void updateContact(Contact contact);
    public void deleteContact(int contactId);
}

ContactDAO is a simple CRUD interface: it defines methods to create, update, retrieve, and delete operations.

Now, create an implementation of the ContactDAO.java class, as shown in Listing 5.

Listing 5. ContactDAOImpl.java

package com.javaworld.sample.osgi.spring.contact.impl;
public class ContactDAOImpl implements ContactDAO {
    public Contact getContact(int contactId) {
        System.out.println("Inside ContactDAOImpl.getContact()");
        return null;
    }
    // Do nothing implementation of all other methods defined in ContactDAO
}

ContactDAOImpl.java provides a "do nothing" implementation of the ContactDAO interface. All we're doing with this class is writing the method name to System.Out.

Note that both Contact and ContactDAO, which are public classes by necessity (other bundles will need to access them in order to use the ContactDAO service), are in the com.javaworld.sample.osgi.spring.contact package. But the actual implementation class, ContactDAOImpl.java (which is an internal class to the ContactDAO bundle) is in the com.javaworld.sample.osgi.spring.contact.impl package.

Let's change the MANIFEST.MF file for the ContactDAO bundle to export the com.javaworld.sample.osgi.spring.contact bundle so that it can be accessed from the HelloWorld bundle. All we need to do is add the following line to the MANIFEST.MF:

Export-Package: com.javaworld.sample.osgi.spring.contact
1 2 3 4 5 6 7 Page 4
Page 4 of 7