package org.nthx.pat;
import org.apache.log4j.Logger;
import org.jboss.aop.joinpoint.ConstructorInvocation;
import org.jboss.aop.joinpoint.Invocation;
import org.prevayler.PrevalentSystem;
import org.prevayler.implementation.SnapshotPrevayler;
import org.nthx.util.jbossaop.DefaultInterceptorImpl;
import java.lang.reflect.Constructor;
import java.util.Arrays;
public class RootCreationInterceptor
extends DefaultInterceptorImpl
{
private static String REPOSITORY_DIR = System.getProperty("repository.dir", "");
private static Logger log = Logger.getLogger("pat");
public Object invoke(Invocation invocation)
throws Throwable
{
ConstructorInvocation ci = (ConstructorInvocation) invocation;
Constructor rootConstructor = ci.getConstructor();
Object[] arguments = ci.getArguments();
if (Pat.finishedCreatingRoot)
{
String warning = "\nYou try to instantiate ROOT class more than once."
+ "\nIt is not possible."
+ "\nIf you're testing you system with PAT and need to instantiate"
+ "\nPAT in every test case, then use `Pat.unload()' method."
+ "\nFurther explanation in: <PAT>/docs/htdocs/framework.html\n\n";
throw new IllegalStateException(warning);
}
log.debug("Pat.Root's class is: " + rootConstructor);
if (null == arguments)
arguments = new Object[]{};
log.debug("Root's args: " + Arrays.asList(arguments));
PrevalentSystem system = (PrevalentSystem) rootConstructor.newInstance(arguments);
SnapshotPrevayler snapshotPrevayler = null;
if (!REPOSITORY_DIR.equals(""))
{
snapshotPrevayler = new SnapshotPrevayler(
system,
REPOSITORY_DIR);
}
else
{
snapshotPrevayler = new SnapshotPrevayler(system);
}
Pat.getInstance().setSnapshotPrevayler(snapshotPrevayler);
if (Pat.getInstance().isFreshRoot())
{
log.debug("PAT always takes snapshot number ZERO.");
Pat.getInstance().takeSnapshot();
}
Pat.getInstance().finishedCreatingRoot();
return snapshotPrevayler.system();
}
}