Intro to Orion, Part 1

Objective

While there are other beginner's guides to Orion, I feel that none really focus on working with Sun's standard utilities. This guide is meant to introduce you to Orion, and show you how to begin developing using the javac compiler, the jar utility, and Sun's deploytool.

Part 1 shows you how to create your first Enterprise Java Bean and JSP, and deploy them on Orion. Part 2 will show you how to use helper classes and connect to an Oracle database.


Getting Started

This guide assumes you are using Windows NT or 2000 as your OS.

First, download and install the JDK 1.3 and J2EE packages, as well as Orion. (If you have not done so already)

It is recommended that you use the default installation folders (this guide will assume that you have done so).

Next, create the following directory structure:
c:\orionapp
c:\orionapp\public_html
c:\orionapp\classes
c:\orionapp\classes\orionapp
c:\orionapp\classes\orionapp\objects
c:\orionapp\classes\orionapp\utils
c:\orionapp\classes\orionapp\beans
c:\orionapp\classes\orionapp\beans\Info
All of your JSPs, images, HTML files, etc. will be placed in the public_html folder. We will create a session bean called Info and place it in the orionapp\beans\Info folder. The objects and utils folders will be used for additional helper classes in Part 2 of this guide.

Now go to Start > Settings > Control Panel > System. Find the environment variables setup ("Advanced" on Win2000). Under "User variables" add:
variable J2EE_HOME, value: c:\j2sdkee1.2.1 (or the folder you installed to)
variable JAVA_HOME, value: c:\jdk1.3 (same as above)

Now under System Variables, select PATH and edit it. Go to the end of the variable value and add ;c:\jdk1.3\bin;c:\j2sdkee1.2.1\bin

Also under System Variables, select CLASSPATH and edit it. Go to the end of the variable value and add ;c:\orionapp\classes. This is the folder that you just created.


Creating the EJB

Now your system should be set up, so let's create the Info bean first. Copy the following code into a file and save it as Info.java. Make sure you save it to your orionapp\classes\orionapp\beans\Info folder.

package orionapp.beans.Info;

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

public interface Info extends EJBObject 
{
    public String getMessage(String name) throws RemoteException;
}

This is the remote interface of your Info bean. The remote interface lists the methods that will be accessible by the JSPs. This bean contains only a single method at the moment.

The next bit of code is the home interface. Copy this code and save it as InfoHome.java.

package orionapp.beans.Info;

import javax.ejb.*;
import java.rmi.RemoteException;

public interface InfoHome extends EJBHome
{
    public Info create() throws CreateException, RemoteException;
}

The home interface for session beans is quite basic. It only contains a create method declaration.

Finally, here is the code for the bean itself. Save this code in a file called InfoBean.java.

package orionapp.beans.Info;

import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;

public class InfoBean implements SessionBean 
{
    public String getMessage(String name)
    {
        String rtn = "Hello, " + name + "!";
        return rtn;
    }
    
  public void ejbCreate() { }
  public void setSessionContext(
      SessionContext ctx) { }
  public void ejbRemove() { }
  public void ejbActivate() { }
  public void ejbPassivate() { }
  public void ejbLoad() { }
  public void ejbStore() { }
}

Those methods at the end are not important right now. What is important is that we are implementing the method getMessage declared in the remote interface. All we need now is a JSP.

Save the following code in a file named index.jsp. Make sure you save this file in the orionapp/public_html folder.
     
<%@ page import="
    javax.naming.*,
    javax.ejb.*,
    java.rmi.*,
    javax.rmi.PortableRemoteObject,
    orionapp.beans.Info.*"
%>

<HTML><HEAD><TITLE>Test</TITLE></HEAD>
<BODY>

<%
    // variables for the Info bean
    InfoHome        iHome           = null;
    Info            iBean           = null;
    
    // check for form data
    String          name            = request.getParameter("txtName");
    
    // variable for String returned from Info bean
    String          msg             = null;

    // if form data was submitted....
    if (name != null)
    {
        try
        {
            InitialContext ctx = new InitialContext();
            iHome = (InfoHome)PortableRemoteObject.narrow(ctx.lookup("java:comp/env/ejb/orionapp/beans/Info"), InfoHome.class);
            iBean = iHome.create();
        } 
        catch(Exception e)
        {
            e.printStackTrace();
        }

        msg = iBean.getMessage(name);
%>

Here is your message: <%= msg %>

<%
    } // end if (name != null)
    else
    {
%>

<FORM METHOD="get" action="index.jsp">
<BR>Enter your name: <INPUT TYPE="text" name="txtName"><BR>
<INPUT TYPE="submit" value="Submit"><BR>
</FORM>

<%
    } // end else
%>

</BODY></HTML>



Assembling the Application

Now that you have all your files, it is time to assemble everything. First, compile the bean classes. Open a DOS window by typing cmd under Start > Run. Type cd\orionapp\classes to get to your classes folder.

Now type javac orionapp\beans\Info\*.java
This will compile your bean classes. It is important that you compile them from the classes folder, instead of going to the Info folder and typing "javac *.java".

Next, open a new DOS window and type deploytool. If you set your PATH correctly, this will start Sun's deploytool utility. This utility can be used to assemble the EAR file that J2EE applications are commonly packaged in. This utility will also create the necessary XML files for the application, freeing you from that task.

In the deploytool console, go to File > New Application. Browse to c:\orion\applications and name the file orionapp.ear. Click New Application.

Go to File > New Enterprise Bean. This will start the EJB wizard. Click Next.

For Jar Display Name, enter Info. Leave the Manifest Classpath window empty, but click Add next to the Contents window.

Click on Browse for the Root Directory. Click to c:\orionapp, then highlight classes and click on Choose Root Directory. Make sure it says c:\orionapp\classes as your Root Directory, as having the classes folder as your root directory is very important.

In the window below Root Directory, click through orionapp, then beans, then Info. Highlight the three files that end in .class by holding Ctrl as you click them, then click on Add. The bottom window should show:
orionapp\beans\Info\Info.class
orionapp\beans\Info\InfoBean.class
orionapp\beans\Info\InfoHome.class

Click OK. Click Next.

For Enterprise Bean Class, select orionapp.beans.Info.InfoBean
For Home Interface, select orionapp.beans.Info.InfoHome
For Remote Interface, select orionapp.beans.Info.Info

Bean Type should be session, and should be stateless.

For the Display Name, enter Info.

Click Finish, because we will not use the advanced options presented in the rest of the wizard.

We have added our Info bean. Now we will add the web component. Go to File > New Web Component. This will start the web component wizard. Click Next.

For WAR Display Name, enter public_html.

Under Contents, click Add. For Root Directory, navigate to c:\orionapp, highlight public_html and click Choose Root Directory. Your Root Directory should end in public_html.

Highlight index.jsp and click Add. Click Next, then Finish.

Click Next. Select JSP. Click Next.

For JSP File Name, select index.jsp. For Web Component Display Name, enter index.jsp. Click Finish, because we will not use the advanced settings presented in the rest of the wizard.

Now in the Local Applications window, under orionapp, click on public_html. Click on the EJB References tab.

Click Add. Click the box under Coded Name and enter ejb/orionapp/beans/Info. Make sure Type is Session. Under Home, enter orionapp.beans.Info.InfoHome. Under Remote, enter orionapp.beans.Info.Info.

Now go to Tools > Verifier. Click OK. The verifier will run, and you should get no errors. Do not worry if you get a warning. Click Close.

Got to File > Save, and you are done!


Deploying in Orion

Now you must deploy your application. Open the file default-web-site.xml under Orion's config folder. Add the following line, before the </web-site> tag:
<web-app application="orionapp" name="war-ic" root="/orionapp" />
Open the file server.xml, also under Orion's config folder. Add the following line, before the </application-server> tag:
<application name="orionapp" path="../applications/orionapp.ear" />

That's it! You are ready to test your application! Open a new DOS window, and type cd\orion, then java -jar orion.jar to start Orion. Wait for it to deploy your application, then open a Browser and type http://localhost/orionapp/index.jsp as the URL. If all goes well, your application will work!

Now feel free to go back and change the method in the bean, or add new methods or JSPs. When you make a change to the bean, you must recompile the java classes, then in the deploytool utility, go to Tools > Udate Application Files. Then go to File > Save, and Orion will automatically detect the changes and redeploy the application. When updating JSPs, you don't have to compile anything, just go to Update Application Files and Save, in the deploytool. Orion will detect and redeploy.