/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * 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
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jtests.beans.folder;

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EJBObject;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionSynchronization;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import org.objectweb.jonas.common.Log;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.BasicLevel;

/**
 * FolderSY implementation
 * This bean is a stateful session bean that implements SessionSynchronization
 * @author Philippe Durieux, Philippe Coq
 */
public class FolderSY implements SessionBean, SessionSynchronization {

    static protected Logger logger = null;
    SessionContext ejbContext;
    InitialContext ictx;
    FileHome filehome;

    // ------------------------------------------------------------------
    // SessionBean implementation
    // ------------------------------------------------------------------

    public void setSessionContext(SessionContext ctx) { 
	if (logger == null)
	    logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
	logger.log(BasicLevel.DEBUG, "");
	ejbContext = ctx;
	try {
	    // Get initial Context
	    ictx = new InitialContext();
	    // lookup filehome in JNDI
	    filehome = (FileHome) PortableRemoteObject.narrow(ictx.lookup("java:comp/env/ejb/file"), FileHome.class);
	} catch (NamingException e) {
	    throw new EJBException("FolderSY: Cannot get filehome:"+e);
	}
    }

    public void ejbRemove() {
	logger.log(BasicLevel.DEBUG, "");
    }
    public void ejbCreate() throws CreateException {
	logger.log(BasicLevel.DEBUG, "");
    }
    public void ejbPassivate() {
	logger.log(BasicLevel.DEBUG, "");
    }
    public void ejbActivate() {
	logger.log(BasicLevel.DEBUG, "");
    }

    // ------------------------------------------------------------------
    // SessionSynchronization implementation
    // ------------------------------------------------------------------

    public void afterBegin() {
	logger.log(BasicLevel.DEBUG, "");
    }
    public void beforeCompletion() {
	logger.log(BasicLevel.DEBUG, "");
    }
    public void afterCompletion(boolean committed) {
	logger.log(BasicLevel.DEBUG, "");
    }

    // ------------------------------------------------------------------
    // Folder implementation
    // ------------------------------------------------------------------

    public File newFile(String fname) throws RemoteException, CreateException {
	logger.log(BasicLevel.DEBUG, "");
	File ret = filehome.create(fname);
	return ret;
    }

    public File getFile(String fname) throws RemoteException, FinderException {
	logger.log(BasicLevel.DEBUG, "");
	File ret = filehome.findByPrimaryKey(fname);
	return ret;
    }
}
