| O r i o n C M P P r i m e r |
| $Revision: 1.19 $ |
This article will show you how to write and run a simple Container-Managed Persistent Entity Bean using the Orion application server (www.orionserver.com).
| Note: | |
|
| Introduction |
The Orion Primer showed you how to write, compile and deploy a servlet and a session bean. This article will show you how to write a Container Managed Entity Bean as well as an HTML page and a JSP that you can use to access the entity bean.
We will first draw a sketch of what we are going to build. Then we'll set
up a directory structure for this, and write the necessary HTML, JSP and Java
files. After that we will build the JAR, WAR and EAR files and deploy them
into Orion.
If you have any trouble, see if your question is answered by any of the
resources at the bottom of this article. If it's not, try
posting your question on the orion-interest mailing list (see the Orion website), or on
the IRC channel #java on
EFnet.
Before you start, make sure you have downloaded and installed the following software:
This article has the following sections:
|
| Case description |
For this example, we will create a small address book application. Users will be able to list, add, edit and delete address entries. No security will be involved at this time, so all users will be able to perform these operations.
Here's a sketch of what the structure of our J2EE application will be:

The web application will contain the files needed to implement the front end for our address book application. We will create a HTML page to represent the front page, and one JSP per action.
| Step 1: Setup directory structure |
First thing we do, is set up a directory structure that will contain all
of the files we write and those we generate. You should create the
addressbook directory somewhere in your home directory, for
example as /home/john.doe/projects/addressbook/.
During this exercise we will put all source files in the following directory structure:
addressbook addressbook/etc addressbook/src addressbook/src/java addressbook/src/java/addressbook addressbook/src/java/addressbook/ejb addressbook/src/jsp addressbook/src/web
All .java source files will be placed under
addressbook/src/java. The JSP files will be placed under
src/jsp and any .html, .gif,
.jpg and .css files will be put in
addressbook/src/web.
The directory addressbook/etc will contain all the
necessary .xml configuration files.
The generated files will end up in the lib and
build directories. We haven't created these directories yet, but
our build file will do this for us.
When archives are created, they will be placed under
addressbook/build/.
|
| Step 2: Write the remote interface |
Now for the back end we will write an interface for your bean, just as we
did in the Orion Primer. Again the new
interface will be derived from
javax.ejb.EJBObject:
public interface AddressEntry extends EJBObject {
public String getName() throws RemoteException
public String getAddress() throws RemoteException
public String getCity() throws RemoteException
public void setAddress(String newAddress) throws RemoteException
public void setCity(String newCity) throws RemoteException
}
|
| Step 3: Write the bean class |
Again, we have the interface that the clients can use to modify an existing bean. So far there are little differences visible between the session bean developed in the Orion Primer trail, and the entity bean we are developing now.
Now let's look at the entity bean. This is a class that must implement the
interface
javax.ejb.EntityBean
public class AddressEntryBean extends Object implements EntityBean
This class will need to contain the following things:
AddressEntry interface.
EntityBean interface.
ejbCreate and ejbPostCreate.
The EntityBean
interface defines the following methods:
public void ejbActivate()
public void ejbPassivate()
public void ejbRemove()
public void setEntityContext(EntityContext)
public void unsetEntityContext()
public void ejbLoad()
public void ejbStore()
The EJB specification mandates that we add an ejbCreate method
too, and a corresponding ejbPostCreate:
public String ejbCreate(String name,
String address,
String city)
public void ejbPostCreate(String name,
String address,
String city)
|
Note that we have some additional functionality in our bean. We have a
class field instanceCount and an instance field
tracer. Both are used for tracing calls to our bean's methods.
After deploying our bean you will be able to see what methods are called. The
console will show something like:
09:55:04 AddressEntryBean[1]: <init>()
09:55:04 AddressEntryBean[1]: setEntityContext("com.evermind.server.ejb.EvermindEntityContext@5b18006768")
09:55:04 AddressEntryBean[1]: ejbActivate()
09:55:04 AddressEntryBean[1]: ejbLoad()
09:55:04 AddressEntryBean[1]: getName() -> "Ernst de Haan"
etc.
For this, the bean uses a class called TraceHelper. You will
need this class too.
|
| Step 4: Write the home interface |
A home interface is used to create and destroy instances of
Enterprise JavaBeans. So we need a home interface for our
AddressEntry bean as well.
The home interface will be derived from
javax.ejb.EJBHome.
We will call it AddressBook:
public interface AddressBook extends EJBHome {
public AddressEntry create(String name,
String address,
String city);
public AddressEntry findByPrimaryKey(String name);
public Collection findAll();
}
We need to use this interface from our JSP pages to get a reference to an
instance of AddressEntry. The home interface we produced will
give us three ways of accessing an AddressEntry instance. We can
create a new instance using the create() method. We can use the
findByPrimaryKey() method to get an entry by primary key, or we
can get the set of all address entries by calling findAll.
|
This is all we need to do for our home interface. Orion will create an implementation for this interface that will implement the behaviour we need. Life doesn't have to be complicated !
| Step 5: Write the EJB deployment descriptor |
We need an XML deployment descriptor that will describe the EJB part of our application.
In the deployment descriptor we describe every Enterprise JavaBean we would like to deploy. In our case, there is only one. This time it's an entity bean:
<entity> <display-name>Address book entry</display-name> <ejb-name>addressbook.ejb.AddressEntry</ejb-name> <home>addressbook.ejb.AddressBook</home> <remote>addressbook.ejb.AddressEntry</remote> <ejb-class>addressbook.ejb.AddressEntryBean</ejb-class> <persistence-type>Container</persistence-type> <prim-key-class>java.lang.String</prim-key-class> <reentrant>False</reentrant> <cmp-field><field-name>name</field-name></cmp-field> <cmp-field><field-name>address</field-name></cmp-field> <cmp-field><field-name>city</field-name></cmp-field> <primkey-field>name</primkey-field> </entity>
This deployment descriptor specifies a few characteristics for the entity bean, including the fields the EJB container (Orion in this case) should persist and what the primary key is.
|
| Step 6: Create the front end |
It is now time to create the files that implement the front end of our application. This is the web module.
We will create a single HTML page as the front page, and 4 JSP files, one per action:
We can visualize this as follows:

The idea of this article is not to teach you how to write JSP's, it helps you get your feet wet. See the links at the bottom of this article for links to more information that will help you improve your skills.
|
This is all there is to the front end. Take a good look at the code and make sure you get the general idea. The code contains quite some documentation to help you understand what goes on.
| Step 7: Write the Web deployment descriptor |
The descriptor for our J2EE web module is very simple. With optional things removed it looks like this:
<web-app>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<ejb-ref>
<ejb-ref-name>ejb/AddressBook</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<home>addressbook.ejb.AddressBook</home>
<remote>addressbook.ejb.AddressEntry</remote>
</ejb-ref>
</web-app>
The <welcome-file-list> tag specifies that if no file is
specified in the URL, then index.html will be served to the web
client.
The <ejb-ref> tag specifies that this web module needs
the address book entity bean at the JNDI location
java:comp/env/ejb/AddressBook. Orion will make sure that our web
application will find the home interface there.
|
| Step 8: Write the J2EE application definition |
Now we need to combine the EJB module and the web module into a J2EE application. For this we need to write a J2EE application deployment descriptor. This is an XML file that defines the modules of a Java 2 Enterprise Edition application, and how they should be deployed. In our case we can keep the file very simple:
<application>
<module>
<ejb>addressbook-ejb.jar</ejb>
</module>
<module>
<web>
<web-uri>addressbook-web.war</web-uri>
<context-root>addressbook-web</context-root>
</web>
</module>
<application>
The root tag is <application>, and inside the
application we define the two <module>s.
|
| Step 9: Compile and create the .jar, .war and .ear files |
An .ear file is a special form of a ZIP file. It contains a
complete deployable J2EE application. We will create a file
addressbook.ear to contain
the following files and directories:
META-INF/ META-INF/application.xml addressbook-ejb.jar addressbook-web.war
The addressbook-ejb.jar file is another special ZIP file, that
contains the EJB part of the application. We will put the following files and
directories in it:
META-INF/ META-INF/ejb-jar.xml addressbook/ addressbook/ejb/AddressEntry.class addressbook/ejb/AddressBook.class addressbook/ejb/AddressEntryBean.class
The file addressbook-web.war is our third special ZIP file. It
contains the Web part of the application. This is what we will put in this
file:
WEB-INF/ WEB-INF/web.xml index.html list.jsp add.jsp edit.jsp delete.jsp
For this purpose, I have written an Ant build file that will compile all
the .java files and create the .jar,
.war and .ear files for us.
|
In this file you may want to change one or two settings:
orion.dir must point the directory where you
installed Orion, in my case: /usr/local/orion. If you
installed Orion in D:\Orion, then you would set this to
D:\Orion (not something like D:\\Orion or
D:/Orion).
build.compiler can be
changed to jikes to improve
performance, if you installed Jikes,
a Java compiler by IBM.
We are now ready to generate the class files, the web module archive
(.war), the EJB module archive (.jar) and the J2EE
application archive (.ear).
|
This will (should) create the following files. Click on one to download the files generated on my system:
lib/ejb/addressbook/ejb/AddressEntry.class lib/ejb/addressbook/ejb/AddressEntryBean.class lib/ejb/addressbook/ejb/AddressBook.class build/addressbook-ejb.jar build/addressbook-web.war build/addressbook.ear
| Step 10: Install the application |
To actually install our application, you need to add a line like the
following to your server.xml in the Orion configuration directory
(orion/config). Make sure you use an absolute path!
<application name="addressbook"
path="path to ear file" />
In my case:
<application name="addressbook"
path="/home/ernst/projects/addressbook/build/addressbook.ear" />
Please make sure that the process that will run Orion has write
access to the directory that contains the .ear file.
| Step 11: Bind the web application |
Now we need to bind the web application to the
default site. This is done by adding the following line to the file
default-web-site.xml in the
Orion configuration directory:
<web-app application="addressbook"
name="addressbook-web"
root="/addressbook" />
| Step 12: Start Orion |
Everything should work, so let's start Orion.
|
You should see some log messages from Orion now. The exact messages depend on your version of Orion and configuration. On my system I got this:
Auto-unpacking /usr/home/ernst/addressbook/addressbook/build/addressbook.ear... done.
Auto-deploying addressbook...
Auto-deploying addressbook-ejb.jar (No previous deployment found)...
Auto-creating table: create table addressbook_ejb_AddressEntry (name char (255) not null primary key, address char (255), city char (255))
done.
Orion/1.2.7 initialized
Now open your favourite webbrowser, and point it to:
http://hostname:port/addressbook/
In my case I am running the Orion webserver on a different computer, that computer has IP address 10.0.0.1, and it runs Orion on port 9000:
http://10.0.0.1:9000/addressbook/
As soon as you access the web module, you should see a message appear in the screen you started Orion in, something like:
Auto-deploying Orion CMP Primer Web Application...
You should see the index.html file in your browser. It will
look similar to this:

You can test the JSP pages by clicking on the link. You should see a screen indicating that there are no entries found, similar to the following:

Add entries using the Add entry link. After submitting a new address you will return to the Add address screen until you click the Back to the list link. After adding a few addresses the list page will look similar to this:

When you change anything in one of the deployment descriptors, JSP pages or
Java source files, rerun ant to generate a new
.ear file. Orion will detect the file has been changed and will
redeploy the application.
| See also |
See the following pages for more information on Orion:
See the following sites for more information on Enterprise JavaBeans (EJB) and Java 2 Enterprise Edition (J2EE):
| To do |
|
| $Revision: 1.19 $ $Date: 2001/01/30 23:38:55 $ |