Methods Summary |
---|
public void | ejbActivate()
//--------------------------------------------------
// Entity bean methods
//--------------------------------------------------
// EJB activation method. During activation, create our entry lookup table.
mEntries = new Properties();
System.out.println("ProfileBean activated.");
|
public java.lang.String | ejbCreate(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.Collection | ejbFindByEntryValue(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.String | ejbFindByPrimaryKey(java.lang.String key)
loadFromDB(key);
return key;
|
public void | ejbLoad()
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 void | ejbPassivate()
mEntries = null;
System.out.println("ProfileBean passivated.");
|
public void | ejbPostCreate(java.lang.String name)
System.out.println("ProfileBean post-create called for " + name + ".");
|
public void | ejbRemove()
// 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 void | ejbStore()
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.Connection | getConnection()
Context ctx = new InitialContext();
DataSource profileDB =
(DataSource)ctx.lookup("java:comp/env/jdbc/profileDB");
return profileDB.getConnection();
|
public java.lang.String | getEntry(java.lang.String key)
return mEntries.getProperty(key);
|
public java.lang.String | getName()
return (String)mContext.getPrimaryKey();
|
protected void | loadFromDB(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 void | setEntityContext(javax.ejb.EntityContext context)
mContext = context;
System.out.println("ProfileBean context set.");
|
public void | setEntry(java.lang.String key, java.lang.String value)
if (mEntries == null) {
mEntries = new Properties();
}
mEntries.put(key, value);
|
public void | unsetEntityContext()
mContext = null;
System.out.println("ProfileBean context unset.");
|