Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 7
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.)
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.
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.
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
Technology downloads
More
Archived Discussions (Read only)