OSGi blog sample application

The blog sample application is a traditional blogging application, used for publishing essay-length articles and allowing readers to comment on them. This sample application shows how to write and package bundles into an enterprise bundle archive (EBA) file. The sample includes example code for Blueprint management, bean injection, using and publishing services from and to the OSGi service registry, and the use of Java™ persistence.

Before you begin

The blog sample application consists of five bundles, but can be run with four bundles because the fifth bundle is an upgrade to the persistence bundle.

The sample application requires that the supplied com.ibm.samples.websphere.osgi.logging.api.jar and com.ibm.samples.websphere.osgi.logging.impl.jar bundles are installed into the internal bundle repository. The following procedure describes how to do this. These two JAR files, and the blog sample EBA file, are provided in the installableApps directory of the OSGi_blogSample.zip compressed archive file.

About this task

The bundles are divided into the following functional areas:
  • com.ibm.samples.websphere.osgi.blog.persistence, which contains code relating to the Java Persistence API (JPA) layer, and also contains interfaces that are used by the main application code to update and query blog entries.
  • com.ibm.samples.websphere.osgi.blog_1.0.0, which contains the main application logic code, and interacts between the web front end and the back end persistence code layer.
  • com.ibm.samples.websphere.osgi.blog.web, which contains the static web content and backing Java code for the web front end for the application.
  • com.ibm.samples.websphere.osgi.blog.api, which contains the API for the whole sample.
  • com.ibm.samples.websphere.osgi.blog.persistence_1.1.0, which contains an upgraded persistence bundle that also supplies a comment service.

You can use scripts to completely install the sample application, or you can use scripts to complete the initial configuration of the application then use the administrative console to install the application into the application server. You can also use, modify and remove the sample, and upgrade the persistence service that is provided by the sample.

Procedure

Configure and install the blog sample.

You can either configure and install the sample using scripts (the first optional step in the instructions that follow), or you can configure the sample using scripts then install the sample using the administrative console (the second optional step in the instructions that follow).

The scripts that you use to do this are provided in the scripts directory of the OSGi_blogSample.zip compressed archive file. The createBlogDb.sql script contains the necessary configuration commands to create the required Derby database and associated tables. The blogSampleInstall.py script contains the required jython to create data sources, and to install the blog sample with default configuration. You should fully qualify the path to the script if you do not run it from the directory that contains the script.

Note: If you have data sources already defined in your environment, and these data sources have the same name as the data sources that are specified in the blogSampleInstall.py script, then the sample might not install and run successfully.

In the following steps you must substitute your own values for the variables app_server_root, profileName, serverName, nodeName, blogSample.eba_Location, com.ibm.samples.websphere.osgi.logging.api.jar_Location, com.ibm.samples.websphere.osgi.logging.impl.jar_Location, uncompressed_sample_directory, and path_to_ant.

  • Optional: Configure and install the sample using scripts.
    1. Create and configure the BLOGDB Derby database and associated tables by running the following command.
      On UNIX platforms:
      app_server_root/derby/bin/embedded/ij.sh scripts/createBlogDb.sql
      On Windows platforms:
      app_server_root\derby\bin\embedded\ij.bat scripts\createBlogDb.sql
    2. Create the data sources, create the business-level application and import the .eba file as an asset by running the following command.
      Note: The target application server must be running before you use this script.
      On UNIX platforms:
      app_server_root/profiles/profileName/bin/wsadmin.sh 
           -f scripts/blogSampleInstall.py fullInstall serverName nodeName 
           blogSample.eba_Location 
           com.ibm.samples.websphere.osgi.logging.api.jar_Location
           com.ibm.samples.websphere.osgi.logging.impl.jar_Location
      On Windows platforms:
      app_server_root\profiles\profileName\bin\wsadmin.bat 
           -f scripts\blogSampleInstall.py fullInstall serverName nodeName 
           blogSample.eba_Location 
           com.ibm.samples.websphere.osgi.logging.api.jar_Location
           com.ibm.samples.websphere.osgi.logging.impl.jar_Location
  • Optional: Configure the sample using scripts, then install the sample using the administrative console.
    1. Create and configure the "BLOGDB" Derby database and associated tables by running the following command.
      On UNIX platforms:
      app_server_root/derby/bin/embedded/ij.sh scripts/createBlogDb.sql
      On Windows platforms:
      app_server_root\derby\bin\embedded\ij.bat scripts\createBlogDb.sql
    2. Create the data sources by running the following command.
      On UNIX platforms:
      app_server_root/profiles/profileName/bin/wsadmin.sh 
           -f scripts/blogSampleInstall.py setupOnly serverName nodeName
      On Windows platforms:
      app_server_root\profiles\profileName\bin\wsadmin.bat 
           -f scripts\blogSampleInstall.py setupOnly serverName nodeName 
    3. Use the administrative console to add the com.ibm.samples.websphere.osgi.logging.api.jar file to the internal bundle repository.
      1. Navigate to Environment > OSGi bundle repositories > Internal bundle repository
      2. Click New.
      3. Select the file system that hosts the com.ibm.samples.websphere.osgi.logging.api.jar file, then click Browse.
      4. Browse to the uncompressed_sample_dir/installableApps directory.
      5. Select the com.ibm.samples.websphere.osgi.logging.api.jar file, then click OK.
      6. Click OK.
      7. Click Save.
    4. Repeat the previous step to add the com.ibm.samples.websphere.osgi.logging.impl.jar file to the internal bundle repository.
    5. Use the administrative console to import the application (EBA file) as an asset, and to configure the business-level application.
      1. Import the asset.
        1. Navigate to Applications > New application > New Asset.
        2. On the Upload asset panel, browse to the uncompressed_sample_dir/installableApps directory.
        3. Select the com.ibm.samples.websphere.osgi.blog.eba file, then click Next.
        4. On the Select options for importing an asset panel, click Next.
        5. On the Summary panel, click Finish.
        6. Click Save.
      2. Create the business-level application and add the asset.
        1. Navigate to Applications > New application > New Business Level Application.
        2. On the New Application panel, enter a name for the business-level application. For example, blog.
        3. Click Apply.
        4. In the Deployed assets pane, click Add > Add Asset.
        5. Select the com.ibm.samples.websphere.osgi.blog.eba asset, then click Continue.
        6. On the Set options panel, click Next.
        7. Select a target for the composition unit, then click Next.
        8. Modify the context root if required, then click Next.
        9. Modify the virtual host if required, then click Next.
        10. On the Summary panel, click Finish.
        11. Click Save.
    6. Start the application.
      1. Select the newly-created business-level application.
      2. Click Start.

Use the blog sample.

  • Use your browser to navigate to http://server:port/context_root/ (by default http://localhost:9080/blog/).
    The View blog screen is displayed.
  • Register yourself as an Author.
    You need to do this before you can create blog entries.
    1. Click Create Author.
    2. Complete the form.
    3. Click Submit.
  • Create a blog entry.
    1. Navigate back to http://localhost:9080/blog/ (or click Blog Home).
    2. Click Create New Post.
    3. Complete the form. Use the email address that you specified when you created the Author.
    4. Click Submit.
    The View blog screen is redisplayed, and should contain your post.

Upgrade the persistence service.

To add the new persistence bundle to the blog application, use the administrative console to load the com.ibm.samples.websphere.osgi.blog.persistence_1.1.0.jar file into the internal bundle repository, then modify the asset to expect a new bundle version. Complete the following steps:

  • Load the persistence bundle into the internal bundle repository.
    1. Navigate to Environment > OSGi bundle repositories > Internal bundle repository.
    2. Click New.
    3. Browse to the uncompressed_sample_dir/installableApps directory.
    4. Select the com.ibm.samples.websphere.osgi.blog.persistence_1.1.0.jar file, then click OK.
    5. Save your changes to the master configuration.
  • Add the persistence bundle to the blog application.
    1. Navigate to Applications > Application types > Assets.
    2. Click com.ibm.samples.websphere.osgi.blog.eba to view the asset information.
    3. Scroll to the end of the asset information, then click Update bundle versions in this application.
    4. Find the com.ibm.samples.websphere.osgi.blog.persistence bundle in the list.
    5. Click the down arrow next to the text no preference.
      You are offered a choice between version 1.0.0 or 1.1.0 of the persistence bundle.
    6. Select version 1.1.0, then work through the preview, create and save screens.
  • Update the application to use the new bundle version.
    1. Navigate to Applications > Application types > Business-level applications.
    2. Click the blog sample application to display its details.
    3. Click com.ibm.samples.websphere.osgi.blog.eba to view the asset information.
    4. Click Update to latest deployment, then click OK.
    5. Save your changes to the master configuration.
  • Stop and restart the blog application.
    1. Navigate to Applications > Application types > Business-level applications.
    2. Select the check box next to the blog sample application, then click Stop.
    3. Wait until the status column shows that the application has stopped, then click Start.
    4. Use your browser to navigate to http://server:port/context_root/ (by default http://localhost:9080/blog/) then refresh the screen.
      The View blog screen is displayed. There is a new option to add comments to blog posts.

Modify the blog sample

All the source code for this application is provided in sub-directories under the uncompressed_sample_dir directory. Each part of the project has its own ant build.xml script. To build the whole application into a newly-available EBA file, you use the ant build.xml file located in the uncompressed_sample_dir directory. WebSphere® Application Server ships a version of ant in its bin directory called ws_ant. To build the sample, you also need a JAR file on the ant classpath. To simplify matters, you can edit the was.root property in the supplied build.properties file to point to this JAR file.

  • Put the JAR file j2ee.jar on the ant classpath:

    This JAR file is available in the dev/JavaEE directory of Websphere Application Server. Edit the was.root property in the supplied uncompressed_sample_dir/scripts/build.properties file, and point it to your app_server_root directory. If you have copied the JAR file to another location, modify the was.root property to point to this location.

  • Run the following ant command from the uncompressed_sample_dir directory:
    path_to_ant -propertyfile scripts/build.properties -buildfile build.xml
    The newly-built binary file (EBA file) is in the uncompressed_sample_dir/output directory.
    Note: This is the only location to which this EBA file is written. The script does not overwrite the original binary file located in the uncompressed_sample_dir/installableApps directory.

Remove the blog sample.

To remove the blog sample, you complete the following 3 steps:
  1. Remove the application configuration and the data sources, either by using a script (the first optional step in the instructions that follow), or by using the administrative console (the second optional step in the instructions that follow).
  2. Remove the shared logging bundles from the internal bundle repository.
  3. Remove the database.
Note: Only remove the bundles if you have no other applications installed that use them. Both the blog and blabber sample applications use the shared logging bundles, so if both applications are installed these bundles should not be removed.

The uninstall script is provided in the scripts directory of the OSGi_blogSample.zip compressed archive file. The blogSampleUninstall.py script contains the required jython to remove data sources and to remove the installation of the blog sample with default configuration. You should fully qualify the path to the script if you do not run it from the directory that contains the script.

In the following steps you must substitute your own values for the variables app_server_root, and profileName.

  • Optional: Remove the application configuration and the data sources by running the following command.
    Note: The target application server must be running before you use this script.
    On UNIX platforms:
    app_server_root/profiles/profileName/bin/wsadmin.sh 
              -f scripts/blogSampleUninstall.py
    On Windows platforms:
    app_server_root\profiles\profileName\bin\wsadmin.bat 
              -f scripts\blogSampleUninstall.py
  • Optional: Remove the application configuration and the data sources by using the administrative console.
    1. Remove the application configuration.
      1. Navigate to Applications > Application types > Business-level applications.
      2. Select the business-level application representing the blog sample application, then click Stop.
      3. Click the business-level application representing the blog sample application to view the configuration details.
      4. Select all the deployed assets, then click Delete.
      5. Click OK to confirm removal.
      6. Click Save.
      7. Select the business-level application representing the blog sample application, then click Delete.
      8. Navigate to Applications > Application types > Assets.
      9. Select the com.ibm.samples.websphere.osgi.blog.eba asset, then click Delete.
      10. Click OK to confirm removal.
      11. Click Save.
    2. Remove the data sources.
      1. Navigate to Resources > JDBC > Data sources.
      2. Select the two data sources configured for the blog application.
      3. Click Delete.
      4. Click Save.
  • Remove the shared logging bundles from the internal bundle repository by using the administrative console.
    1. Navigate to Environment > OSGi bundle repositories > Internal bundle repository.
    2. Select the following bundles:
      • com.ibm.samples.websphere.osgi.logging.api.jar
      • com.ibm.samples.websphere.osgi.logging.impl.jar
      • com.ibm.samples.websphere.osgi.blog.persistence_1.1.0
    3. Click Delete.
    4. Click Save.
  • Remove the database.
    On UNIX platforms, open a command prompt, then enter the following command:
    cd app_server_root/derby
    rm -fr BLOGDB

    On Windows platforms, delete the app_server_root\derby\BLOGDB directory.

Results

The following illustration shows the correctly running sample:

A screen capture of the Blog application Home page. There is a banner labeled "Blog Home", and a blank page under the banner. At the top of the blank page are two options: "CREATE NEW POST" and "CREATE AUTHOR".