| Method from org.jboss.ejb.plugins.CMPInMemoryPersistenceManager Detail: |
public void activateEntity(EntityEnterpriseContext instance) {
// nothing to do
}
|
public Object createBeanClassInstance() throws Exception {
return con.getBeanClass ().newInstance ();
}
Returns a new instance of the bean class or a subclass of the bean class. |
public Object createEntity(Method m,
Object[] args,
EntityEnterpriseContext ctx) throws Exception {
try
{
Object id = idField.get (ctx.getInstance ());
// Check exist
if (this.beans.containsKey (id))
throw new javax.ejb.DuplicateKeyException ("Already exists: "+id);
// Store to file
storeEntity (id, ctx.getInstance ());
return id;
}
catch (IllegalAccessException e)
{
throw new javax.ejb.CreateException ("Could not create entity: "+e);
}
}
This method is called whenever an entity is to be created.
The persistence manager is responsible for handling the results properly
wrt the persistent store. |
protected void createService() throws Exception {
this.beans = new HashMap(1000);
String ejbName = con.getBeanMetaData ().getEjbName ();
idField = con.getBeanClass ().getField ("id");
log.debug("Using id field: " + idField);
// Lookup the isModified method if it exists
try
{
isModified = con.getBeanClass().getMethod("isModified", new Class[0]);
if (!isModified.getReturnType().equals(Boolean.TYPE)) {
isModified = null; // Has to have "boolean" as return type!
log.warn("Found isModified method, but return type is not boolean; ignoring");
}
else {
log.debug("Using isModified method: " + isModified);
}
}
catch (NoSuchMethodException ignored) {}
}
create the service, do expensive operations etc |
public Collection findEntities(Method finderMethod,
Object[] args,
EntityEnterpriseContext instance,
GenericEntityObjectFactory factory) throws Exception {
if (finderMethod.getName ().equals ("findAll"))
{
return GenericEntityObjectFactory.UTIL.getEntityCollection(factory, this.beans.keySet());
}
else
{
// we only support find all
return Collections.EMPTY_LIST;
}
}
This method is called when collections of entities are to be found. The
persistence manager must find out whether the wanted instances are
available in the persistence store, and if so it must return a
collection of primaryKeys. |
public Object findEntity(Method finderMethod,
Object[] args,
EntityEnterpriseContext instance,
GenericEntityObjectFactory factory) throws Exception {
if (finderMethod.getName ().equals ("findByPrimaryKey"))
{
if (!this.beans.containsKey (args[0]))
throw new javax.ejb.FinderException (args[0]+" does not exist");
return factory.getEntityEJBObject(args[0]);
}
return null;
}
This method is called when single entities are to be found. The
persistence manager must find out whether the wanted instance is
available in the persistence store, if so it returns the primary key of
the object. |
public void initEntity(EntityEnterpriseContext ctx) {
// first get cmp metadata of this entity
Object instance = ctx.getInstance ();
Class ejbClass = instance.getClass ();
Field cmpField;
Class cmpFieldType;
EntityMetaData metaData = (EntityMetaData)con.getBeanMetaData ();
Iterator i= metaData.getCMPFields ();
while(i.hasNext ())
{
// get the field declaration
try
{
cmpField = ejbClass.getField ((String)i.next ());
cmpFieldType = cmpField.getType ();
// find the type of the field and resets it
// to the default value
if (cmpFieldType.equals (boolean.class))
{
cmpField.setBoolean (instance,false);
}
else if (cmpFieldType.equals (byte.class))
{
cmpField.setByte (instance,(byte)0);
}
else if (cmpFieldType.equals (int.class))
{
cmpField.setInt (instance,0);
}
else if (cmpFieldType.equals (long.class))
{
cmpField.setLong (instance,0L);
}
else if (cmpFieldType.equals (short.class))
{
cmpField.setShort (instance,(short)0);
}
else if (cmpFieldType.equals (char.class))
{
cmpField.setChar (instance,'\u0000");
}
else if (cmpFieldType.equals (double.class))
{
cmpField.setDouble (instance,0d);
}
else if (cmpFieldType.equals (float.class))
{
cmpField.setFloat (instance,0f);
}
else
{
cmpField.set (instance,null);
}
}
catch (NoSuchFieldException e)
{
// will be here with dependant value object's private attributes
// should not be a problem
}
catch (Exception e)
{
throw new EJBException (e);
}
}
}
Initializes the instance context.
This method is called before createEntity, and should
reset the value of all cmpFields to 0 or null. |
public boolean isModified(EntityEnterpriseContext ctx) throws Exception {
return isStoreRequired(ctx);
}
|
public boolean isStoreRequired(EntityEnterpriseContext ctx) throws Exception {
if(isModified == null)
{
return true;
}
Boolean modified = (Boolean) isModified.invoke (ctx.getInstance (), new Object[0]);
return modified.booleanValue ();
}
This method is used to determine if an entity should be stored. |
public void loadEntity(EntityEnterpriseContext ctx) {
try
{
// Read fields
java.io.ObjectInputStream in = new CMPObjectInputStream
(new java.io.ByteArrayInputStream ((byte[])this.beans.get (ctx.getId ())));
Object obj = ctx.getInstance ();
Field[] f = obj.getClass ().getFields ();
for (int i = 0; i < f.length; i++)
{
f[i].set (obj, in.readObject ());
}
in.close ();
}
catch (Exception e)
{
throw new EJBException ("Load failed", e);
}
}
This method is called whenever an entity shall be load from the
underlying storage. The persistence manager must load the state from
the underlying storage and then call ejbLoad on the supplied instance. |
public void passivateEntity(EntityEnterpriseContext instance) {
// This plugin doesn't do anything specific
}
|
public Object postCreateEntity(Method m,
Object[] args,
EntityEnterpriseContext ctx) throws Exception {
return null;
}
This method is called after the createEntity.
The persistence manager is responsible for handling the results properly
wrt the persistent store. |
public void removeEntity(EntityEnterpriseContext ctx) throws RemoveException {
if (this.beans.remove (ctx.getId ()) == null)
throw new javax.ejb.RemoveException ("Could not remove bean:" + ctx.getId ());
}
This method is called when an entity shall be removed from the
underlying storage. The persistence manager must call ejbRemove on the
instance and then remove its state from the underlying storage. |
public void setContainer(Container con) {
this.con = (EntityContainer)con;
}
This callback is set by the container so that the plugin may access it |
protected void stopService() throws Exception {
this.beans.clear();
}
|
public void storeEntity(EntityEnterpriseContext ctx) {
storeEntity (ctx.getId (), ctx.getInstance ());
}
This method is called whenever an entity shall be stored to the
underlying storage. The persistence manager must call ejbStore on the
supplied instance and then store the state to the underlying storage. |
protected void storeEntity(Object id,
Object obj) {
try
{
// Store fields
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream ();
java.io.ObjectOutputStream out = new CMPObjectOutputStream (baos);
try {
Field[] f = obj.getClass ().getFields ();
for (int i = 0; i < f.length; i++)
{
out.writeObject (f[i].get (obj));
}
}
finally {
out.close();
}
this.beans.put (id, baos.toByteArray ());
}
catch (Exception e)
{
throw new EJBException ("Store failed", e);
}
}
|