Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

developerWorks Community:

  • Close [x]

Server-side mobile application development with IBM Worklight: Part 1. IBM Worklight adapter integration with Java business logic

Bhargav Perepa (bvperepa@us.ibm.com), IT Specialist, IBM Japan
Bhargav Perepa is a WebSphere architect and IT specialist in the IBM Federal Software Group in Washington DC area. Previously, he was a developer in the Austin WebSphere Development Lab and had Smalltalk and C++ development experience at IBM Chicago. Bhargav holds a master's degree in Computer Science from the Illinois Institute of Technology, Chicago, and a master's in business administration (MBA) from the University of Texas, Austin.

Summary:  This is Part 1 of a series that explains how to use IBM® Rational® Application Developer for WebSphere® Software and IBM® Worklight together to develop applications for mobile devices, including those that run Android, iOS, BlackBerry, and Microsoft Windows Metro operating systems. In this first article, Bhargav Perepa covers developing and testing a Java application in Rational Application Developer, creating a custom Java application logic package as a library to use in mobile application development, how to use Worklight to develop a server-side mobile application component to use that library, and how to use Worklight Studio to test the mobile application.

View more content in this series

Date:  30 Apr 2013
Level:  Introductory PDF:  A4 and Letter (1221 KB | 20 pages)Get Adobe® Reader®
Also available in:   Chinese

Activity:  8532 views
Comments:  

Introduction

IBM® Rational® Application Developer for WebSphere Software, Version 8.5.1 iFix1, facilitates speedy development, testing, and operational deployment of Java, Java Enterprise Edition (JEE), Web 2.0, mobile, OSGi, portal, and service-oriented architecture (SOA) business applications. For mobile application development, it includes productivity features for working with HTML 5, CSS3, JavaScript, Dojo toolkit, and JavaScript Object Notation (JSON) programming constructs. It also integrates seamlessly with IBM® WebSphere® Application Server programming models and supported runtime environments.

The IBM® Worklight® platform facilitates rapid development, testing, and delivery of mobile applications in a platform-neutral, vendor-agnostic manner across Android, iOS, BlackBerry, and Microsoft Windows Metro mobile operating systems. It includes these components:

Worklight Application Center
For keeping and sharing mobile applications among architects, developers, testers, and users across an organization as an enterprise application store.
 
Worklight Console
Uses the browser user interface for administering mobile applications as well as adapters.
 
Worklight Server
For testing and deployment of the mobile applications developed.
 
Worklight Device Runtime Layer
Runs on mobile devices, such as smartphones and tablets.
 
Worklight Studio
For development of native, hybrid, and standard web mobile applications. It supports development of mobile applications that are capable of meeting the fit-for-purpose criteria, spanning native, hybrid, or web programming models in a flexible manner. It also supports development of server-side mobile components, which are typically deployed on the Worklight server running on any one of the supported IBM® WebSphere® Application Server profiles (Liberty, Base, and Network Deployment).
 

In this article, you learn how to develop and test a Java application using the Rational Application Developer for WebSphere 8.5.1 iFix1 developer workbench. Subsequently, you will learn how to develop and test a mobile application featuring a server-side adapter component that uses the previously developed a Java application. The IBM Worklight framework supports development of server-side components that invoke business logic encapsulated in Java, web services, REST services, and Enterprise JavaBean (EJB) components.


Use case architecture

In this example based on fictitious customer requirements, you are required to develop a Java application using Rational Application Developer for WebSphere workbench and test the application running on WebSphere Application Server 8.5.0.2 runtime instance. Customer requirements call for extending the application logic developed and deployed to smartphones and tablets by leveraging the developed or deployed business logic through strategies of extension and transformation, rather than reinvention ("rip and replace"). The requirements define two concepts identified by Usecase and Usecases:

Usecase
This is an encapsulation of essential mobile application characteristics, representing client, server, and middleware aspects of the application. Individual rows in Figure 1 represent the Usecase concept.
 
Usecases
These are an encapsulation of the collection of possible individual use case instances or concepts. Nine possible combinations of Usecase instances are shown in Figure 1. This table represents the Usecases concept in this article.
 

Figure 1. Encapsulation of Usecase and Usecases requirements
Details such as client, type, server OS, backend

Larger view of Figure 1.

In your Java application, you capture the customer requirements by defining two interfaces: one to represent the Usecase concept, another to represent the Usecases concept. You define implementations of these two interfaces and one additional class to capture any type of exception or error condition that your logic might encounter. Table 1 summarizes the Java application development artifacts that you plan to develop in Rational Application Developer and then deploy and test on WebSphere Application Server.


Table 1. Java application artifacts
Sequence numberArtifact nameArtifact typeRelationshipRemarks
1 UsecaseInterface.java Java interface Abstract contract Abstract contract that defines the Usecase concept
2 Usecase.java Java class Implements UsecaseInterface Concrete implementation of the abstract contract UsecaseInterface
3 UsecasesInterface.java Java interface Abstract contract Abstract contract that defines the Usecases concept – as a finite or bounded collection of Usecase instances
4 Usecases.java Java class Implements UsecasesInterface Concrete implementation of the abstract contract, UsecasesInterface
5 UsecasesException.java Java class Implements user-defined exception Used commonly by implementations of both Usecase and Usecases


The use case implementation approach

The use case implementation consists of six distinct activities, broadly identified as these steps:

  • Develop a Java application using Rational Application Developer.
  • Test the Java application using Rational Application Developer.
  • Create a Java application logic package as a Java archive (.jar file) for mobile application use.
  • Develop a mobile application using Worklight Studio.
  • Incorporate the Java application logic provided as a Java archive library in a mobile application.
  • Test the mobile application using Worklight Studio, which also functions as an end-to-end testing of complete scenario.

Develop a Java application using Rational Application Developer

The following steps outline the Java application development using Rational Application Developer for WebSphere Version 8.5.1.

  1. Launch Rational Application Developer, and specify the location of the workspace on the file system:
    C:\temp\RADWS\WSUsecasesJavaProject
  2. Open the Java perspective, for Java application development if it is not opened already (Window > Open Perspective > Other > Java), and then click OK.
  3. Create a new Java project.
    1. Select the New Java Project creation wizard: File > New > Java Project.
    2. Enter the project name: UsecasesJavaProject
    3. Set the Java Runtime Environment (JRE) by selecting Use an execution environment JRE: JavaSE-1.6.
    4. Leave the defaults for the rest of options.
    5. Click Next and then click Finish.

The Package Explorer of the Java perspective now shows the newly created Java project.

  1. Create a new Java package.
    1. To create a new Java package, select usecasesJavaProject > src > New > Package option
    2. Accept the default value for source folder: use casesJavaProject/src
    3. Specify the Java package name to be com.worklight.customcode.
    4. Click Finish.
  2. Create a new Java class.
    1. Select use casesJavaProject > src > com.worklight.customcode > New > Class.
    2. Specify the Java class name to be usecasesException.
    3. Indicate the super class name to be java.lang.Exception.
    4. Click Finish.

At this point, a newly created Java class named usecasesException.java appears in the Package Explorer view of Java perspective. The UsecasesException.java class takes a string parameter and passes it on to its super class, java.lang.Exception.

  1. Create a new interface class named usecaseInterface:
    1. To create a new interface class in same package, select usecasesJavaProject > src > com.worklight.customcode package.
    2. To invoke the new Java interface creation wizard, select New > Interface.
    3. Enter the new Java interface class name as usecaseInterface.
    4. Specify the extended interfaces:
      java.io.Serializable
  2. Click Finish.

Note:
UsecaseInterface.java interface class defines public getter, setter methods for each defined class attribute and one method to implement the toString() capability.

  1. Create a new Java class named use case.
    1. Specify the package name for this class by selecting the usecasesJavaProject > src > com.worklight.customcode package.
    2. Select the new Java class creation wizard by clicking New > Class.
    3. Enter the class name: usecase
    4. Specify the extended interfaces:
      • java.io.Serializable
      • com.worklight.customcode.usecaseInterface
    5. Click Finish.
  2. Create a new interface class named usecasesInterface.
    1. Specify the package name by selecting usecasesJavaProject > src > com.worklight.customcode package.
    2. Open the new interface creation wizard by clicking New > Interface.
    3. Enter the interface name: usecasesInterface.
    4. Specify the extended interfaces: java.io.Serializable
    5. Click Finish.

You defined the UsecasesInterface.java class, which you can see in the Package Explorer view and can open in an editor. This interface class defines three public methods named getUsecase(id), getAllUsecases(), and getNumOfUsecasesInstances(). Using this interface contract, you can retrieve a Usecase instance by its unique ID or retrieve all Usecase instances as an array of Usecase instances and return Usecases instance attributes as a string.

  1. Create a new Java class named Usecases.
    1. Specify the package name by selecting the usecasesJavaProject > src > com.worklight.customcode package.
    2. Open the new Java class creation wizard by clicking New > Class.
    3. Enter the class name as usecases.
    4. Specify the extended interfaces:
      • java.io.Serializable
      • com.worklight.customcode.use casesInterface
  2. Click Finish.

You have now created the Usecases.java class. This class implements the interfaces defined previously and encapsulates the behavior of the Usecases concept.

  1. Open the source file of this class implementation and study it.

Test the Java application using Rational Application Developer

The following steps outline testing the Java application that you developed using Rational Application Developer.

  1. Build the project cleanly by selecting Project > Clean > Clean all projects, and then clicking OK.
  2. Run the application as a Java application by selecting UsecasesJavaProject > src > com.worklight.customcode > Usecases.java -> Run As > Java Application.

Figure 2 shows the correct execution of the Java application with output shown in the Console view.


Figure 2. Verify correct execution of application (Console tab view)
Console display of Java application execution

Larger view of Figure 2.

Create a Java application logic package as a Java archive for mobile application use

The following steps outline the packaging of Java application in order for its logic to be usable in mobile application.

  1. Navigate to the binary folder:
    C:\temp\RADWS\WSUsecasesJavaProject\UsecasesJavaProject\bin
  2. Compress the contents of com\worklight\customcode and save as the UsecasesJavaProjectUtils.jar file as shown in Figure 3.

The archive file contains the following classes under the com\worklight\customcode subdirectory (see Figure 3):

  • Usecase.class
  • UsecaseInterface.class
  • Usecases.class
  • UsecasesException.class
  • UsecasesInterface.class

Figure 3. Packaging the Java application as a usable library
Display of contents of Java archive library

Develop a mobile application using IBM Worklight Studio

The following steps outline mobile application development using IBM Worklight Studio Version 5.0.6.1.

Create a new project

  1. Launch Worklight Studio, and specify the location of the workspace on file system to be C:\temp\WRKLT\WSAdapterCall2Java.
  2. Create new Worklight project by using the project creation wizard: File > New > Worklight Project.
  3. Specify the Worklight project name as WLHTTPAdapterCall2JavaProject.
  4. Specify the Worklight project template to be Project Templates: HybridApplication.
  5. Click Next.
  6. Specify the application name as WLHTTPAdapterCall2Java.
  7. Leave the rest of the default values on this screen, and click Finish.

Create a new Worklight adapter

  1. To launch the new Worklight adapter creation wizard, select WLHTTPAdapterCall2JavaProject > adapters > Worklight Adapter.
  2. Start the wizard by clicking New > Worklight Adapter.
  3. Specify the Project name as WLHTTPAdapterCall2JavaProject.
  4. Specify Adapter type as HTTP Adapter.
  5. Specify Adapter name as Call2JavaAdapter.
  6. Click Finish.

Figure 4 shows the configured adapter metadata file in the editor.


Figure 4. Worklight adapter metadata in the editor
Specific details of adapter configuration file

Figure 5 shows the completed implementation of adapter logic in the editor. The adapter implements three methods in the JavaScript functions, and each of these functions call the Java application logic developed in previous steps. The Java application logic is made available to the mobile application in a Java archive library. The implementation demonstrates how to invoke Java application static methods, as well as Java application instance methods.


Figure 5. Worklight adapter implementation in the editor
Adapter function implementations in JavaScript

Start the Worklight server to deploy the adapter

To start the Worklight server, follow these steps:

  1. Select WLHTTPAdapterCall2JavaProject.
  2. Click the Start Worklight Server option.
  3. Deploy the Worklight adapter to the Worklight server:
    1. Select WLHTTPAdapterCall2JavaProject > adapters > Call2JavaAdapter.
    2. Select Run As > Deploy Worklight Adapter.

The Console view in Figure 6 confirms successful deployment of the Worklight adapter in Worklight Studio.


Figure 6. Verifying adapter deployment in the Worklight Studio console
Console display says adapter deployed successfully

Figure 7 shows successful deployment of Worklight Adapter in Worklight Admin Console.

  1. To verify, enter this URL in the Google Chrome browser (the preferred browser, given its support for web development):
    http://localhost:8080/console

Figure 7. Verifying adapter deployment, Worklight admin console display
Browser display of successful adapter deployment

Incorporate the Java application logic in the mobile application

The following steps describe how to import the Java application library to be used in mobile application being developed in Worklight Studio:

  1. Launch the import wizard to import the Java application library into your Worklight Studio workspace, to be deployed to Worklight server.
  2. Select WLHTTPAdapterCall2JavaProject.
  3. Navigate to the server by selecting server > lib > import... > File System > Next.
  4. Specify the Java application library to be used in the mobile application.
  5. Select the .jar file to import from file system:
    c:\temp\UsecaseJavaProjectUtils.jar
  6. Accept the default location to import the file in the workspace:
    WLHTTPAdapterCall2JavaProject /server/lib
  7. Click Finish.

Figure 8 shows the completed Worklight application project, ready for testing.


Figure 8. Worklight application project finished and ready for testing
Project Explorer shows Call2JavaAdapter

Add Google Chrome as the default browser

Figure 9 shows how to add the Chrome browser to the Worklight Studio workbench:

  1. Select these menu options: Windows > Preferences.
  2. Select General > Web Browser > Use external web browser.
  3. Click the Select New button.
  4. In the Name field, specify the name of the browser as Google Chrome.
  5. In the Location field, specify where the browser is installed:
    C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
  6. Click OK.
  7. To use Chrome as the default browser for testing, in External web browsers, check Google Chrome.
  8. Click Apply and then OK.

Figure 9. Worklight Studio configuration for the Chrome browser
Preferences and Add External Web Browser screens

Test the mobile application using Worklight Studio

The following steps outline the end-to-end testing of mobile application. In this mobile application execution, the Java application logic is invoked from the adapter component of the mobile application.

Invoke the getNumOfUsecasesInstances static adapter method

  1. For the static adapter method to test, select getNumOfUsecasesInstances.
  2. Select WLHTTPAdapterCall2JavaProject > adapters > Call2JavaAdapter.
  3. Select Run As > Invoke Worklight Procedure.
  4. Specify the Project name: WLHTTPAdapterCall2JavaProject.
  5. Specify the Adapter name: Call2JavaAdapter.
  6. Specify the Procedure name: getNumOfUsecaseInstances (see Figure 10).
  7. Click Apply and then Run.

Figure 10. Procedure testing, getNumOfUsecaseInstances
Invocation steps of getNumOfUsecasesInstances()

Figure 11 shows a successful invocation of getNumOfUsecasesInstances() static adapter method.


Figure 11. Successful invocation, getNumOfUsecasesInstances()
Invocation result confirms success

Larger view of Figure 11.

Invoke the getUsecase(4) instance adapter method

  1. Select getUsecase(4) instance adapter method to test.
  2. Select WLHTTPAdapterCall2JavaProject > adapters > Call2JavaAdapter.
  3. Select Run As > Invoke Worklight Procedure.
  4. Specify the Project name: WLHTTPAdapterCall2JavaProject.
  5. Specify the Adapter name: Call2JavaAdapter.
  6. Specify the Procedure name: getUsecase(4) (see Figure 12).
  7. Click Apply and then Run.

Figure 12. Procedure testing, getUsecase(4)
Configuration steps to invoke getUsecase(id)

Figure 13 shows successful invocation of getUsecase(4) instance adapter method.


Figure 13. Successful invocation, getUsecase(4)
getUsecase invocation result

Larger view of Figure 13.

Invoke the getAllUsecases() instance adapter method

  1. Select the getAllUsecases() instance adapter method to test.
  2. Select WLHTTPAdapterCall2JavaProject > adapters > WLHTTPAdapterCall2JavaProject.
  3. Select Run As > Invoke Worklight Procedure.
  4. Select Project name: WLHTTPAdapterCall2JavaProject.
  5. Select Adapter name: Call2JavaAdapter.
  6. Select Procedure name: getAllUsecases() (see Figure 14).
  7. Click Apply and then Run.

Figure 14. Procedure testing, getAllUsecases()
Configuration steps to invoke getAllUsecases()

Figure 15 shows successful invocation of getAllUsecases() instance adapter method.


Figure 15. Successful invocation, getAllUsecases()
Result details

Larger view of Figure 15.


Summary

Part 1 of this series of articles shows how to use the Rational Application Developer for WebSphere Software workbench to develop and test a Java application and then how to use Worklight Studio to develop and test a mobile application that makes use of logic developed in that Java application. In summary, this article demonstrates how to extend and transform the existing server-side business logic assets to the mobile domain without engaging in wholesale re-engineering by using IBM Rational Application Developer for WebSphere and IBM Worklight Studio developer workbench tools. Please see the Download section to download the article sample artifacts. In other articles in this series, you learn how to extend and transform a JAX-WS web service, a JAX-RS service, and an EJB 3.1 Java Persistence API (JPA assets to the mobile domain by using the same approach explained in this article.


Acknowledgments

The author gratefully acknowledges receiving review feedback, constructive suggestions, support, and productive teamwork from Sujatha Perepa (perepa@us.ibm.com) and Roger Snook (rcsnook@us.ibm.com), both with IBM.



Download

DescriptionNameSizeDownload method
Application sample, configuration and log filesRAD_UsecasesJavaProject.zip73KBHTTP

Information about download methods


Resources

Learn

Get products and technologies

Discuss

About the author

Bhargav Perepa is a WebSphere architect and IT specialist in the IBM Federal Software Group in Washington DC area. Previously, he was a developer in the Austin WebSphere Development Lab and had Smalltalk and C++ development experience at IBM Chicago. Bhargav holds a master's degree in Computer Science from the Illinois Institute of Technology, Chicago, and a master's in business administration (MBA) from the University of Texas, Austin.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Rational, Mobile development, WebSphere
ArticleID=900838
ArticleTitle=Server-side mobile application development with IBM Worklight: Part 1. IBM Worklight adapter integration with Java business logic
publish-date=04302013