FileDocCategorySizeDatePackage
ProfileBean.javaAPI DocExample9147Mon Sep 12 23:14:00 BST 2005com.oreilly.jent.ejb.beanManaged

ProfileBean

public class ProfileBean extends Object implements javax.ejb.EntityBean
ProfileBean: A bean-managed entity implementation of the Profile EJB. Example 8-12, Java Enterprise in a Nutshell, 2nd ed. Author: Jim Farley

Fields Summary
public Properties
mEntries
public javax.ejb.EntityContext
mContext
Constructors Summary
Methods Summary
public voidejbActivate()


  //--------------------------------------------------
  // Entity bean methods
  //--------------------------------------------------

  // EJB activation method.  During activation, create our entry lookup table.
     
    mEntries = new Properties();
    System.out.println("ProfileBean activated.");
  
public java.lang.StringejbCreate(java.lang.String name)

    try {
      Connection conn = getConnection();
      Statement s = conn.createStatement();
      s.executeUpdate("insert into profile (name) values ('" + name + "')");
      s.close();
      conn.close();
    }
    catch (SQLException se) {
      System.out.println("Error creating profile, assuming duplicate.");
      try {
        StringWriter strw = new StringWriter();
        PrintWriter prntw = new PrintWriter(strw);
        se.printStackTrace(prntw);
        throw new DuplicateProfileException("SQL error creating profile for " +
                                            name + ": " + se.toString() +
                                            "\n" + strw.toString());
      }
      catch (Exception e) {}
    }
    catch (NamingException ne) {
      System.out.println("Error accessing DataSource");
      throw new CreateException("Error accessing DataSource.");
    }
    System.out.println("ProfileBean created for " + name + ".");
    return name;
  
public java.util.CollectionejbFindByEntryValue(java.lang.String key, java.lang.String value)

    LinkedList userList = new LinkedList();
    // Get a new connection from the EJB server
    try {
      Connection conn = getConnection();
      Statement s = conn.createStatement();
      // Issue a query for matching profile entries, grabbing just the name
      s.executeQuery("select distinct(name) from profile_entry where " +
                     " key = '" + key + "' and value = '" + value + "'");
      // Convert the results in primary keys and return an enumeration
      ResultSet results = s.getResultSet();
      while (results.next()) {
        String name = results.getString(1);
        userList.add(name);
      }
    }
    catch (SQLException se) {
      // Failed to do database lookup
      throw new FinderException();
    }
    catch (NamingException ne) {
      // Failed to access DataSource
      throw new FinderException();
    }
    return userList;
  
public java.lang.StringejbFindByPrimaryKey(java.lang.String key)

    loadFromDB(key);
    return key;
  
public voidejbLoad()

    try {
      String key = (String)mContext.getPrimaryKey();
      loadFromDB(key);
    }
    catch (Exception e) {
      System.out.println("Failed to load ProfileBean: ");
      e.printStackTrace();
      throw new EJBException("ejbLoad failed: ", e);
    }
    System.out.println("ProfileBean load finished.");
  
public voidejbPassivate()

    mEntries = null;
    System.out.println("ProfileBean passivated.");
  
public voidejbPostCreate(java.lang.String name)

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

    // Get this profile's name
    String key = (String)mContext.getPrimaryKey();
    try {
      Connection conn = getConnection();
      // Clear out any profile entries
      Statement s = conn.createStatement();
      s.executeUpdate("delete from profile_entry where name = '" + key + "'");
      // Clear out the profile itself
      s.executeUpdate("delete from profile where name = '" + key + "'");

      s.close();
      conn.close();
      System.out.println("ProfileBean removed.");
    }
    catch (SQLException se) {
      System.out.println("Error removing profile for " + key);
      se.printStackTrace();
    }
    catch (NamingException ne) {
      System.out.println("Error accessing DataSource");
      ne.printStackTrace();
    }
  
public voidejbStore()

    String key = (String)mContext.getPrimaryKey();
    try {
      Connection conn = getConnection();
      // Clear out old profile entries and replace with the current ones
      Statement s = conn.createStatement();
      s.executeUpdate("delete from profile_entry where name = '" + key + "'");
      Enumeration pKeys = mEntries.propertyNames();
      while (pKeys.hasMoreElements()) {
        String pKey = (String)pKeys.nextElement();
        String pValue = mEntries.getProperty(pKey);
        s.executeUpdate("insert into profile_entry (name, key, value) values "
                        + "('" + key + "', '" + pKey + "', '" + pValue + "')");
      }
      // Close the statement and the connection, just to be tidy...
      s.close();
      conn.close();
    }
    catch (Exception e) {
      System.out.println("Failed to store ProfileBean: ");
      e.printStackTrace();
      throw new EJBException("ejbStore failed: ", e);
    }
    System.out.println("ProfileBean store finished.");
  
private java.sql.ConnectiongetConnection()

    Context ctx = new InitialContext();
    DataSource profileDB =
      (DataSource)ctx.lookup("java:comp/env/jdbc/profileDB");
    return profileDB.getConnection();
  
public java.lang.StringgetEntry(java.lang.String key)

    return mEntries.getProperty(key);
  
public java.lang.StringgetName()

    return (String)mContext.getPrimaryKey();
  
protected voidloadFromDB(java.lang.String key)

    boolean found = false;
    try {
      Connection conn = getConnection();
      Statement s = conn.createStatement();
      s.executeQuery("select name from profile where name = '" + key + "'");
      ResultSet rs = s.getResultSet();
      if (rs.next()) {
        found = true;
        s.executeQuery("select key, value from profile_entry where name = '" +
                       key + "'");
        rs = s.getResultSet();
        while (rs.next()) {
          String pKey = rs.getString(1);
          String pValue = rs.getString(2);
          mEntries.put(pKey, pValue);
        }
      }
    }
    catch (SQLException e) {
      throw new FinderException("Failed to load profile entries from DB: " +
                                e.toString());
    }
    catch (NamingException ne) {
      throw new FinderException("Failed to access DataSource from server: " +
                                ne.toString());
    }
    if (!found) {
      throw new FinderException("No profile found for " + key);
    }
  
public voidsetEntityContext(javax.ejb.EntityContext context)

    mContext = context;
    System.out.println("ProfileBean context set.");
  
public voidsetEntry(java.lang.String key, java.lang.String value)

    if (mEntries == null) {
      mEntries = new Properties();
    }
    mEntries.put(key, value);
  
public voidunsetEntityContext()

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