FileDocCategorySizeDatePackage
ProfileBean.javaAPI DocExample11183Thu Dec 15 21:02:24 GMT 2005com.oreilly.jent.ejb.beanManaged

ProfileBean

public 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
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)

      Connection conn = null;
      Statement s = null;
      try {
          conn = getConnection();
          s = conn.createStatement();
          s.executeUpdate("insert into profile (name) values ('" + name + "')");
      }
      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.");
      }
      finally {
          try { s.close(); } catch (Exception e) {}
          try { conn.close(); } catch (Exception e) {}
      }
      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
      Connection conn = null;
      Statement s = null;
      try {
          conn = getConnection();
          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();
      }
      finally {
          try { s.close(); } catch (Exception e) {}
          try { conn.close(); } catch (Exception e) {}
      }
      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();
      Connection conn = null;
      Statement s = null;
      try {
          conn = getConnection();
          s = conn.createStatement();
          // Clear out any profile entries
          s.executeUpdate("delete from profile_entry where name = '" + key + "'");
          // Clear out the profile itself
          s.executeUpdate("delete from profile where name = '" + key + "'");
          
          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();
      }
      finally {
          try { s.close(); } catch (Exception e) {}
          try { conn.close(); } catch (Exception e) {}
      }
  
public voidejbStore()

      String key = (String)mContext.getPrimaryKey();
      Connection conn = null;
      Statement s = null;
      try {
          conn = getConnection();
          s = conn.createStatement();
          // Clear out old profile entries and replace with the current ones
          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 + "')");
          }
      }
      catch (Exception e) {
          System.out.println("Failed to store ProfileBean: ");
          e.printStackTrace();
          throw new EJBException("ejbStore failed: ", e);
      }
      finally {
          try { s.close(); } catch (Exception e) {}
          try { conn.close(); } catch (Exception 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;
      Connection conn = null;
      Statement s = null;
      try {
          conn = getConnection();
          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());
      }
      finally {
          try { s.close(); } catch (Exception e) {}
          try { conn.close(); } catch (Exception e) {}
      }
      
      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.");