/*
 * 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$
 * --------------------------------------------------------------------------
 */

// BaseCommon.java

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

import java.util.Hashtable;
import java.rmi.RemoteException;
import java.security.Principal;
import javax.ejb.SessionBean;
import javax.ejb.EJBContext;
import javax.ejb.EJBException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import org.objectweb.jonas.common.Log;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.BasicLevel;

public abstract class BaseCommon {

    static Logger logger = null;

    public abstract EJBContext   getEJBContext();

    // ------------------------------------------------------------------
    // Base implementation
    // ------------------------------------------------------------------

    public String getPrincipalName() throws RemoteException {

	Principal callerPrincipal = getEJBContext().getCallerPrincipal();
        logger.log(BasicLevel.DEBUG, "principal = "+callerPrincipal.getName());
	return callerPrincipal.getName();
    }

    /*
     * Access to the Derived bean
     */

    public String getPrincipalNameOfAnotherBean() throws RemoteException {
	logger.log(BasicLevel.DEBUG, "");
	try {
	    Context it = new InitialContext();
	    DerivedHome h = (DerivedHome) PortableRemoteObject.narrow(it.lookup("securedDerivedSFHome"), 
								      DerivedHome.class);
	    Derived other = h.create();
	    return other.getPrincipalName();
	} catch (RemoteException e) {
	    throw e;
	} catch (Exception e) {
	    e.printStackTrace();
	    return null;
	}
    }

    /**
     * Access to a local interface of a bean
     * we must catch access denied exception 
     */
    public boolean  callAnotherMethod() throws RemoteException {
       	logger.log(BasicLevel.DEBUG, ""); 
        try {
	    Context it = new InitialContext();
	    DerivedLocalHome h = (DerivedLocalHome)it.lookup("java:comp/env/ejb/derivedlocal");
	    DerivedLocal  other = h.create();
	    other.anotherMethod();
        } catch (EJBException e) {
	    return false;
        }catch (Exception e) {
	    e.printStackTrace();
            return false;
	}
        return true;
    }

    public boolean isCallerInRole(String role) throws RemoteException {
	logger.log(BasicLevel.DEBUG, "isCallerInRole "+role);
	return getEJBContext().isCallerInRole(role);
    }

    public void simpleMethod() throws RemoteException {
	logger.log(BasicLevel.DEBUG, "");
    }

    public void complexMethod(Hashtable h, Object[] o) throws RemoteException {
	logger.log(BasicLevel.DEBUG, "");
    }
    
}
