package org.jboss.ejb.plugins;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import javax.ejb.EJBObject;
import javax.ejb.Handle;
import javax.ejb.CreateException;
import javax.ejb.DuplicateKeyException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import org.jboss.ejb.Container;
import org.jboss.ejb.EntityContainer;
import org.jboss.ejb.EntityPersistenceStore;
import org.jboss.ejb.EntityEnterpriseContext;
import org.jboss.ejb.GenericEntityObjectFactory;
import org.jboss.metadata.EntityMetaData;
import org.jboss.system.server.ServerConfigLocator;
import org.jboss.system.ServiceMBeanSupport;
import org.jboss.util.file.FilenameSuffixFilter;
public class CMPFilePersistenceManager
extends ServiceMBeanSupport
implements EntityPersistenceStore
{
public static final String DEFAULT_STORE_DIRECTORY_NAME = "entities";
private EntityContainer con;
private String storeDirName = DEFAULT_STORE_DIRECTORY_NAME;
private File storeDir;
private Field idField;
private Method isModified;
public void setContainer(final Container c)
{
con = (EntityContainer)c;
}
public void setStoreDirectoryName(final String dirName)
{
this.storeDirName = dirName;
}
public String getStoreDirectoryName()
{
return storeDirName;
}
public File getStoreDirectory()
{
return storeDir;
}
protected void createService() throws Exception
{
boolean debug = log.isDebugEnabled();
String ejbName = con.getBeanMetaData().getEjbName();
File dir = ServerConfigLocator.locate().getServerDataDir();
dir = new File(dir, storeDirName);
dir = new File(dir, ejbName);
storeDir = dir;
if (debug) {
log.debug("Storing entity state for '" + ejbName + "' in: " + storeDir);
}
if (!storeDir.exists()) {
if (!storeDir.mkdirs()) {
throw new IOException("Failed to create directory: " + storeDir);
}
}
if (!storeDir.isDirectory()) {
throw new IOException("File exists where directory expected: " + storeDir);
}
if (!storeDir.canWrite() || !storeDir.canRead()) {
throw new IOException("Directory must be readable and writable: " + storeDir);
}
idField = con.getBeanClass().getField("id");
if (debug) log.debug("Using id field: " + idField);
try
{
isModified = con.getBeanClass().getMethod("isModified", new Class[0]);
if (!isModified.getReturnType().equals(Boolean.TYPE)) {
isModified = null; log.warn("Found isModified method, but return type is not boolean; ignoring");
}
else {
if (debug) log.debug("Using isModified method: " + isModified);
}
}
catch (NoSuchMethodException ignored) {}
}
protected void destroyService() throws Exception
{
storeDir.delete();
}
public Object createBeanClassInstance() throws Exception
{
return con.getBeanClass().newInstance();
}
public void initEntity(final EntityEnterpriseContext ctx)
{
Object instance = ctx.getInstance();
Class ejbClass = instance.getClass();
Field cmpField;
Class cmpFieldType;
EntityMetaData metaData = (EntityMetaData)con.getBeanMetaData();
Iterator i = metaData.getCMPFields();
while (i.hasNext())
{
try
{
cmpField = ejbClass.getField((String)i.next());
cmpFieldType = cmpField.getType();
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)
{
}
catch (Exception e)
{
throw new EJBException(e);
}
}
}
public Object createEntity(final Method m,
final Object[] args,
final EntityEnterpriseContext ctx)
throws Exception
{
try {
Object id = idField.get(ctx.getInstance());
if (getFile(id).exists())
throw new DuplicateKeyException("Already exists: "+id);
storeEntity(id, ctx.getInstance());
return id;
}
catch (IllegalAccessException e)
{
throw new CreateException("Could not create entity: "+e);
}
}
public Object postCreateEntity(final Method m,
final Object[] args,
final EntityEnterpriseContext ctx)
throws Exception
{
return null;
}
public Object findEntity(final Method finderMethod,
final Object[] args,
final EntityEnterpriseContext ctx,
GenericEntityObjectFactory factory)
throws FinderException
{
if (finderMethod.getName().equals("findByPrimaryKey"))
{
if (!getFile(args[0]).exists())
throw new FinderException(args[0]+" does not exist");
return factory.getEntityEJBObject(args[0]);
}
return null;
}
public Collection findEntities(final Method finderMethod,
final Object[] args,
final EntityEnterpriseContext ctx,
GenericEntityObjectFactory factory)
{
if (finderMethod.getName().equals("findAll"))
{
String[] files = storeDir.list(new FilenameSuffixFilter(".ser"));
ArrayList result = new ArrayList(files.length);
for (int i = 0; i < files.length; i++) {
final String key = files[i].substring(0,files[i].length()-4);
result.add(factory.getEntityEJBObject(key));
}
return result;
}
else
{
return Collections.EMPTY_LIST;
}
}
public void activateEntity(final EntityEnterpriseContext ctx)
{
}
public void loadEntity(final EntityEnterpriseContext ctx)
{
try
{
Object obj = ctx.getInstance();
ObjectInputStream in = new CMPObjectInputStream
(new BufferedInputStream(new FileInputStream(getFile(ctx.getId()))));
try {
Field[] f = obj.getClass().getFields();
for (int i = 0; i < f.length; i++)
{
f[i].set(obj, in.readObject());
}
}
finally {
in.close();
}
}
catch (Exception e)
{
throw new EJBException("Load failed", e);
}
}
private void storeEntity(Object id, Object obj)
{
try
{
ObjectOutputStream out = new CMPObjectOutputStream
(new BufferedOutputStream(new FileOutputStream(getFile(id))));
try {
Field[] f = obj.getClass().getFields();
for (int i = 0; i < f.length; i++)
{
out.writeObject(f[i].get(obj));
}
}
finally {
out.close();
}
}
catch (Exception e)
{
throw new EJBException("Store failed", e);
}
}
public boolean isStoreRequired(final EntityEnterpriseContext ctx) throws Exception
{
if (isModified == null)
{
return true;
}
Boolean modified = (Boolean) isModified.invoke(ctx.getInstance(), new Object[0]);
return modified.booleanValue();
}
public boolean isModified(EntityEnterpriseContext ctx) throws Exception
{
return isStoreRequired(ctx);
}
public void storeEntity(final EntityEnterpriseContext ctx)
{
storeEntity(ctx.getId(), ctx.getInstance());
}
public void passivateEntity(final EntityEnterpriseContext ctx)
{
}
public void removeEntity(final EntityEnterpriseContext ctx)
throws RemoveException
{
File file = getFile(ctx.getId());
if (!file.delete()) {
throw new RemoveException("Could not remove file: " + file);
}
}
protected File getFile(final Object id)
{
return new File(storeDir, String.valueOf(id) + ".ser");
}
static class CMPObjectOutputStream
extends ObjectOutputStream
{
public CMPObjectOutputStream(final OutputStream out)
throws IOException
{
super(out);
enableReplaceObject(true);
}
protected Object replaceObject(final Object obj)
throws IOException
{
if (obj instanceof EJBObject)
return ((EJBObject)obj).getHandle();
return obj;
}
}
static class CMPObjectInputStream
extends ObjectInputStream
{
public CMPObjectInputStream(final InputStream in)
throws IOException
{
super(in);
enableResolveObject(true);
}
protected Object resolveObject(final Object obj)
throws IOException
{
if (obj instanceof Handle)
return ((Handle)obj).getEJBObject();
return obj;
}
}
}