/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2006 Bull S.A.S.
 * Contact: jonas-team@objectweb.org
 * Copyright (C) 2006 Distributed Systems Lab.
 * Universidad Polit??cnica de Madrid (Spain)
 * Contact: http://lsd.ls.fi.upm.es/lsd
 *
 * 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.1 of the License, or 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
 *
 * --------------------------------------------------------------------------
 *
 * --------------------------------------------------------------------------
 */
package org.objectweb.jonas_ejb.container;

import org.objectweb.carol.cmi.configuration.TraceCmi;
import org.objectweb.carol.cmi.ha.RequestId;
import org.objectweb.jonas.ha.interceptor.HACurrentDelegateImpl;
import org.objectweb.jonas_ejb.deployment.api.SessionDesc;

/**
 * Generic part of the EJBLocalHome implementation for replicated SFSBs
 * @author Francisco Perez-Sorrosal (fpsorrosal@no-spam@fi.upm.es)
 * @author Alberto Paz-Jimenez (apaz@no-spam@fi.upm.es)
 */
public abstract class JRepStatefulLocalHome extends JSessionLocalHome {

    /**
     * constructor
     * @param dd The Session Deployment Decriptor
     * @param bf The Session Factory
     * @throws Exception 
     */
    public JRepStatefulLocalHome(SessionDesc dd, JSessionFactory bf) {
        super(dd, bf);
        if (TraceCmi.isDebugCmiHA()) {
            TraceCmi.debugCmiHA("JRepStatefulLocalHome : this=" + this);
        }
    }

    /**
     * Called if we have to replicate a request
     * @param rctx The RequestCtx that was returned at preInvoke()
     */
    protected void replicateCreate(JStatefulSwitch sfsw) {
        if (TraceCmi.isDebugCmiHA()) {
            TraceCmi.debugCmiHA("replicateBean : this=" + this);
        }
        // Create the rootId from the current HACtx/Thread association
        HACurrentDelegateImpl current = HACurrentDelegateImpl.getCurrent();
        // Obtain the root request
        RequestId rootId = null;
        try { 
            rootId = (RequestId) current.getRequests().get(0);
            
            
            JRepStatefulLocal jrsl = (JRepStatefulLocal) sfsw.getLocal();
            
            JRepUtil.addModifiedBean(sfsw, jrsl.getClusterOId(), rootId);
        } catch (ArrayIndexOutOfBoundsException e) {
            if (TraceCmi.isDebugCmiHA()) {
                TraceCmi.debugCmiHA("Not in a remote method call chain.");
            }
            // do nothing
        }
    }

    protected BeanFactory getBeanFactory() {
        return bf;
    }
    
    /**
     * Creates the EJBLocalObject This is in the generated class because it is
     * mainly "new objectClass()"
     * @return The Local Object
     */
    abstract public JSessionLocal createLocalObject();

}
