FileDocCategorySizeDatePackage
ProfileBean.javaAPI DocExample9321Wed Apr 05 11:25:42 BST 2000entity.beanManaged

ProfileBean

public class ProfileBean extends Object implements EntityBean
A ProfileBean, which provides enterprise profile information for a named person.

Fields Summary
public String
mName
public Properties
mEntries
private transient EntityContext
mContext
Constructors Summary
Methods Summary
public voidejbActivate()
During activation, create our entry lookup table.


  // Entity bean methods

            
     
    mEntries = new Properties();
    System.out.println("ProfileBean activated.");
  
public ProfilePKejbCreate()
Create method (corresponds to each create() method on the home interface, ProfileHome). Nothing to initialize in this case.

    System.out.println("Nameless ProfileBean created.");
    return new ProfilePK();
  
public ProfilePKejbCreate(java.lang.String name)
Create method with name of profile owner.

    try {
      Connection conn = newConnection();
      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) {}
    }
      
    System.out.println("ProfileBean created for " + name + ".");
    return new ProfilePK(name);
  
public java.util.EnumerationejbFindByEntryValue(java.lang.String key, java.lang.String value)

    Vector userList = new Vector();
    // Get a new connection fro the EJB server
    try {
      Connection conn = newConnection();
      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.addElement(new ProfilePK(name));
      }
    }
    catch (SQLException se) {
      // Failed to do database lookup
      throw new FinderException();
    }
    return userList.elements();
  
public ProfilePKejbFindByPrimaryKey(ProfilePK key)
Since we're managing persistence here in the bean, we need to implement the finder methods.

    loadFromDB(key);
    return key;
  
public voidejbLoad()
Load bean from persistent store. In this case, we're managing the dbase storage, so we store our profile entries as independent records in a separate "PROFILE_ENTRY" table.

    try {
      ProfilePK key = (ProfilePK)mContext.getPrimaryKey();
      loadFromDB(key);
    }
    catch (Exception e) {
      System.out.println("Failed to load ProfileBean: ");
      e.printStackTrace();
      throw new RemoteException("ejbLoad failed: ", e);
    }
    System.out.println("ProfileBean load finished.");
  
public voidejbPassivate()
When we're passivated, release our entry table.

    mEntries = null;
    System.out.println("ProfileBean passivated.");
  
public voidejbPostCreate()
Post-creation notification. Nothing to do here, but we need to provide an implementation.

    System.out.println("ProfileBean post-create called.");
  
public voidejbPostCreate(java.lang.String name)
Post-creation notification. Nothing to do here, what we need to provide an implementation.

    System.out.println("ProfileBean post-create called for " + name + ".");
  
public voidejbRemove()
Remove this named profile from the database.

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

      s.close();
      conn.close();
      System.out.println("ProfileBean removed.");
    }
    catch (SQLException se) {
      System.out.println("Error removing profile for " + key.mName);
      se.printStackTrace();
    }
  
public voidejbStore()
Store bean to persistent store. Properties are stored as records in the PROFILE_ENTRY table.

    ProfilePK key = (ProfilePK)mContext.getPrimaryKey();
    try {
      Connection conn = newConnection();
      // Clear out old profile entries and replace with the current ones
      Statement s = conn.createStatement();
      s.executeUpdate("delete from profile_entry where name = '" + key.mName +
                      "'");
      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.mName + "', '" + 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 RemoteException("ejbStore failed: ", e);
    }
    System.out.println("ProfileBean store finished.");
  
public java.lang.StringgetEntry(java.lang.String key)

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

    ProfilePK key = (ProfilePK)mContext.getPrimaryKey();
    return key.mName;
  
protected voidloadFromDB(ProfilePK key)

    boolean found = false;
    try {
      Connection conn = newConnection();
      Statement s = conn.createStatement();
      s.executeQuery("select name from profile where name = '" + key.mName +
                     "'");
      ResultSet rs = s.getResultSet();
      if (rs.next()) {
        found = true;
        s.executeQuery("select key, value from profile_entry where name = '" +
                       key.mName + "'");
        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());
    }
    if (!found) {
      throw new FinderException("No profile found for " + key.mName);
    }
  
private java.sql.ConnectionnewConnection()

    // Make sure that the JDBC driver is loaded
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
    }
    catch (ClassNotFoundException cnfe) {
      System.out.println("Failed to load JDBC drivers.");
    }
    // Set the connection properties
    Properties dbProps = new Properties();
    dbProps.put("user", "libra");
    dbProps.put("password", "banana");
//     dbProps.put("server", "dev1");
    // Get the connection from the pool
    return DriverManager.getConnection("jdbc:oracle:thin:@tarazed.hbs.edu:1525:dev1", dbProps);
  
public voidsetEntityContext(EntityContext context)
Get context from container.

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

    if (mEntries == null) // ejbhome doesn't call ejbActivate reliably...
      mEntries = new Properties();
    mEntries.put(key, value);
  
public voidunsetEntityContext()
Container is removing our context.

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