/*
* This example is from the book "Java Enterprise in a Nutshell".
* Copyright (c) 1999 by O'Reilly & Associates.
* You may distribute this source code for non-commercial purposes only.
* You may study, modify, and use this example for any purpose, as long as
* this notice is retained. Note that this example is provided "as is",
* WITHOUT WARRANTY of any kind either expressed or implied.
*/
package entity.containerManaged;
import javax.ejb.*;
import java.rmi.RemoteException;
import NoSuchPersonException;
import java.util.Properties;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* A ProfileBean, which provides enterprise
* profile information for a named person.
*/
public class ProfileBean implements EntityBean {
//
// Persistent state for this bean
//
// Name of the person owning the profile
public String mName = "";
// Entries in the profile (name/value pairs)
transient private Properties mEntries = new Properties();
// Serialized representation of profile entries, for storage in dbase/file
public byte[] mEntriesBytes = null;
// Store context (non-persistent)
private transient EntityContext mContext = null;
// A dirty bit, so that we know if the persistent state needs an update
private transient boolean mDirtyBit = false;
// Entity bean methods
/**
* No need for us to activate anything in this bean, but we need to
* provide an implementation.
*/
public void ejbActivate() {
System.out.println("ProfileBean activated.");
}
/**
* Load bean from persistent store. Since the profile entries are stored in
* a non-primitive object (Properties), they are stored in the database as a
* raw byte array. In this load method, we convert the serialized bytes
* loaded by the container into the Properties object.
*/
public void ejbLoad() throws RemoteException {
try {
ByteArrayInputStream byteIn = new ByteArrayInputStream(mEntriesBytes);
ObjectInputStream objIn = new ObjectInputStream(byteIn);
mEntries = (Properties)objIn.readObject();
}
catch (Exception e) {
System.out.println("Failed to load ProfileBean: ");
e.printStackTrace();
throw new RemoteException("ejbLoad failed: ", e);
}
System.out.println("ProfileBean load finished.");
}
/**
* Store bean to persistent store. We store our Properties object in the
* database as a serialized byte array. Here, we serialize the Properties
* object so that the container can store it.
*/
public void ejbStore() throws RemoteException {
try {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream objOut = new ObjectOutputStream(byteOut);
objOut.writeObject(mEntries);
mEntriesBytes = byteOut.toByteArray();
}
catch (Exception e) {
System.out.println("Failed to store ProfileBean: ");
e.printStackTrace();
throw new RemoteException("ejbStore failed: ", e);
}
System.out.println("ProfileBean store finished.");
}
/**
* Nothing to do on a remove.
*/
public void ejbRemove() {
System.out.println("ProfileBean removed.");
}
/**
* No state to store on passivation.
*/
public void ejbPassivate() {
System.out.println("ProfileBean passivated.");
}
/**
* Get context from container.
*/
public void setEntityContext(EntityContext context) {
System.out.println("ProfileBean context set.");
mContext = context;
}
/**
* Container is removing our context.
*/
public void unsetEntityContext() throws RemoteException {
System.out.println("ProfileBean context unset.");
mContext = null;
}
/**
* Create method (corresponds to each create() method on the
* home interface, ProfileHome). Nothing to initialize in this case.
*/
public void ejbCreate() {
System.out.println("Nameless ProfileBean created.");
}
/**
* Post-creation notification. Nothing to do here, but we need
* to provide an implementation.
*/
public void ejbPostCreate() {
System.out.println("ProfileBean post-create called.");
}
/**
* Create method with name of profile owner.
*/
public void ejbCreate(String name) throws NoSuchPersonException {
mName = name;
System.out.println("ProfileBean created for " + mName + ".");
}
/**
* Post-creation notification. Nothing to do here, what we need
* to provide an implementation.
*/
public void ejbPostCreate(String name) {
System.out.println("ProfileBean post-create called.");
}
// Finally, the remote methods. Signatures of methods must match those
// on remote interface (Profile), except that throwing
// RemoteException is not necessary.
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
mDirtyBit = true;
}
public String getEntry(String key) {
return mEntries.getProperty(key);
}
public void setEntry(String key, String value) {
mEntries.put(key, value);
mDirtyBit = true;
}
public boolean isDirty() {
return mDirtyBit;
}
}
|