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

import java.rmi.RemoteException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.rmi.PortableRemoteObject;


/**
 * Stress Test with create/remove
 * @author Philippe Durieux
 */
public class A_remthread extends Thread {
    String managerName;
    String name;
    int accounts;
    int loops;
    Context ictx; 

    public A_remthread(String mName, int num, Context ictx, int accounts, int loops) {
        managerName = mName;
        name = managerName + "_remac_" + num;
        setName(name);
        this.ictx = ictx;
        this.accounts = accounts;
        this.loops = loops;
    }

    /**
     * random returns an integer between 0 and max - 1
     */
    private int random(int max) throws RemoteException {
        double d = Math.random();
        int ret = (int) (max * d);
        return ret;
    }

    public void run() {

        // Create a session bean
        ManagerHome mgrHome = null;
        Manager mgr = null;
        try {
            mgrHome = (ManagerHome) PortableRemoteObject.narrow(ictx.lookup(managerName),
                                                                ManagerHome.class);
            mgr = mgrHome.create(F_remove.initialValue);
        } catch (Exception e) {
            System.out.println("Cannot Create Session:" + e);
            return;
        }

        try {
            // main loop
            boolean ok = true;
            for (int i = 0; i < loops; i++) {
                // Choose an account randomly
                int a1 = random(accounts);
                // Remove it or Create it randomly
                int value = random(100);
                if (value < 10) {
                    mgr.delAccount(a1);
                } else {
                    mgr.getAccount(a1);
                }
            }

            // delete Session
            mgr.remove();
        } catch (RemoteException e) {
            System.out.println("Thread " + name + " : " + e);
            F_remove.threadfail = true;
        } catch (RemoveException e) {
            System.out.println("Thread " + name + " Cannot remove bean: " + e);
            F_remove.threadfail = true;
        }
    }
}
