Exploring Eclipse RCP

A "Rich" Alternative for System i GUI Development

January 3, 2007

How to modify the Eclipse RCP tutorial (part 1) to connect to a System i.

Previously I made a case for

1. Considering rich applications as one alternative to extend selected functionality of the System i to the desktop, and

2. using Eclipse RCP to create rich clients.

The last entry introduced Ed Burnette's Rich Client Tutorial to give hands on experience with Eclipse RCP. Part 1 of the tutorial walked you through creating a do-nothing basic Eclipse RCP application using the wizards. Part 2 of the tutorial explained the classes created by the wizard.

This entry adds a System i connection to to the rich application created in part 1 of the tutorial. The same basic Eclipse RCP application as part 1 of the tutorial will be created and then modified to connect to a System i using JTOpen or the System i java toolkit.

Creating a test project is very simple with the Eclipse RCP templates, probably faster than trying to unzip an archived starting point and get it into Eclipse. Repeat steps in Part 1 to create "Hello RCP" with these changes:

- name the project .eclipsercp.tutorial.mod1 instead of "org.eclipse.ui.tutorial.rcp.part1"
- change Application window title from "Hello RCP" to "Tutorial Mod 1"

otherwise take the defaults and follow the part 1 instructions. Stop after the step "Taking it for a spin".

The explanations of the classes in Part 2 of the tutorial apply for the most part to the new project just created by the wizard. There is one exception,the wizard creates "Activator.java" as the default (optional) activator class. In Part 2 of the tutorial this class is explained at the end under the heading "Plugin class", named "Part2Plugin.java". The wizard named this class "Activator.java".

Add jt400.jar to the project classpath

The Java toolkit which comes with the System i, or the open source JTOpen, is the java class library to provide java programs access to System i functions. jt400.jar is the primary archive.

To use jt400.jar in a java application you would add it to the classpath when compiling and executing the application. You also need to do this for Eclipse because JtOpen is not part of Eclipse.

1. Create a folder in the new project (right click the project > New > Folder) and call it "lib"

2. Import jt400.jar into the lib folder. Right click the folder > Import > File System

3. Add jt400.jar to the project classpath with these steps:
- open META-INF/MANIFEST.MF with PDE (double click on it)
- select "runtime" tab, third from left (see tutorial)
- in "Classpath" group press "Add" button. This opens a dialog titled "JAR Selection". The "lib" folder should be present. Open it and select jt400.jar.

4. Optionally attach the javadoc to the jt400.jar. You would want to do this so Eclipse can give you hover help and Shift F2 javadocs for JTOpen classes and methods.
- Right click jt400.jar which now in the project, probably immediately below the src folder
- Select properties
- Select javadoc location
- put in the location of the JTOpen javadocs. See the help dialog, question mark at bottom left, for more information.

Modify the Workbench Activator class

Part 2 of the tutorial explains the "Plugin class" which the wizard created as Activator.java. This class is optional,it does some setup before executing the application class and tear down when the application class completes. If not present the Application class is executed.

1. Import AS400 class from jt400.jar


import com.ibm.as400.access.*;

The IDE gives the mesage that the import cannot be resolved when it cannot find jt400.jar. If you get this message look in the previous part for the problem. jt400.jar is listed in the first level of the project when it is loaded. In PDE it is listed on the "Runtime" tab, in the classpath section as described previously. If you look at the project properties, in "Java Build Path", jt400.jar will be on the Library and "Order and Export" tabs.

2. Declare global variable for AS400


pivate static Activator plugin; // original code
private static AS400 as400;

3. In the constructor create an instance of the AS400 class.

public Activator() {
plugin = this; // original code
as400 = new AS400();
}

4. In the start() method connect to the System i, forcing the user to log on.

public void start(BundleContext context) throws Exception {
try {
as400.connectService(AS400.CENTRAL);
if ( !as400.isConnected() ) {
System.out.println("unable to connect to System i");
System.exit(0);
}
} catch(AS400SecurityException se) {
System.out.println("signon cancelled or security failure");
System.exit(0);
} catch(Exception e) {
e.printStackTrace();
throw e;
}

super.start(context); // original code
}


5. In the stop() method disconnect from the System i.

public void stop(BundleContext context) throws Exception {
plugin = null; // original code
super.stop(context); // original code
as400.disconnectAllServices();
as400 = null;
}

6. Add a method to get the AS400 object which was instantiated by the constructor and connected in the start() method. Make a copy of method getDefault() with cut and paste and change it,

/**
* Returns the shared AS400 object
*
* @return the shared AS400 object
*/
public static AS400 getAs400() {
return as400;
}

When the RCP application is started the Activator constructor creates a session instance of an AS400 object.

Before starting the Application class the start() method will try to connect to the central services of the System i, forcing the uesr to enter the name of the system, a user id and password. Then it will test the connection, failing and ending if it cannot connect.

Before ending the Application class the stop() method will disconnect for the System i.

Modify the Workbench Advisor class

In the interest of keeping it simple and getting something to happen quickly lets override two methods of the Workbench Advisor explained in Part 2 to do something with the AS400 object, just to prove the RCP application is connected to the System i


public void preStartup() {
super.preStartup();
System.out.println("Workbench Advisor preStartup() -- connected to System i: " +
Activator.getAs400().getSystemName());
}

public boolean preShutdown() {
if ( Activator.getAs400().isConnected() )
System.out.println("Workbench Advisor preShutdown() -- connected to System i!" );
else
System.out.println("Workbench Advisor preShutdown() -- not connected to System i!" );
return super.preShutdown();
}

Yes, cheezy but simple.

Try it! Run the project from within Eclipse. See Part 1 of the tutorial, "Taking it for a spin".

If the signon fails or is cancelled a message appears in the console.

If something truely bazzar happens when trying to connect to the System i a stack trace sent to the console.

After the connection is made the preStartup() method writes the name of the System i to the console, that is before the RCP application window appears.

The RCP application window appears, ta da.

When the RCP application is ended the connection is the preShutdown() method tests the connection and tells you if it is still connected to your System i.

Finaly the Activator disconnects all services and terminates the connection as part of the final clean up.

Discussion

The AS400 object was instantiated by the Activator class.

It could be instantiated in the Application class in either the preStartup() or postStartup() methods. You might want to do this so you can use the RCP Application facilities to communicate with the user, say the status line.

The RCP application preferences are availalbe at this point as are the system properties set by config.ini, so you could use these when creating the AS400 object. For example you could set a system property for the AS400 server name in config.ini then use it in the RCP application with System.getProperty().

The connection to the AS400 could be created in a view or editor when it was openned. It does not have to be "global" to the application. It depends on how you would use it.

Source code

Eclipse projects described in the blog, and other materials, will be posted in a CVS (Concurrent Versions System) version control system. Readers have access to the CVS. Eclipse has a built in CVS client which can view or check out these projects. A web based project browser is also available.

How to add a CVS repository to Eclipse for Exploring Eclipse RCP blog

1. First open up the CVS perspective. Click on "Window" -> "Open perspective" -> "Other" -> "CVS Repository Explorer"
2. Create a new Repository Location. Right click on the left window -> "New" -> "Repository Location".
3. Fill out the form


  • Host: cvsdude.org

  • Repository path: /cvs/billblalock

  • User: systeminetworkblog

  • Password: eclipsercp

  • Connection type: pserver

  • Select "Use default port" radio button

  • Check "Save password"

4. Open the repository
5. Open HEAD
6. The projects will be listed. They can be explored through the CVS perspective or checked out.

Web access

The link for web access to the CVS repository for Exploring Eclipse RCP blog is http://cvsdude.com/vcvs/billblalock


  • User: systeminetworkblog

  • Password: eclipsercp

Eclipse projects will have a "src" folder. That is where the Java source will be found.


Thanks to CVSDude for hosting the materials for this blog at CVSDude : CVS, Subversion, Trac source control and project management hosting

Posted by Bill Blalock at January 3, 2007 4:41 PM

Comments

Post a comment




Remember Me?


Bill Blalock
October 2008
Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  

Blog Policy

We welcome your comments and opinions and encourage lively debate on the issues. However, Penton Media reserves the right to delete or move any content that it may determine, in its sole discretion, violates or may violate its Terms of Use or is otherwise unacceptable. For more information, see Penton Media's Terms of Use.

ProVIP Sponsors