2. Message Service Bundle 1.1

Manifest Configuration

This is the same as the 1.0 version of the Message Service bundle, but it has it's version set to 1.1.0 on the Bundle-Version property and also depends on Commons Lang 2.4.0.

src/main/resources/META-INF/MANIFEST.MF
                    
Manifest-Version: 1.0
Bundle-Name: Simple Service Bundle
Bundle-Description: Simple Service
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.springbyexample.sdms.message.messageService  1
Bundle-Version: 1.1.0  2
Import-Library: org.aspectj, 
 org.springframework.spring
Import-Bundle: com.springsource.org.apache.commons.lang;version="[2.4.0,2.4.0]"  3
Export-Package: org.springbyexample.sdms.message.service;version="1.1.0"  4
                    
                
1 The symbolic name the bundle is deployed under and another bundle could use to reference it.
2 The version the bundle is deployed under.
3 The Import-Bundle property imports the package com.springsource.org.apache.commons.lang, version 2.4.0.
4 The Export-Package property exposes any packages for external use by other bundles. This exposes the package org.springbyexample.sdms.message.service which has the MessageService interface as version 1.1.0.

Spring Configuration

The version 1.1 of MessageServiceImpl has a different message than version 1.0. Also the implementation uses a Commons Lang 2.4.0 specific method call that is appended to the message.

src/main/resources/META-INF/spring/bundle-context.xml
                    
<?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 from Spring by Example!"/>
    
</beans>
                    
                

The messageService bean is exposed as a service under the interface org.springbyexample.sdms.message.service.MessageService.

src/main/resources/META-INF/spring/bundle-context-osgi.xml
                    
<?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>
                    
                

Code Example

The MessageService interface is identical to the one in the Message Service Bundle 1.0 class. So the Message Service Web Module can dynamically use either implementation's service.

Example 3. Message Service MessageService (version 1.1)

sdms/message-service/service-version1_1/src/main/java/org/springbyexample/sdms/message/service/MessageService.java
                    
public interface MessageService {

    /**
     * Gets message.
     */
    public String getMessage();
    
}
                    
                

This implementation uses FastDateFormat from Commons Lang 2.4.0 to display a formatted date after the message that will be injected from the Spring configuration. It also uses StringUtils.length(String) to add the message length at the end of the message. This method was adding Commons Lang 2.4.0. If you try to use this method in the Message Service Bundle 1.0 implementation, you will see that it won't be able to find that method on StringUtils.

Example 4. Message Service MessageServiceImpl (version 1.1)

sdms/message-service/service-version1_1/src/main/java/org/springbyexample/sdms/message/service/impl/MessageServiceImpl.java
                    
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/yyyy").format(new Date()));
        sb.append("]");
        
        // add Commons Lang StringUtils.length(String) which was specifically added in Commons Lang 2.4.0)
        sb.append("  (message length=");
        sb.append(StringUtils.length(message));
        sb.append(")");

        return sb.toString();
    }

    /**
     * Sets message.
     */
    public void setMessage(String message) {
        this.message = message;
    }
    
}