1. Non-web app applicability

By now you probably agree that RIFE is a powerful framework for developing Java web applications. However, RIFE is also useful when developing non-web applications, like a standalone server and GUI applications. Let's take a look at what RIFE can offer in this area.

Using RIFE for non-web application development gives several benefits, for example RIFE's scheduler, the database connectivity layer, and the authentication layer.

It can also be of good help when developing the backend for a web application, since it makes it easy to test it without having to run it inside a full blown web application server. The next few sections will give an example of how to do this.

1.1. Writing a non-web application

To make an application work both inside an application server and as a standalone application, we need a custom participant that launches the actual application. This way the application can be launched either from the main method or when RIFE is initializing all the participants in the repository.

We'll write a very simple participant called TestParticipant to demonstrate this. First, we add it to the repository:

Example 12.1. Participant file defining TestParticipant

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE rep SYSTEM "/dtd/rep.dtd">

<rep>
  <participant param="rep/test.xml">TestParticipant</participant>
</rep>

The actual implementation of the participant must be implement the interface com.uwyn.rife.rep.Participant. For example, like in the TestParticipant.java below:

Example 12.2. Implementation of a Participant

package com.imendio.test;
      
import com.uwyn.rife.rep.RepParticipant;
import java.util.ArrayList;
                                                                                
public class TestParticipant implements Participant
{
  TestInstance instance;
                                                                               
  public TestParticipant() 
  {
    // Set the message shown in the log when starting participant
    setInitializationMessage("Starting test...");
  }
                                                                                
  public void initialize() 
  {
    instance = new TestInstance();
  }
                                                                                
  public Object getObject()
  {
    return getObject(null);
  }
                                                       
  public Object getObject(Object o) 
  {
    return instance;
  }
                                                                                
  public List getObjects()
  {
    return getObjects(null);
  }
                                                       
  public List getObjects(Object o) 
  {
    ArrayList objectsList = new ArrayList();
    objectsList.add(mServer);
    return objectsList;
  }
}

As you can see we have a TestInstance object that is created in the initialize method. This is the actual class that does all the work, and the participant is just a container class to launch the instance. To access the instance from the rest of the application, we just call (TestInstance)participant.getObject().

Now the application is running as a participant in an application server. How do we start it from a non-web application?

Example 12.3. Starting a participant from the main method

public class TestApp
{
  TestInstance instance;
                                                                                
  public static void main (String args[]) 
  {
    ResourceFinder resourceFinder = ResourceFinderClasspath.getInstance();
    Rep.initialize("rep/participants.xml", resourceFinder);
                                                                                
    TestInstance instance = (TestInstance) Rep.getParticipant("TestParticipant").getObject();
  }
}

As you can see we use the same methods for accessing our application from the main method as we do from within a web application.