In the previous post we saw how to inject resources to bundles, and today we will make our first service it is going to be a Date formatting service.
you can download the source for this service from dateformattertar and now we will start creating our service…
go to in the same workspace we started before to get advantage of the target platform and the log4j configuration
File –> New –> Other…
And select to create Plug-in project

Click next and name the project DateFormatter and don’t forget to change the target platform to “an OSGi Target”

Click Next and Finish.
In thee newly created project Open the MAINFEST.MF file in the META-INF folder and add the “org.apache.log4j” to the imported packages as we are going to use it.
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: DateFormatter Plug-in
Bundle-SymbolicName: DateFormatter
Bundle-Version: 1.0.0
Bundle-Activator: dateformatter.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework;version=”1.3.0?,
org.apache.log4j
Now create a sub-package “dateformatter.app” And create a new Interface in it with the name DateFormatter
package dateformatter.app;
import java.util.Date;
public interface DateFormatter {
String formatDate(Date date);
}
Now create a sub-package “dateformatter.app.impl” And create a new Interface in it with the name DateFormatterImpl
package dateformatter.app.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.Logger;
import dateformatter.app.DateFormatter;
public class DateFormatterImpl implements DateFormatter {
private String format;
private Logger logger = Logger.getLogger(DateFormatterImpl.class);
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
@Override
public String formatDate(Date date) {
logger.info(“formatting a new date”);
SimpleDateFormat formatter = new SimpleDateFormat(format);
return formatter.format(date);
}
}
Now all we need to do is to add a spring definition for our classes, spring looks for application context in the META-INF/spring/ so create your application context file there…
<?xml version=”1.0? encoding=”UTF-8??>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:context=”http://www.springframework.org/schema/context”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd”>
<bean id=”formatterImpl” class=”dateformatter.app.impl.DateFormatterImpl”>
<property name=”format” value=”dd-MM-yyyy”></property>
</bean>
</beans>
Creating a Registration listener and Importing the service
Now it is very useful to use a registration listener for keeping track of your bundle, Spring DM offers a very good way for doing this, it is by making a new class containing at least two methods which there names are irrelevant -as the class name- but the only constraint is to make the arguments in the both methods take the first argument as reference to the service INTERFACE and the other as a Map to hold the service properties…
package dateformatter.listener;
import java.util.Map;
import dateformatter.app.DateFormatter;
public class RegListener {
public void reg(DateFormatter formatter, Map map) {
System.out.println(“am here”);
}
public void unReg(DateFormatter formatter, Map map) {
System.out.println(“am going away”);
}
}
Now a wrote another spring context file and put it in the spring folder too, this one to hold the OSGi beans:
<?xml version=”1.0? encoding=”UTF-8??>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:osgi=”http://www.springframework.org/schema/osgi”
xmlns:osgi-compendium=”http://www.springframework.org/schema/osgi-compendium”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd
http://www.springframework.org/schema/osgi-compendium http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd”>
<osgi:service id=”formatter” interface=”dateformatter.app.DateFormatter” ref=”formatterImpl”>
<osgi:registration-listener registration-method=”reg” unregistration-method=”unReg”>
<bean class=”dateformatter.listener.RegListener”>
</bean>
</osgi:registration-listener>
</osgi:service>
</beans>
this way I publish a service with a registration listener, to run it you need to run the target platform.
if every thing goes will you will see something like the following:

you can test the registration listener by stopping and running the service.
Next post we will write a client that uses this service…