FileDocCategorySizeDatePackage
ProfileBean.javaAPI DocExample2530Wed Apr 05 11:25:42 BST 2000stateful

ProfileBean.java

/*
 * This example is from the book "Java Enterprise in a Nutshell".
 * Copyright (c) 1999 by O'Reilly & Associates.  
 * You may distribute this source code for non-commercial purposes only.
 * You may study, modify, and use this example for any purpose, as long as
 * this notice is retained.  Note that this example is provided "as is",
 * WITHOUT WARRANTY of any kind either expressed or implied.
 */

package stateful;

import javax.ejb.*;
import java.rmi.RemoteException;
import NoSuchPersonException;
import java.util.Properties;

/**
 * A ProfileBean, which provides enterprise
 * profile information for a named person.
 */
public class ProfileBean implements SessionBean {
  //
  // State for this (stateful) bean
  //

  // Name of the person owning the profile
  private String mName = "";
  // Entries in the profile (name/value pairs)
  private Properties mEntries = new Properties();
  
  // Store session context
  private SessionContext mContext = null;
  
  // Session bean methods

  /**
   * No need for us to activate anything in this bean, but we need to
   * provide an implementation.
   */
  public void ejbActivate() {
    System.out.println("ProfileBean activated.");
  }

  /**
   * Nothing to do on a remove.
   */
  public void ejbRemove() {
    System.out.println("ProfileBean removed.");
  }

  /**
   * No resources to release on passivation...
   */
  public void ejbPassivate() {
    System.out.println("ProfileBean passivated.");
  }

  /**
   * Get context from container.
   */
  public void setSessionContext(SessionContext context) {
    System.out.println("ProfileBean context set.");
    mContext = context;
  }

  /**
   * Create method (corresponds to each create() method on the
   * home interface, ProfileHome).  Nothing to initialize in this case.
   */
  public void ejbCreate() {
    System.out.println("Nameless ProfileBean created.");
  }

  /**
   * Create method with name of profile owner.
   */
  public void ejbCreate(String name) {
    mName = name;
    System.out.println("ProfileBean created for " + mName + ".");
  }

  // Finally, the remote methods.  Signatures of methods must match those
  // on remote interface (Profile), except that throwing
  // RemoteException is not necessary.

  public String getName() {
    return mName;
  }

  public void setName(String name) {
    mName = name;
  }

  public String getEntry(String key) {
    return mEntries.getProperty(key);
  }

  public void setEntry(String key, String value) {
    mEntries.put(key, value);
  }
}