HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Coming Out of the JDO Closet"
Vol. 7, Issue 2, p. 30

	


Listing 1: LifeForm entities - naive implementation


// LifeForm.java
public abstract class LifeForm {
  // private members
  private Age _age;
  private Name _name;


  // methods
  public Name getName() {
    return _name;
  }
  public void setName(Name name) {
    _name = name;
  }
  public Age getAge() {
    return _age;
  }
  public void setAge(Age age) {
    _age = age;
  }
}


// Man.java
public class Man extends LifeForm {
}


// Flea.java
public class Flea extends LifeForm{
}


// Dog.java
public class Dog extends LifeForm {
  // private members
  private Collection _fleas = new Vector();
  // Conveniently enough, we initialize the Fleas collection in the Dog
  // constructor, thus a Dog with no Fleas (assuming one exists?)                                     will hold
  // an empty Collection


  private Man _bestFriend;


  // methods
  public Collection getFleas() {
    return _fleas;
  }
  public Man getBestFriend() {
    return _bestFriend;
  }
  public void setBestFriend(Man bestFriend) {
    _bestFriend = bestFriend;
  }
}


// Name.java
public class Name {
  // private members
  private String _name;


  // constructor
  public Name(String name) {
    if (!validate(name)) {
      throw new IllegalArgumentException(
        "Invalid value: name must contain only letters");
    }
   _name = name;
  }


  // methods
  public String stringValue() {
    return _name;
  }
  private boolean validate(String name) {
    for (int i = 0 ; i < name.length() ; i++) {
      if (!Character.isLetter(name.charAt(i)))
        return false;
    }
    return true;
  }
}


// Age.java
public class Age {
  // private members
  private Integer _age;


  // constructor
  public Age(Integer age) {
    if (age.intValue() < 0) {
      throw new IllegalArgumentException(
        "Invalid value: age must be a positive integer");
    }
    _age = age;
  }


  // methods
  public int intValue() {
    return _age.intValue();
  }
}



Listing 2: XML file used by JDO enhancer

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jdo SYSTEM "jdo.dtd">
<jdo>
  <package name="lifeforms">
    <class name="Name"/>
    <class name="Age"/>
    <class name="LifeForm">
      <field name="_name" embedded="true"/>
      <field name="_age" embedded="true"/>
    </class>
    <class name="Man" persistence-capable-superclass="LifeForm"/>
    <class name="Flea" persistence-capable-superclass="LifeForm"/>
    <class name="Dog"  persistence-capable-superclass="LifeForm">
      <field name="_fleas">
        <collection element-type="Flea" embedded-element="true"/>
      </field>
    </class>
  </package>
</jdo>



Listing 3: Session Bean working on life forms

public class DogServices implements SessionBean {
  // A CMT stateless session bean


  public void createDog(Age age, Name name,
                        Collection fleas, Man bestFriend) {
    PersistenceMnager pm = JDOFactory.getPersistenceManager();
    Dog dog = new Dog();
    dog.setAge(age);
    dog.setName(name);
    dog.getFleas().addAll(fleas);
    dog.setBestFriend(bestFriend);
    pm.makePersistent(dog);
  }


  public void addFlea(Object dogId, Flea flea) {
    PersistenceMnager pm = JDOFactory.getPersistenceManager();
    Dog dog = pm.getObjectById(dogId);
    dog.getFleas().add(flea);
  }


  public void deleteDog(Dog dog) {
    PersistenceMnager pm = JDOFactory.getPersistenceManager();
    pm.deletePersistent(dog);
  }


  // ...
}


Listing 4: LifeForm interfaces


// LifeForm.java
public interface LifeForm {
  public Age getAge();
  public void setAge(Age age);


  public Name getName();
  public void setName(Name name);
}


// Age.java
public interface Age {
  Integer integerValue();
}


// Name.java
public interface Name {
  String stringValue();
}


// Flea.java
public interface Flea extends LifeForm{
}


// Man.java
public interface Man extends LifeForm {
}


// Dog.java
public interface Dog extends LifeForm {
  public Collection getFleas();
  public Man getBestFriend();
  public void setBestFriend(Man man);
}



Listing 5: Sample code, using Delta

// Client side code
public void someFunc(Dog dog) {
  ...
  dog.setAge(new AgeImpl(7));
  dog.getFleas().remove(dog.getFleas().iterator().next());
  DogServices services = ServiceFactory.getDogServices();
  // send dog's delta to server
  services.updateDog(dog.buildDelta());
  ...
}


// Server side code
public void updateDog(DogDelta dd) {
  ...
  PersistenceMnager pm = JDOFactory.getPersistenceManager();
  Object oid = dd.getObjectId();
  Dog dog = pm.getObjectById(oid);
  if (dd.getAge() != null)
Dog.applyDelta(dd);
  else ...
  ...
}

  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.