03 - Let us say Hello in EJB

Let’s Say 'Hello'

In this chapter, we will try to give an overview of most of the technologies involved in the EJB3 development from coding on the IDE to packaging, deployment and finally testing.

Hello Word EJB3 Application [Currency Converter Web Service]

The problem

We need to create a web service that can be used for currency conversion service, the web service accepts the amount of money in specific currency and converts it to another currency, and the service will mainly avail the following:

  • Converting from USD to GBP
  • Converting from GBP to USD
  • Converting from USD to EURO
  • Converting from EURO to USD

The service should be secured, also should have Authentication - who is allowed to access the service - and authorization - what authenticated users are allowed to do - method applied as a security model.

Also the web service should avail an action logging module that logs in the backend database all the actions done by the web service clients.

What we are going to learn from this Hello World Application

  • Using Eclipse IDE to develop and packaging EJB3 and Web Services applications
  • Using JBOSS 7.x to deploy the final package
  • Securing Web Services using Authentication/Authorization method
  • Using JPA to perform database operations
  • Using Transaction Management for backend database access

Preparing the development environment

For installing MySQL, you can refer to the following article https://dev.mysql.com/doc/refman/5.5/en/installing.html

For installing JBOSS tools on Eclipse IDE, you can refer to the following article http://tools.jboss.org/downloads/installation.html

Creating the database, and the database test data

From the problem description, we will need 2 tables for the actions logging ACTION_LOG and ACTION_LOG_PARAMETERS - can be one table, but for the aim of explaining transaction management we will use 2 tables - and 3 table for the authentication and authorization model S_USER for holding the web service registered client and S_ROLE for holding all the application roles - methods that should be secured - and finally S_USER_ROLE to map between each user and the allowed roles - means it will hold the answer of Who is allowed to access what.

So the first step is to create the database schema, us the following query:

        Create database test_ejb3_db;

2nd make the test_ejb3_db the active schema:

        Use test_ejb3_db;

Now, let’s create the 5 tables:

       CREATE TABLE IF NOT EXISTS 's_user' (
         'ID' int(100) NOT NULL AUTO_INCREMENT,
         'USERNAME' varchar(100) NOT NULL,
         'PASSWORD' varchar(100) NOT NULL,
         PRIMARY KEY ('ID'),
         UNIQUE KEY 'USERNAME' ('USERNAME')
       )

      CREATE TABLE IF NOT EXISTS 's_role' (
         'id' int(11) NOT NULL,
         'name' varchar(500) NOT NULL
      )

    CREATE TABLE IF NOT EXISTS 's_user_role' (
      'user_id' int(11) NOT NULL,
      'role_id' int(11) NOT NULL
     )

   CREATE TABLE IF NOT EXISTS 'action_log' (
     'id' int(11) NOT NULL AUTO_INCREMENT,
     'username' varchar(500) NOT NULL,
     'action_time' datetime NOT NULL,
     PRIMARY KEY ('id')
   )

   CREATE TABLE IF NOT EXISTS 'action_log_descripton' (
     'action_log_id' int(11) NOT NULL,
    'description' varchar(500) NOT NULL
   )

The final step in the database preparation, Inserting the test data into the database, we will assume that there are only 2 clients registered in our service, use the following queries:

     insert into s_user (username, password) values ('client1', 'welcome'), ('client2', 'welcome')

The application roles:

Insert into s_role (id, name) values (0, 'CLIENT'), (1, 'FROM_USD_TO_GBP'), (2, 'FROM_GBP_TO_USD'), (3, 'FROM_USD_TO_EURO'), (4, 'FROM_EURO_TO_USD')

Now, the assignment between the roles and the registered users (who are allowed to do that), we will assume that client1 is allowed only to access “USD to GBP” and “GBP to USD” and client 2 is allowed only to access “USD to EURO” and “EURO to USD”:

      insert into s_user_role (user_id, role_id) values (1, 0), (1, 1), (1, 2), (2,0), (2,3), (2, 4)

That’s all for the database, now lets move to the 3rd step in our web service development,

Using Eclipse IDE to Develop the Web Service

After installing JBOSS tools on Eclipse IDE, open Eclipse IDE and use a new Workspace for our tutorial projects, the 1st step is to configure JBOSS AS 7.1 on Eclipse IDE:

Configuring JBOSS AS 7.1 on Eclipse IDE

From the perspective window, choose Java EE, if not open by default:

                 

 

              

 

Open the Servers View

               

 

From the Servers View, choose to create new server:

        

This will open the create New Server dialog, choose JBOSS AS 7.1 from the servers tree, will be found under JBOSS Community folder:

             Fig define a new Server

 

   Keep the Server’s host name and Server name as is, now click the next button

           

Like us on Facebook