/*
 * 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.local;

import javax.ejb.CreateException;
import javax.transaction.UserTransaction;

import org.objectweb.util.monolog.api.BasicLevel;

/**
 * Stateful Session
 * @author Philippe Durieux, Philippe Coq
 */
public class TargetSF extends TargetSL {

    boolean dirty = false;
    public long mylong = 1953L;
    public double pi = 3.14159;
    public String mystring = "leChatQuiDort";
    public UserTransaction ut;
    
    private void checkAllowedMethods() throws CreateException {
        
        // These operations are allowed
        ejbContext.getEJBHome();
        ejbContext.getEJBLocalHome();
        ejbContext.getEJBObject();
        ejbContext.getEJBLocalObject();
        ejbContext.getCallerPrincipal();
        ejbContext.isCallerInRole("");

        // These operations are disallowed
        try {
            ejbContext.getTimerService();
            throw new CreateException("getTimerService disallowed");
        } catch (IllegalStateException e) {
            logger.log(BasicLevel.DEBUG, "getTimerService disallowed");
        }
    }
    
    /**
     * The Session bean must define 1 or more ejbCreate methods.
     *
     * @throws CreateException Failure to create a session EJB object.
     */
    public void ejbCreate() throws CreateException {
        logger.log(BasicLevel.DEBUG, "");
        string = "";
        number = 0;
        createdViaCreateXX = false;        
        createdViaCreateYY = false;
        checkAllowedMethods();
    }

	
    /**
     * ejbCreate methods with parameter
     *
     * @throws CreateException Failure to create a session EJB object.
     */
    public void ejbCreate(String s, int n) throws CreateException {
        logger.log(BasicLevel.DEBUG, "");
        string = s;
        number = n;
        createdViaCreateXX = false;        
        createdViaCreateYY = false;
        checkAllowedMethods();
        ut = ejbContext.getUserTransaction();
    }

    	
    /**
     * ejbCreate methods for a create<METHOD>
     *
     * @throws CreateException Failure to create a session EJB object.
     */
    public void ejbCreateXX(String s, int n) throws CreateException {
        logger.log(BasicLevel.DEBUG, "");
        string = s;
        number = n;
        createdViaCreateXX = true;        
        createdViaCreateYY = false;
        checkAllowedMethods();
    }

    /**
     * ejbCreate methods for a Local create<METHOD>
     *
     * @throws CreateException Failure to create a session EJB object.
     */
    public void ejbCreateYY(String s, int n) throws CreateException {
        logger.log(BasicLevel.DEBUG, "");
        string = s;
        number = n;
        createdViaCreateXX = false;        
        createdViaCreateYY = true;
        checkAllowedMethods();
    }


    // ------------------------------------------------------------------
    // isModified method
    // ------------------------------------------------------------------
    
    public boolean isModified() {
        return dirty;
    }

    // ------------------------------------------------------------------
    // Target implementation
    // ------------------------------------------------------------------

    /**
     * getNumber
     */
    public int  getNumber() {
        logger.log(BasicLevel.DEBUG, "");
        return number;
    }

    /**
     * getString
     */
    public String  getString() {
        logger.log(BasicLevel.DEBUG, "");
        return string;
    }

    /**
     * isCreatedViaCreateXX
     */
    public boolean  isCreatedViaCreateXX() {
        logger.log(BasicLevel.DEBUG, "");
        return createdViaCreateXX;
    }

    // ------------------------------------------------------------------
    // TargetLocal implementation
    // ------------------------------------------------------------------

    /**
     * isCreatedViaCreateYY
     */
    public boolean  isCreatedViaCreateYY() {
        logger.log(BasicLevel.DEBUG, "");
        return createdViaCreateYY;
    }
}
