Methods Summary |
---|
public void | ejbActivate()During activation, create our entry lookup table.
// Entity bean methods
mEntries = new Properties();
System.out.println("ProfileBean activated.");
|
public ProfilePK | ejbCreate()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 ProfilePK | ejbCreate(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.Enumeration | ejbFindByEntryValue(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 ProfilePK | ejbFindByPrimaryKey(ProfilePK key)Since we're managing persistence here in the bean, we need to
implement the finder methods.
loadFromDB(key);
return key;
|
public void | ejbLoad()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 void | ejbPassivate()When we're passivated, release our entry table.
mEntries = null;
System.out.println("ProfileBean passivated.");
|
public void | ejbPostCreate()Post-creation notification. Nothing to do here, but we need
to provide an implementation.
System.out.println("ProfileBean post-create called.");
|
public void | ejbPostCreate(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 void | ejbRemove()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 void | ejbStore()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.String | getEntry(java.lang.String key)
return mEntries.getProperty(key);
|
public java.lang.String | getName()
ProfilePK key = (ProfilePK)mContext.getPrimaryKey();
return key.mName;
|
protected void | loadFromDB(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.Connection | newConnection()
// 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 void | setEntityContext(EntityContext context)Get context from container.
mContext = context;
System.out.println("ProfileBean context set.");
|
public void | setEntry(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 void | unsetEntityContext()Container is removing our context.
mContext = null;
System.out.println("ProfileBean context unset.");
|