There are three OSGi bundles in this example. Two are different versions of a bundle that exposes a message interface as an OSGi service. Each bundle depends on a different version of Commons Lang to demonstrate the side-by-side versioning capabilities of OSGi. The third bundle is a web module that displays the result from calling the OSGi message service. The example was made to be deployed on the Spring dm Server.
Note | |
---|---|
Currently the examples are setup to only run from the Eclipse environment and to be deployed using the Spring dm Server's IDE integration. A Maven build will eventually be added. |
The manifest defines the services name, description, it's symbolic name (will deployed under this name), version, and whatever libraries, bundles, and/or packages it imports. Also, whatever packages are exported are also defined.
Note | |
---|---|
If when opening the manifest and viewing the Dependencies tab in the Eclipse IDE, there isn't an 'Import Bundle' section on the right and an 'Import Library' section underneath it the default Eclipse manifest editor may have been opened. To open the Spring manifest editor, right click, 'Open with'/'Other...', and then choose the 'Bundle Manifest Editor'. |
Manifest-Version: 1.0 Bundle-Name: Simple Service Bundle Bundle-Description: Simple Service Bundle-ManifestVersion: 2 Bundle-SymbolicName: org.springbyexample.sdms.message.messageService Bundle-Version: 1.0.0 Import-Library: org.aspectj, org.springframework.spring Import-Bundle: com.springsource.org.apache.commons.lang;version="[2.1.0,2.1.0]" Export-Package: org.springbyexample.sdms.message.service;version="1.0.0"
By default any spring configuration files in the META-INF/spring will be processed (this is configurable). Following the naming conventions setup by Spring Dynamic Modules for OSGi(tm) Service Platforms there are two Spring configuration files. One is called bundle-context.xml and the other is called bundle-context-osgi.xml. The first one is for standard Spring configuration and the latter is meant for Spring Dynamic Modules specific configuration. Like exposing OSGi services or defining services as bean references.
The MessageServiceImpl
is defined as a messageService bean which will
be exposed as a service in the bundle-context-osgi.xml configuration file.
Spring's context:component-scan could have also been used, but wasn't just to keep the
example a little more clear.
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="messageService" class="org.springbyexample.sdms.message.service.impl.MessageServiceImpl" p:message="Greetings!"/> </beans>
The Spring Dynamic Modules' namespace is
setup as the default namespace for the configuration file. The service
element exposes the messageService bean as an OSGi service under the
interface org.springbyexample.sdms.message.service.MessageService
.
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/osgi" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" 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"> <service ref="messageService" interface="org.springbyexample.sdms.message.service.MessageService"/> </beans:beans>
The code consists of the MessageService
interface and the implementation MessageServiceImpl
.
It's just an interface to get a message and the implementation uses Commons Lang.
Example 1. Message Service MessageService
(version 1.0)
public interface MessageService { /** * Gets message. */ public String getMessage(); }
This implementation uses FastDateFormat
from Commons Lang 2.1.0 to display a formatted date
after the message that will be injected from the Spring configuration.
Example 2. Message Service MessageServiceImpl
(version 1.0)
public class MessageServiceImpl implements MessageService { private String message = null; /** * Gets message. */ public String getMessage() { StringBuffer sb = new StringBuffer(); sb.append(message); sb.append(" ["); sb.append(FastDateFormat.getInstance("MM/dd/yy").format(new Date())); sb.append("]"); return sb.toString(); } /** * Sets message. */ public void setMessage(String message) { this.message = message; } }