/*
 * File: LoginBean.java
 * Created on Mar 8, 2005
 */
package com.dragonsoft.tryapp.ejb.session;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import com.dragonsoft.tryapp.common.UserCredentials;
import com.dragonsoft.tryapp.ejb.entity.interfaces.UserHome;
/**
 * Resposible for authentication of users.
 * @ejb.bean name="Login"
 *           display-name="Name for Login"
 *           description="Description for Login"
 *           jndi-name="ejb/Login"
 *           type="Stateless"
 *           view-type="remote"
 */
public class LoginBean implements SessionBean {

	/**
	 * 
	 */
	public LoginBean() {
		super();
	}

	/* 
	 * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
	 */
	public void setSessionContext(SessionContext arg0)
		throws EJBException,
		RemoteException {}

	/* 
	 * @see javax.ejb.SessionBean#ejbRemove()
	 */
	public void ejbRemove() throws EJBException, RemoteException {}

	/* 
	 * @see javax.ejb.SessionBean#ejbActivate()
	 */
	public void ejbActivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* 
	 * @see javax.ejb.SessionBean#ejbPassivate()
	 */
	public void ejbPassivate() throws EJBException, RemoteException {}

	/**
	 * Authenticate a user against the database
	 * 
	 * @param uc
	 * @return The Credentials for a User
	 * 
	 * @ejb.interface-method  view-type = "remote"
	 */

	public UserCredentials login(String username, String password, String ip)
		throws RemoteException, FinderException {
		UserCredentials userCredentials = null;
		List list = new ArrayList();
	    try {
			InitialContext context = new InitialContext();
			Object obj = context.lookup("ejb/User");
			UserHome uh = (UserHome) PortableRemoteObject.narrow(
				obj,
				UserHome.class);
			
			com.dragonsoft.tryapp.ejb.entity.interfaces.User ub = uh.findByChallenge(username,password);
		
			userCredentials = new UserCredentials( username, null );

		} catch (FinderException e) {
			System.err.println(e.getMessage());
			throw e;
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return userCredentials;
	}
	
	/**
	 * Create method
	 * @ejb.create-method  view-type = "remote"
	 */
	public void ejbCreate() throws javax.ejb.CreateException {
	}
}