FileDocCategorySizeDatePackage
ProfileBean.javaAPI DocExample8687Sun May 14 16:20:12 BST 2006com.oreilly.jent.ejb.containerManaged

ProfileBean

public abstract class ProfileBean extends Object implements javax.ejb.EntityBean
In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product's documentation does require permission. We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: "Java Enterprise in a Nutshell, Third Edition, by Jim Farley and William Crawford with Prakash Malani, John G. Norman, and Justin Gehtland. Copyright 2006 O'Reilly Media, Inc., 0-596-10142-2." If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com.

Fields Summary
private transient Properties
mEntries
private javax.ejb.EntityContext
mContext
Constructors Summary
Methods Summary
private voidclearEntries()

    // Initialize our profile properties to an empty set, and 
    // transfer the empty collection to the byte array
    this.mEntries = new Properties();
    try { 
      transferToBytes();
    } 
    catch (IOException ioe) {
      System.out.println("Failed to initialize binary entries: "
                         + ioe.getMessage());
    }
  
public voidejbActivate()

    try {
      transferToProps();
      System.out.println("ProfileBean activated.");
    }
    catch (IOException ioe) {
      System.out.println("Failed to convert entries during activation.");
      ioe.printStackTrace();
    }
  
public java.lang.StringejbCreate()

    System.out.println("Nameless ProfileBean created.");
    setName(" ");
    clearEntries();
    return null;
  
public java.lang.StringejbCreate(java.lang.String name)

    setName(name);
    clearEntries();
    System.out.println("ProfileBean created for " + name + ".");
    return null;
  
public voidejbLoad()

    try {
      transferToProps();
    }
    catch (IOException e) {
      System.out.println("Failed to load ProfileBean: ");
      e.printStackTrace();
      throw new EJBException("ejbLoad failed: ", e);
    }
    System.out.println("ProfileBean load finished.");
  
public voidejbPassivate()

    try {
      transferToBytes();
      System.out.println("ProfileBean being passivated.");
    }
    catch (IOException ioe) {
      System.out.println("Failed to convert entries during activation.");
      ioe.printStackTrace();
    }
  
public voidejbPostCreate()

    System.out.println("ProfileBean post-create called.");
  
public voidejbPostCreate(java.lang.String name)

    System.out.println("ProfileBean post-create called.");
  
public voidejbRemove()

    System.out.println("ProfileBean removed.");
  
public voidejbStore()

    try {
      transferToBytes();
    }
    catch (IOException e) {
      System.out.println("Failed to store ProfileBean: ");
      e.printStackTrace();
      throw new EJBException("ejbStore failed: ", e);
    }
    System.out.println("ProfileBean store finished.");
  
public abstract byte[]getEntriesBytes()

public java.lang.StringgetEntry(java.lang.String key)

    System.out.println("getEntry(): principle = " +
                       mContext.getCallerPrincipal().getName());
    return mEntries.getProperty(key);
  
public abstract java.lang.StringgetName()

public abstract PersonLocalgetPersonLocal()

public voidsetEntityContext(javax.ejb.EntityContext context)

    System.out.println("ProfileBean context set.");
    mContext = context;
  
public abstract voidsetEntriesBytes(byte[] entries)

public voidsetEntry(java.lang.String key, java.lang.String value)

    mEntries.put(key, value);
  
public abstract voidsetName(java.lang.String name)

public abstract voidsetPersonLocal(PersonLocal person)

private voidtransferToBytes()


  //--------------------------------------------------
  // Utility methods
  //--------------------------------------------------

  // Transfer the list of entries from our Properties member to the byte
  // array.
       
    // Serialize the Properties into a byte array using an ObjectOutputStream
    if (mEntries != null) {
      ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
      ObjectOutputStream objOut = new ObjectOutputStream(byteOut);
      objOut.writeObject(mEntries);
      setEntriesBytes(byteOut.toByteArray());
    }
  
private voidtransferToProps()

    // Take the raw byte array and de-serialize it
    // back into a Properties object using an ObjectInputStream
    try {
      if (getEntriesBytes() != null) {
        ByteArrayInputStream byteIn =
          new ByteArrayInputStream(getEntriesBytes());
        ObjectInputStream objIn = new ObjectInputStream(byteIn);
        mEntries = (Properties)objIn.readObject();
      }
      // If no entries in database, set properties to a new, empty collection
      else {
        mEntries = new Properties();
      }
    }
    catch (ClassNotFoundException cnfe) {
      System.out.println("Properties class not found during de-serialization");
    }
  
public voidunsetEntityContext()

    System.out.println("ProfileBean context unset.");
    mContext = null;