Back on Java Track: JBoss3.0TemplateAndExamples

Times are pretty quiet at the moment, and I thought it was just about time to go back to J2EE. Since I was willing to give a look to JBoss that I’ve not used for quite a while, I stumbled upon this article written by Phil DeJarnett on Netbeans.org website. I just followed the recipe and discovered that there is a mock application which can be used to start with JBoss, called JBoss3.0TemplateAndExamples.

I downloaded the whole lot, along with JBoss 3.2.3 and XDoclet 1.2. I was first frustrated to see that it was not quite a “plug-and-play” operation: the Ant file delivered with the templates wouldn’t compile:

XDoclet classpath missing J2EE classes
        at xdoclet.modules.ejb.EjbDocletTask¶
                   .validateOptions(EjbDocletTask.java:85)
        at xjavadoc.ant.XJavadocTask.execute(XJavadocTask.java:94)
        at org.apache.tools.ant.UnknownElement¶
                   .execute(UnknownElement.java:166) etc.

What I didn’t know starting from there is that it would take me quite a while before I could really make it run… Here are the steps I’ve followed to do so.

Ant file

I had therefore to update the Ant file and add the following in the xdoclet-generate target:

      <taskdef
         name="ejbdoclet"
         classname="xdoclet.modules.ejb.EjbDocletTask"
      >
         <classpath>
             <fileset dir="${xdoclet.home}/lib">
                 <include name="**/*.jar"/>
             </fileset>
             <b>&lt;fileset dir="${jboss.client}"&gt;
                 &lt;include name="**/*.jar"/&gt;
             &lt;/fileset&gt;</b>
         &lt;/classpath&gt;
      &lt;/taskdef&gt;

Names and JNDI

After having made those few changes, I managed to compile the test application. But that was certainly not the last step of my long journey towards deployment. The application wouldn’t deploy because of the following error:

The ejb-name for a CMP2.x Entity must be a valid Java Identifier

I then changed the name for my CMP EJB, TestEntity from test/TestEntity to TestEntity, both in TestEntityBean and TestSessionBean which refers to the latter. The reference to the CMP EJB looks like this:

 * @ejb:ejb-ref ejb-name="TestEntity"
 *              ref-name="ejb/TestEntity"

Finders

But the EJB still wouldn’t deploy, JBoss complaining about the fact that the EJB were not spec-compliant.

Several reasons for that. First of all, it looked like the @jboss directives, supposed to generate the finders were not working properly. I moved the @jboss directives into plain EJB-QL queries for the TestEntity bean:

 * ejb:finder signature="java.util.Collection findAll()"
 *            query="SELECT OBJECT(t) FROM TestEntity t"
 *
 * ejb:finder signature="test.interfaces.TestEntity findByName( &para;
 java.lang.String pSurname, java.lang.String pLastName )"
 *            query="SELECT OBJECT(t) FROM TestEntity t&para;
 WHERE t.firstName = ?1 AND t.lastName = ?2"
 *
 * @ejb:finder signature="test.interfaces.TestEntity findAnotherByName( &para;
 int pId, java.lang.String pSurname, java.lang.String pLastName )"
 *            query="SELECT OBJECT(t) FROM TestEntity t WHERE t.id &lt;&gt; ?1&para;
 AND t.firstName = ?2 AND t.lastName = '?3'"
 *
 

I also removed in the BMP Bean the @ejb:finder directive and wrote the finder in the bean:

    public java.util.Collection ejbFindAll(  )
      throws FinderException
   {
      DataSource lDataSource = getDataSource();
      Connection lConnection = null;
      PreparedStatement lStatement = null;
      java.util.Collection result = new java.util.LinkedList();
      try {
         lConnection = lDataSource.getConnection();
         lStatement = lConnection.prepareStatement(
            “SELET Id FROM TestEntity”
         );
         ResultSet lResult = lStatement.executeQuery();
         while( lResult.next() ) {
             result.add(new TestBMPEntityPK(lResult.getInt(1)));
         }

         return result;
      }
      catch ( SQLException se ) {
         throw new FinderException( “Could not find record from DB: “
          + se.getMessage() );
      }
      finally {
         if( lStatement != null ) {
            try {
               lStatement.close();
            }
            catch( Exception e ) {}
         }
         if( lConnection != null ) {
            try {
               lConnection.close();
            }
            catch( Exception e ) {}
         }
      }
   }
   

Local and LocalHome

The last error to occur was that the entity bean was produced without its Local and LocalHome classes. The solution for that was to add the associated XDoclet tasks, &lt;homeinterface&gt; and &lt;localhomeinterface&gt;.

And it worked <sigh>:

New Entity Id is: 2

Take your Pick

If you happen to want to do the same, the zip containing the template for JBoss can be found here. As I may have forgotten to mention changes I’ve made, here are the ones I’ve modified:

I’m aware of the fact that I’ve decided to take easy shortcuts to reach my goal but you’ll have to keep in mind that my original plan was to have a plain look at JBoss… I hope this will help and if you manage to do it in a much cleaner way, I’d be quite happy to hear about it.

References

 
---

Comments

  1. Thanks for the link, I was actually looking for something like this to have a look at JBoss !!

    pierre · 2004-03-30 09:24 · #

  2. Thanks for your work, it was very helpfull. I found your weblog through Google.

    Nicolas · 2004-04-07 09:25 · #

  3. Mother o’ Jaysus, now that’s a feat! Wouldn’t be able to do that myself – finding my own site with Google, that is.

    s · 2004-04-09 16:52 · #

Commenting is closed for this article.

---