package org.nthx.pat;
import org.apache.log4j.Logger;
import org.prevayler.Command;
import org.prevayler.PrevalentSystem;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
public class BOStorageCommand
implements Command
{
private Class clazz;
private ObjectForStream[] classAndArgsForStream;
private Class[] parameterTypes;
public BOStorageCommand(Constructor constructor,
Object[] args)
{
this.clazz = constructor.getDeclaringClass();
this.classAndArgsForStream =
CommandGenerationHelper.targetAndBos2FakeOIDs("", args);
this.parameterTypes = constructor.getParameterTypes();
if (!Modifier.isPublic(constructor.getModifiers()))
throw new IllegalStateException("Constructor: " + constructor
+ " has to be public!");
}
public Serializable execute(PrevalentSystem system)
throws Throwable
{
Constructor constructor = clazz.getConstructor(parameterTypes);
Object[] recoveredArgs =
CommandGenerationHelper.fakeBOs2targetAndBos((IdentityMap) system,
classAndArgsForStream,
constructor.toString(),
parameterTypes);
try
{
Object[] recoveredArgsOnly =
new Object[recoveredArgs.length - 1];
System.arraycopy(recoveredArgs, 1,
recoveredArgsOnly, 0,
recoveredArgsOnly.length);
constructor.setAccessible(true);
Object created = constructor.newInstance(recoveredArgsOnly);
Pat.getInstance().getRootAsIdentityMap().putObject((Identifiable) created);
return (Serializable) created;
}
catch (IllegalAccessException e)
{
throw e.getCause();
}
catch (java.lang.reflect.InvocationTargetException e)
{
throw e.getCause();
}
}
private void logRecoveredParams(Object[] recoveredParams)
{
for (int i = 0; i < recoveredParams.length; i++)
{
Object recoveredParam = recoveredParams[i];
log.debug("\tRecovered: " + recoveredParam.toString());
}
}
private transient static Logger log = Logger.getLogger("pat");
}