/**
 * Protocols test
 * Copyright (C) 2007 Bull S.A.S.
 * 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 (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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.ow2.jonas.protocols.test.ejbs;

import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EJBHome;
import javax.ejb.EJBObject;
import javax.ejb.Handle;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;


/**
 * @author Francois Waeselynck
 */
public class BackAEJB implements BackServiceA, SessionBean {

	/**
     *
     */
    private static final long serialVersionUID = 0L;
    /* (non-Javadoc)
	 * @see org.ow2.jonas.protocols.test.ejbs.BackServiceA#call(java.lang.String)
	 */
	public long call(String comment) throws RemoteException {
		if (delay != 0) {
			try {
				Thread.sleep(delay);
			} catch (InterruptedException e) {
				// nothing to do
			}
		}
		return System.currentTimeMillis();
	}

	public Request call(Request r) throws RemoteException {
		r.addStamp(id, "call IN");
		if (delay != 0) {
			try {
				Thread.sleep(delay);
			} catch (InterruptedException e) {
				// nothing to do
			}
		}
		r.addStamp(id, "call OUT");
		return r;
	}


	/* (non-Javadoc)
	 * @see javax.ejb.EJBObject#getEJBHome()
	 */
	public EJBHome getEJBHome() throws RemoteException {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EJBObject#getHandle()
	 */
	public Handle getHandle() throws RemoteException {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EJBObject#getPrimaryKey()
	 */
	public Object getPrimaryKey() throws RemoteException {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EJBObject#isIdentical(javax.ejb.EJBObject)
	 */
	public boolean isIdentical(EJBObject arg0) throws RemoteException {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EJBObject#remove()
	 */
	public void remove() throws RemoteException, RemoveException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#ejbActivate()
	 */
	public void ejbActivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#ejbPassivate()
	 */
	public void ejbPassivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#ejbRemove()
	 */
	public void ejbRemove() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
	 */
	public void setSessionContext(SessionContext sessionContext) throws EJBException,
			RemoteException {
		ejbContext = sessionContext;
	}

	public void ejbCreate() throws CreateException {
		try {
			InitialContext ictx = new InitialContext();
			String tag = (String) ictx.lookup("java:comp/env/params/myTag");
			id = (tag == null ? id : tag);
			Long delay = (Long) ictx.lookup("java:comp/env/params/wait");
			if (delay != null) {
				this.delay = delay.longValue();
			}
		} catch (NamingException e) {
			e.printStackTrace();
			System.exit(2);
		}
	}


	private SessionContext ejbContext = null;
	private String id = "BackAService";
	private long delay = 0;
}
