WLSKB-010 DEVELOP CUSTOM JAVA CONTROLS IN WEBLOGIC WORKSHOP TUTORIAL

DEVELOP CUSTOM JAVA CONTROLS IN WEBLOGIC WORKSHOP TUTORIAL

INTRODUCTION

Custom Java Controls can be used to develop the model layer in the Model View Controller pattern. It is a technology introduced by WebLogic Workshop as an alternative to Session EJB or Plain Old Java Objects (POJO). Controls have a few advantages:

  1. The execution environment is somewhat more lightweight than an EJB container.
  2. Unlike POJOs and like EJBs, controls execute within a transaction context. By default, a transaction is committed. To rollback the transaction, a control method needs to throw an exception.
  3. Unlike POJOs and like EJBs, a control has a managed life cycle. Lifecycle callback methods can be used to perform one time costly initialization and clean up.
  4. A control method that is confgured to be “buffered” executes asynchronously. Implementing offline processing becomes very easy and you don’t have to configure JMS objects such as queues and queue connection factory.

In this tutorial, you will develop a custom control.

DEVELOP THE CUSTOM CONTROL

Launch WebLogic Workshop. Create a new application called BasicApp (unless it is already created from a previous tutorial). Within the BasicAppWeb project create a folder called ControlTest.

Right click on ControlTest and select New->Java Control.

Select Custom. Enter ItemManagerImpl as the file name. (Note: The control file name must end with “Impl”). Click on Create. This will create the implementation file ItemManagerImpl.jcs and the control interface file ItemManager.java.

System automatically opens ItemManagerImpl.jcs in the editor.

Right click on the diagram and select Add Method. Change the method name to updateItemName. Similarly, add another method called getItemName.

Switch to the Source View.

Add the following import statements.

import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import java.util.*;

Now, we will implement the lifecycle methods. The Context member variable is equivalent to the SessionContext in EJB. The onAcquire method serves the same purpose as the setSessionContext() method for an EJB. It is called once per life time of an instance. Add the context member variable and the onAcquire method as follows.

/**
 * @common:context
 */
public ControlContext myCtx;
DataSource ds;
 
public void myCtx_onAcquire() throws Exception   {
    Context ctx = new InitialContext();
           
    ds = (DataSource) ctx.lookup("cgDataSource");
}

Notice the naming convention of the onAcquire method. Next, change the implementation of the updateItemName method to as follows.

public void updateItemName(int itemId, String name) throws Exception
{
    Connection con = ds.getConnection();
    PreparedStatement ps = con.prepareStatement(
        "update items set ITEMNAME=? where itemnumber=?");
    ps.setString(1, name);
    ps.setInt(2, itemId);
 
    ps.executeUpdate();
    ps.close();
    con.close();
}
public String getItemName(int itemId) throws Exception
{
    String name = "";
    Connection con = ds.getConnection();
    PreparedStatement ps = con.prepareStatement(
        "select ITEMNAME from ITEMS where itemnumber=?");
    ps.setInt(1, itemId);
    ResultSet rs = ps.executeQuery();
 
    if (rs.next()) {
        name = rs.getString("ITEMNAME");
    }
 
    rs.close();
    ps.close();
    con.close();
 
    return name;
}

UNIT TEST

Right click on MyControlImpl.jcs and select Generate Test JWS File (Stateless). This will create a web service called ItemManagerTest.jws. Double click on the JWS file. From the menubar select Debug->Start.

In the getItemName section, enter 625 as the item ID. Click on the getItemName button.

In the result page, scroll down and locate the Service Response section. Make sure that you see water bottle as the name of the item.

Scroll up. Click on the Test Operations link.

In the updateItemName section, enter values as shown above. Click on updateItemName.

Test the getItemName function again and make sure that changes have taken place.