Java Tutorial - Java Scipt : CMP 2.x Entity Bean Tests and Sample Code

Java Tutorial - Java Scipt :

CMP 2.x Entity Bean Tests and Sample Code

removetable=”${jboss.remove_table}”
/>
</ejbdoclet>
</target>
<!-- Compile -->
<target name=”compile” depends=”init”>
<javac srcdir=”${project.src.java.dir};${project.gen-src.dir}”
destdir=”${project.build.classes.dir}”
includes=”**/*.java”
debug=”on”>
<classpath refid=”project.class.path” />
</javac>
</target>
<!-- Create EJB .jar file. -->
<target name=”ejb-jar” depends=”init”>
<jar jarfile=”${project.dist.dir}/${project.ejb.file}” >
<!-- Includes EJBs in test.ejb package only -->
<fileset
dir=”${project.build.classes.dir}”
includes=”test/ejb/**,test/interfaces/**”
/>
<metainf
dir=”${project.gen-meta.dir}”
includes=”**.*”
/>
<fileset
dir=”${project.dist.dir}”
includes=”${project.war.file}”
/>
</jar>
</target>
<!-- Clean everything. -->
<target name=”clean” depends=”init”>
<delete dir=”${project.build.dir}” quiet=”true” />
<delete dir=”${project.dist.dir}” quiet=”true” />
<delete dir=”${project.gen-src.dir}” quiet=”true” />
<delete dir=”${project.gen-meta.dir}” quiet=”true” />
<mkdir dir=”${project.build.dir}” />
<mkdir dir=”${project.build.classes.dir}” />
<mkdir dir=”${project.dist.dir}” />
<mkdir dir=”${project.gen-src.dir}” />
<mkdir dir=”${project.gen-meta.dir}” />
</target>
<!--Clean build -->
<target name=”clean-build” depends=”clean, main” />
<!-- Main -->
<target name=”main” depends=”ejbdoclet, compile, ejb-jar” />
</project>

You should be able to read through the build.xml file and get a good understanding of what it does. Some points that bear mentioning are the autogeneration of database tables, which is a JBoss-specific feature, and the ejbdoclet
stuff.

 In the two lines following the WARNING comment, you’ll see that a property telling JBoss to create new database tables for CMP persistence is set. This information is used by XDoclet to generate the deployment descriptors in jboss-ejb.xml that in turn instruct JBoss. Because we’re just testing out CMP on JBoss with the Hypersonic database, this is not an issue. As a general rule, though, setting jboss createTable to true can be a dangerous because it will overwrite or erase data in your database. Use this with caution!

The other thing is that we are instructing XDoclet to only work on certain files. The lines to note are bolded and appear immediately below comments that read: <!— Includes EJBs in test.ejb package only —> These lines work off of an implicit knowledge of the project’s organization and naming scheme. The build.properties file is very straightforward. You’ll need to modify this file to fit your configuration. The only real property to specify is the
home directory where XDoclet is installed. Here are the contents of build.properties:

# --- Project properties ---
project.name = simple_entity
xdoclet.dir=c:/java/xdoclet
xdoclet.lib.dir = ${xdoclet.dir}/lib

And that’s it for the infrastructure! Now, let’s check out the source code. The first thing to note is that it is all contained within a single source file. We no longer have to write each interface file by hand thanks to XDoclet. However, we could start refactoring our design into more classes that would promote the sharing of common code. Through such architecture, we can gain some objectoriented reuse, while staying within the bounds of the EJB specification.Here is the source for SimpleEntity.java:package test.ejb;

import javax.ejb.*;
import java.rmi.RemoteException;
/**
* Just a simple CMP entity bean with a name and a number
*
* @ejb.bean name=”SimpleEntity”
* type=”CMP”
* jndi-name=”ejb/SimpleEntity”
* view-type=”both”
* primkey-field=”name”
*
* @ejb.persistence
* table-name=”simple_entity”
*
* @ejb.pk
* class=”java.lang.String”
* generate=”false”
* unchecked=”true”
*/
public abstract class SimpleEntityBean
implements EntityBean
{
private EntityContext moContext = null;
/** @ejb.create-method */
public java.lang.String ejbCreate( String name, int number )
throws CreateException
{
setName( name );
setNumber( number );
return null;
}
public void ejbPostCreate( String name, int number )
throws CreateException
{}
public void ejbActivate() throws EJBException
{}
public void ejbPassivate() throws EJBException
{}
public void ejbLoad() throws EJBException
{}
public void ejbStore() throws EJBException
{}
public void ejbRemove() throws RemoveException, EJBException
{}
public void setEntityContext( EntityContext oContext )
throws EJBException
{
moContext = oContext;
}
public void unsetEntityContext()
throws EJBException
{
moContext = null;
}
/**
* @ejb.pk-field
* @ejb.interface-method
* @ejb.persistent-field
* @ejb.persistence column-name=”name”
*/
public abstract String getName();
/**
* @ejb.interface-method
*/
public abstract void setName( String name );
/**
* @ejb.interface-method
* @ejb.persistent-field
* @ejb.persistence column-name=”number”
*/
public abstract int getNumber();
/**
* @ejb.interface-method
*/
public abstract void setNumber( int number );
}

Looking through the source code, you’ll find Javadoc lines that contain @ejb.*. These are used to cue XDoclet on how to generate files. For example, the @ejb.interface-method comment tags the method as one that should be exposed via the remote interface. Other tags such as @ejb.persistence column-name=”number” are used to pass on information on the data schema that the CMP works with. There are many more tags, including ones to generate distribution-specific information for such diverse offerings as JBoss, WebLogic, Orion, Resin, Oracle, WebSphere, and many others. Building the project is as simple as running Ant from the project’s root directory, then copying the newly minted .jar file from the dist directory to the JBoss deploy directory.Here are the Windows instructions:

cd \%PROJECT_ROOT%
ant
copy dist\simple-entity-ejb.jar %JBOSS_HOME%\server\default\deploy
And here are the Linux instructions:
cd /$PROJECT_ROOT
ant
cp dist/simple-entity-ejb.jar $JBOSS_HOME/server/default/deploy

The SimpleEntity CMP bean should now be deployed automatically. Because we have specified the appropriate JBoss-specific tags within the source code, JBoss will also create the appropriate table structure in the Hypersonic
database. This means our CMP bean is ready to go. Here’s the code for a quick-and-dirty Swing client program to test out the CMP bean. It will let you add, load, and remove SimpleEntity CMP beans.
Here’s the source for SimpleEntityClient.java:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.naming.*;
import javax.rmi.*;
import javax.swing.*;
import test.interfaces.*;
public class SimpleEntityClient
extends JFrame
implements ActionListener
{
private Context moContext = null;
private JButton moAddButton = null;
private JButton moRemoveButton = null;
private JButton moLoadButton = null;
private JTextField moNameField = null;
private JTextField moNumberField = null;
private JTextArea moMessageArea = null;
public SimpleEntityClient() {
super( “Simple Entity Client” );
setupContext();
createGui();
}
protected void setupContext() {
// --- Create default properties to find JBoss’s JNDI. ---
Hashtable default_properties = new Hashtable();
default_properties.put(
Context.INITIAL_CONTEXT_FACTORY,
“org.jnp.interfaces.NamingContextFactory” );
default_properties.put(
Context.PROVIDER_URL, “localhost:1099” );
// --- Create various JMS resources. ---
// -- Note that we are using the transaction aware versions of
// -- everything.
try {
moContext = new InitialContext( default_properties );
}
catch( NamingException e ) {
e.printStackTrace();
System.exit( 1 );
}
}
protected void createGui() {
setBounds( 50, 50, 500, 500 );