Monday, September 1, 2008

Tutorial: Sample RCP application using Spring, Spring Dynamic Modules for OSGi Service Platforms and AgileRCP (2/3)

In this tutorial you will extend the sample application of the first tutorial part. You will add another plug-in that provides a new user manager. You will learn how to declare Spring beans as OSGi services and use them in other plug-ins. This is possible without any modification of the original source code.
  1. Create a new plug-in project for the user manager service:
    • New -> Other... -> Plug-in Development / Plug-in Project
    • Choose a proper project name, e.g. "org.digitalcure.hellospring.service"
    • Generate an activator: yes, This plug-in will make contributions to the UI: no, Would you like to create a rich client application? no
    • Create a plug-in using one of the templates: no

  2. Edit the MANIFEST.MF file and import the package "org.digitalcure.hellospring.common" (Dependencies tab).

  3. Create an implementation of a user manager. Create a new class inside the "org.digitalcure.hellospring.service" package:
    • New -> Class
    • Choose a class name: ExtendedUserManager
    • Interfaces: org.digitalcure.hellospring.common.IUserManager

  4. Implement the getUserNames() method, so that it returns three names, e.g.:
    public List<String> getUserNames() {
    final List<String> list = new ArrayList<String>(3);
    list.add("Charly");
    list.add("Dennis");
    list.add("Ed");
    return list;
    }
  5. Now you are ready to create the Spring configuration files. Create a folder named "spring" inside the META-INF folder.

  6. Create two XML files inside the "spring" folder:
    • bundle-context.xml
    • bundle-context-osgi.xml

  7. bundle-context.xml: Create a Spring bean for the user manager:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="extendedUserManager" class="org.digitalcure.hellospring.service.ExtendedUserManager" />
    </beans>
  8. bundle-context-osgi.xml: Export the user manager bean as OSGi service:
    <?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/osgi
    http://www.springframework.org/schema/osgi/spring-osgi.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <service id="extendedUserManagerService" ref="extendedUserManager"
    interface="org.digitalcure.hellospring.common.IUserManager" />
    </beans:beans>
  9. Add Spring support to the "service" project: Open the context menu for the project and select "Spring Tools" -> "Add Spring Project Nature". The project itself will be marked with a "S" in the upper right corner of the project icon.

  10. Open the Properties dialog of the project. Go to "Spring" -> "Beans Support" and add the two XML files of the "spring" folder. After closing the dialog, both XML files will be marked with an "S", plus the Java file "ExtendedUserManager.java".

  11. Switch back to the first project (the "app" project). Now you have to configure the first user manager (default user manager) as service. You have to edit the Spring configuration files of the project.

  12. bundle-context-osgi.xml: Export the user manager bean as OSGi service:
    <?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/osgi
    http://www.springframework.org/schema/osgi/spring-osgi.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <service id="defaultUserManagerService" ref="defaultUserManager"
    interface="org.digitalcure.hellospring.common.IUserManager" />
    </beans:beans>
  13. bundle-context.xml: Import an user manager service:
    <osgi:reference id="userManagerService" interface="org.digitalcure.hellospring.common.IUserManager" />
  14. bundle-context.xml: Use the user manager service for the action:
    <bean id="listUserAction" class="org.digitalcure.hellospring.app.ListUserAction" >
    <property name="userManager" ref="userManagerService" />
    </bean>
  15. Edit the product configuration file "HelloSpringApp.product" and add the "service" plug-in to the list of plug-ins (Configuration tab).

  16. Edit the Run Configuration. Add the "service" plug-in to the list of plug-ins.

  17. Run the application. Take care of the logger messages in the Console view. When the application frame appears, select the menu item "File" -> "List Users". You should see a list of user names in the Console view.

  18. Please note that the user managers of both plug-ins ("app" and "service") are available as OSGi services. You may control the ranking of the services by adding the "ranking" attribute to the service definition.

  19. "app" plug-in, bundle-context-osgi.xml:
    <service id="defaultUserManagerService" ref="defaultUserManager"
    interface="org.digitalcure.hellospring.common.IUserManager" ranking="5" />
  20. "service" plug-in, bundle-context-osgi.xml:
    <service id="extendedUserManagerService" ref="extendedUserManager"
    interface="org.digitalcure.hellospring.common.IUserManager" ranking="7" />
  21. Run the application once again and see what names are printed. You may now modify the ranking of the user manager service of the "service" plug-in to 3 and run the application again. You should see other names printed in the Console view.
You may download the source code of the tutorial from here.

2 comments:

stefan222 said...

Published part 3 of the tutorial too. Enjoy!

stefan222 said...

Added a new tutorial article about the export of this sample application here. Enjoy it!