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

// DBEnvSL.java
// Stateless Session bean

package org.objectweb.jonas.jtests.ftables;

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


/**
 * Session bean used to init all db tables
 */
public class DBEnvSL implements SessionBean {

    SessionContext ejbContext;

    // ------------------------------------------------------------------
    // SessionBean implementation
    // ------------------------------------------------------------------

    public void setSessionContext(SessionContext ctx) {
	ejbContext = ctx;
    }

    public void ejbRemove() {
    }
	
    public void ejbCreate() throws CreateException {
    }

    public void ejbPassivate() {
    }

    public void ejbActivate() {
    }

    // ------------------------------------------------------------------
    // Utility methods
    // ------------------------------------------------------------------
    
    /**
     * lookup DataSource in JNDI
     */
    public static DataSource getDataSource(String jndiName) throws NamingException {
	Context initialContext = new InitialContext(); 
	DataSource ds = (DataSource)initialContext.lookup(jndiName);
	return ds;
    }

    // ------------------------------------------------------------------
    // DBEnv implementation
    // ------------------------------------------------------------------

    /**
     * initTable
     */
    public void initTable(String container) throws RemoteException {
        try {
            Class.forName("org.objectweb.jonas.jtests.ftables."+"T"+container)
                .getMethod("init", new Class[0])
                .invoke(null, new Object[0]);
        } catch(Exception e) {
            System.err.println("Could not init table for " + container + ": " + e);
            throw new RemoteException("Could not init table for " + container);
        }
    }
}


