Sun GlassFish Mobility Platform 1.1 Developer's Guide for Client Applications

Extending the BusinessObject Class

To create a client application using the MCBO API, you need to create your own class that extends the com.sun.mep.client.api.BusinessObject class.

Typically, you begin by importing the packages the class needs. In the Secure MusicDB sample code, the Album class, defined in the file Album.java, begins by importing the following packages:

import com.sun.mep.client.api.BusinessObject;
import com.synchronica.commons.date.DateStringParser;
import com.synchronica.commons.date.Iso8601Converter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

You must implement bean properties for your data model. The only required getter and setter methods are setName and getName, which specify and retrieve the name of the object, and getExtension, which returns the file extension for your object.

In addition, you must implement the serialize and deserialize methods.

The Album class extends the BusinessObject class:

public class Album extends BusinessObject {

The code first declares a string constant and the bean properties:

private static final String DEFAULT_VALUE = "$$default$$";

String albumName;
/**
 * Album's artist.
 */
String artist;
/**
 * Date in which the album was published.
 */
Date datePublished;
/**
 * Album's rating from 1 to 5.
 */
int rating;

The Album class has two constructor methods, a no-argument version and one that takes the name of the album as an argument:

public Album() {
    super();
}

public Album(String name) {
    super(name);
}

The Album class does not implement its own versions of getName and setName, instead inheriting the versions in BusinessObject. It implements getExtension by specifying the suffix .alb as the file extension for Album objects:

public String getExtension() {
    return ".alb";
}

In addition, the class implements getter and setter methods for the String property artist, the java.util.Date property datePublished, and the int property rating:

public String getArtist() {
    return artist;
}
public Date getDatePublished() {
    return datePublished;
}
public int getRating() {
    return rating;
}
public void setArtist(String artist) {
    this.artist = artist;
}
public void setDatePublished(Date datePublished) {
    this.datePublished = datePublished;
}
public void setRating(int rating) {
    this.rating = rating;
}

The Album class implements the serialize method by creating a java.io.DataOutputStream from a java.io.ByteArrayOutputStream, writing the album data to the java.io.DataOutputStream, then returning the java.io.ByteArrayOutputStream converted to a ByteArray.

public byte[] serialize() throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DataOutputStream dOut = new DataOutputStream(out);

    dOut.writeUTF(getName());
    dOut.writeUTF(artist != null ? artist : DEFAULT_VALUE);
    dOut.writeUTF(
        datePublished != null ? getSimplifiedDate(datePublished) : DEFAULT_VALUE);
    dOut.writeUTF(Integer.toString(rating));
    dOut.flush();
    
    System.err.println("Serializing album:");
    System.err.println("    Name: "  + getName());
    System.err.println("  Artist: "  + artist != null ? artist : DEFAULT_VALUE);
    System.err.println("    Date: "  + 
        datePublished != null ? getSimplifiedDate(datePublished) : DEFAULT_VALUE);
    System.err.println("  Rating: "  + Integer.toString(rating));
    
    return out.toByteArray();
}

The class implements the deserialize method by creating a java.io.DataInputStream from a java.io.ByteArrayInputStream passed as an argument, then reading the album data from the java.io.DataInputStream. It uses some utility methods from the com.synchronica API to handle date information.

public void deserialize(byte[] array) throws IOException {
    ByteArrayInputStream in = new ByteArrayInputStream(array);
    DataInputStream dIn = new DataInputStream(in);

    albumName = dIn.readUTF();
    artist = dIn.readUTF();
    if (artist.equals(DEFAULT_VALUE)) {
        artist = null;
    }

    DateStringParser dateParser = new Iso8601Converter();
    String date = dIn.readUTF();
    Calendar c = dateParser.stringToCalendar(date, TimeZone.getDefault());
    datePublished = date.equals(DEFAULT_VALUE) ? null : c.getTime();
    
    rating = Integer.parseInt(dIn.readUTF());
}

The Album class also contains a utility method, getSimplifiedDate, that converts the java.util.Date value to a String.