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

Rather than go through the tedium of creating all the files in the last section by hand again, we’ll show you how XDoclet can simplify your life. The idea behind XDoclet is the automatic generation of support files, such as deployment descriptors, based on Javadoc comments within the source code. As we shall see, this is an eminently practical concept. Also, we will start using Ant build scripts to simplify the build process, because this is fast becoming the standard for building Java projects in any capacity, and it works like a charm. The following examples will assume that you have already installed Ant. Please refer to Chapter 4 for more information on installing these tools. There is a multitude of really good Ant build scripts, projects layouts, and other configuration-related material available. This project layout, and the Ant build script associated with it, is a stripped down version of one Stan normally uses for new projects. It has roots in the original XDoclet samples, but has evolved over the course of usage in real-world projects. As always, it is only a starting point, and each project tends to have unique requirements that warrant new tweaks and changes. The Ant build script we’ll be using will only generate the EJB .jar file. Often, the build will generate .war and .ear files. Automatic deployment, JUnit tests, and source control management round out the other favorites. You’ll find an extensive list of extra capabilities in Ant. For more details, check out their Web site at http://ant.apache.org/. Okay, let’s get started with the basic directory structure for this simple project. The layout is as follows, where the $PROJECT_ROOT is the base directory
for this application:

$PROJECT_ROOT\
|-src
|----java
|--------test
|------------ejb
|-gen-src
|-gen-meta
|-build
|-dist
|-lib

Our source files will go into the src directory. The gen-src and gen-meta directories are where the Xdoclet-generated code and descriptors go, respectively. The build directory just holds temporary build classes and other miscellaneous files. The dist directory is where the final EJB .jar file is placed when the build script completes its work. Finally, the lib directory houses the .jar files that are referenced in building the code. For this project, the only file needed is jboss-j2ee.jar, which can be copied from %JBOSS_HOME%\
client\. In the project’s root directory, we have two files: build.xml and build.properties. The build.xml file is a standard Ant build script. If you run Ant from the project root directory, this file will be detected and used to build the project.

 You can use the build.properties file to hold properties that can change from machine to machine. This way configuration changes are limited to the build.properties file, and the build.xml file can be used by all.

Here are the contents of the build.xml file:

<?xml version=”1.0”?>
<project name=”j2ee-app” default=”clean-build” basedir=”.”>
<!-- Grab overrides for any properties. -->
<property file=”build.properties” />
<property file=”${user.home}/build.properties” />
<!-- Default property values. -->
<property name=”project.ejb.file” value=”${project.name}-ejb.jar” />
<property name=”project.dir” value=”.” />
<property name=”project.dist.dir” value=”${project.dir}/dist” />
<property name=”project.lib.dir” value=”${project.dir}/lib” />
<property name=”project.src.dir” value=”${project.dir}/src” />
<property name=”project.src.java.dir”
value=”${project.src.dir}/java” />
<property name=”project.gen-src.dir”
value=”${project.dir}/gen-src” />
<property name=”project.gen-meta.dir”
value=”${project.dir}/gen-meta” />
<property name=”project.build.dir” value=”${project.dir}/build” />
<property name=”project.build.classes.dir”
value=”${project.build.dir}/classes” />
<property name=”xdoclet.lib.dir”
value=”${project.lib.dir}” />
<property name=”project.xdoclet.force” value=”false” />
<!-- These are here to test the use of properties in @tags. -->
<!-- WARNING: Allowing jBOSS to create or destroy
tables may lead to a loss of data. -->
<property name=”jboss.create_table” value=”true” />
<property name=”jboss.remove_table” value=”false” />
<!-- Set up the classpath to all jars, including those in
the project and affliated tools like xdoclet, etc. -->
<path id=”project.class.path”>
<!-- Add all jars in the lib directory. -->
<fileset dir=”${project.lib.dir}”>
<include name=”**/*.jar” />
</fileset>
<!-- Add all jars in the xdoclet lib directory. -->
<fileset dir=”${xdoclet.lib.dir}”>
<include name=”**/*.jar” />
</fileset>
<!-- Append the external classpath last. -->
<pathelement path=”${java.class.path}” />
</path>
Implementing an EJB Container 383
<!-- Init -->
<target name=”init”>
<tstamp>
<format property=”TODAY” pattern=”yyyy-MM-dd”/>
</tstamp>
<taskdef
name=”xdoclet”
classname=”xdoclet.DocletTask”
classpathref=”project.class.path”
/>
<taskdef
name=”ejbdoclet”
classname=”xdoclet.modules.ejb.EjbDocletTask”
classpathref=”project.class.path”
/>
<taskdef
name=”webdoclet”
classname=”xdoclet.modules.web.WebDocletTask”
classpathref=”project.class.path”
/>
<taskdef
name=”replacecopy”
classname=”xdoclet.ant.ReplaceCopy”
classpathref=”project.class.path”
/>
</target>
<target name=”ejbdoclet” depends=”init”>
<ejbdoclet
destdir=”${project.gen-src.dir}”
mergedir=””
excludedtags=”@version,@author,@todo”
addedtags=”@xdoclet-generated at ${TODAY},@copyright
holder,@author author”
ejbspec=”2.0”
force=”${project.xdoclet.force}”
verbose=”false”
>
<!-- Includes EJBs in test.ejb package only -->
<fileset dir=”${project.src.java.dir}”>
<include name=”test/ejb/*Bean.java” />
<exclude name=”test/ejb/Base*.java” />
</fileset>
<packageSubstitution
packages=”ejb” substituteWith=”interfaces”/>
<remoteinterface/>
<localinterface/>
<homeinterface/>
<localhomeinterface/>
<dataobject/>
<valueobject/>
<entitypk/>
<entitycmp/>
<entitybmp/>
<session/>
<utilobject cacheHomes=”true” includeGUID=”true”/>
<deploymentdescriptor
destdir=”${project.gen-meta.dir}”
validatexml=”true”
mergedir=”fake-to-debug”
>
<configParam name=”clientjar” value=”blah.jar”/>
</deploymentdescriptor>
<jboss
version=”3.0”
unauthenticatedPrincipal=”nobody”
xmlencoding=”UTF-8”
destdir=”${project.gen-meta.dir}”
validatexml=”true”
preferredrelationmapping=”relation-table”
createTable=”${jboss.create_table}”