The Ant Build File


Adding a New Entitlements Bean

To show how to use XDoclet with Resin, let's accomplish two tasks. The first is to build a new bean based on a table called logs, and the second is to expand the login bean created in the previous chapter. First let's look at the new table:

 create table logs (   log_id int not null auto_increment primary key,   application varchar(64),   message varchar(128),   ts timestamp); 

If we were going to create an entity bean using what we learned from the previous chapter, we would need to create three files plus the deployment descriptor information. XDoclet and Resin's support allows us to write just the implementation file—the other files will be created automatically. Listing 7.1 contains the implementation file for our new entity bean.

Listing 7.1: Our new entity bean.

start example
 package entitlements;  /**  * @ejb.bean                   type="CMP"  *                             cmp-version="2.x"  *                             name="LogsBean"  *                             jndi.name="cmr.User"  *                             view-type="local"  *                             primkey-field="log_id"  *  * @ejb.pk                       *  * @ejb.home                   generate="local"  *                local-  *  * @ejb.interface              generate="local"  *                local- *  *  * @resin-ejb:entity-bean      sql-table="logs"  */  public abstract class LogsBean extends com.caucho.ejb.AbstractEntityBean {    /** @ejb.pk-field     *  @ejb.interface-method      view-type="local"     *  @ejb.persistent-field     *  @resin-ejb:cmp-field      sql-column="log_id"     */   public abstract String getLog_id();    /** @ejb.persistent-field     *  @ejb.interface-method      view-type="local"     *  @resin-ejb:cmp-field      sql-column="description"     */   public abstract String getDescription();    /** @ejb.interface-method      view-type="local"*/   public abstract void setDescription(String description);    /** @ejb.persistent-field     *  @ejb.interface-method      view-type="local"     *  @resin-ejb:cmp-field      sql-column="message"     */   public abstract String getMessage();    /** @ejb.interface-method      view-type="local"*/   public abstract void setMessage(String message);    /** @ejb.persistent-field     *  @ejb.interface-method      view-type="local"     *  @resin-ejb:cmp-field      sql-column="ts"     */   public abstract String getTs();    /** @ejb.interface-method      view-type="local"*/   public abstract void setTs(String ts); } 
end example

As you can see, there is little code—and a whole bunch of tags. Starting at the top of the file, we find class-level tags. The first tag, called @ejb.bean, tells the system that we are building a new EJB. There are four important parameters to the @ejb.bean tag:

  • type="CMP"— Indicates that the bean should be container managed.

  • name="LogsBean"— Specifies the name of our bean.

  • view-type="local"— Indicates the view: local, remote, or both.

  • primkey-field="log_id"— Specifies the primary key for the table the bean represents.

After the class-level tags are tags that determine the additional classes created for the bean. The tags are:

  • @ejb.pk— Specifies the data type for the primary key of the table the bean represents.

  • @ejb.home— Indicates a home interface will be created and sets the name of the class.

  • @ejb.interface— Indicates the interface will be created with the provided classname.

  • @resin-ejb:entity_bean— Indicates the table associated with this bean. Note this is a Resin-specific tag and is supported only through the Resin XDoclet extension. All of the other tags to this point are supported by XDoclet itself.

Once the class-level tags have been taken care of, we need to define the methods in the implementation file, as well as provide tags so the appropriate methods are built in the interface files. The code starts with a method for accessing the primary key of the table. The tags are:

  /** @ejb.pk-field   *  @ejb.interface-method       view-type="local"   *  @ejb.persistent-field   *  @resin-ejb:cmp-field       sql-column="log_id"   */ public abstract String getLog_id(); 

You will notice that we don't have a setLog_id() method. This is because the table auto-generates the key value. However, if it didn't, the tags would be:

    /** @ejb.persistent-field */ public abstract void setLog_id(int id); 

The tags for the primary key accessors are:

  • @ejb.pk-field— Tells the system this is the primary key.

  • @ejb.interface-method— Tells the system to expose this method to the appropriate interface: local, remote, or both.

  • @ejb.persistent field— Tells the system this field is persistent.

  • @resin-ejb:cmp-field— Maps the field to the appropriate database column.

For all of the other accessor methods, we have a common pattern. The code for the get/set description field is:

  /** @ejb.persistent-field   *  @ejb.interface-method      view-type="local"   *  @resin-ejb:cmp-field      sql-column="description"   */ public abstract String getDescription();  /** @ejb.interface-method      view-type="local"*/ public abstract void setDescription(String description); 

The tags are:

  • @ejb.persistent-field— Tells the system this field is persistent.

  • @ejb.interface-method— Tells the system to expose this method to the appropriate interface: local, remote, or both.

  • @resin-ejb:cmp-field— Maps the field to the appropriate database column.

That's all that you need to add to the bean implementation file. Now it's time to create an Ant build file that will use our implementation file to build all of the other required bean files.




Mastering Resin
Mastering Resin
ISBN: 0471431036
EAN: 2147483647
Year: 2002
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net