/*  PAT: Persistent Applications Toolkit (patsystem.sf.net)
 *  Copyright (C) 2004, 2005  Tomasz Nazar, nthx at irc dot pl
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Full version of the license is /docs/LICENSE.txt
 */
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;

/** Aspect for intercepting starting moment of the system life -- 
 *  moment of 'new MyRoot(..)'.
 *
 *  <p>Instead of letting to instantiate 
 *  "root" object, PAT creates {@link SnapshotPrevayler} and does original
 *  Prevayler stuff for you.
 *
 *  <p>PAT also recovers data stored in previous "session" of an application.
 *
 *  @version $Id: RootCreationInterceptor.java 3725 2005-06-09 23:57:03Z nthx $
 *  @author nthx@users.sourceforge.net
 */
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));

        //some objects will be created at that moment or before.
        //Those are from root's constructor or passed to it
        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 your system is "fresh" (new) we must save the state now,
         *  because there are some objects created in your Root.new().
         *  Well, for sure one object is created: your Root.
         */
        if (Pat.getInstance().isFreshRoot())
        {
            log.debug("PAT always takes snapshot number ZERO.");
            Pat.getInstance().takeSnapshot();
        }
        
        Pat.getInstance().finishedCreatingRoot();

        return snapshotPrevayler.system();
    }
}