package com.sap.mw.jco.jra.examples;

import java.rmi.*;
import javax.ejb.*;
import javax.resource.cci.*;
import javax.naming.InitialContext;
import java.util.Properties;

/**
 * This is an example of a container managed stateless session bean (container-managed demarcation).
 * The application managed authorization (AMA) is used here.
 * (check, if you have set in the ejb-jar.xml the tag <res-auth>Application</res-auth> correct)
 * Note, the J2EE recomends to use container managed authorization, instead of
 * application managed authorization, to provide easier portability of your applications.
 */
public class ExampleAMABean implements SessionBean
{
    private SessionContext sessionContext;
    public void ejbCreate()
    {
    }
    public void ejbRemove() throws RemoteException
    {
    }
    public void ejbActivate() throws RemoteException
    {
    }
    public void ejbPassivate() throws RemoteException
    {
    }
    public void setSessionContext(SessionContext sessionContext) throws RemoteException
    {
      this.sessionContext = sessionContext;
    }

    /**
     * This method creates a connection to R/3
     */
    public String testSimpleConnection(Properties properties) throws RemoteException
    {
        String result="";
        Connection connection = null;
        ConnectionSpec connectionSpec = null;
        try
        {
            System.out.println("\n*************************************************\n");
            System.out.println("call method testSimpleConnection");

            // create new InitialContext
            InitialContext initialcontext = new InitialContext();

            // lookup for ConnectionFactory in JNDI
            ConnectionFactory connectionfactory = (ConnectionFactory) initialcontext.lookup("eis/SAPJRAFactory");
            //ConnectionFactory connectionfactory = (ConnectionFactory) initialcontext.lookup("java:comp/env/eis/SAPJRAFactory");

            // you can also specify connection properties diferent from default properties
            // already defined in the ManagedConnectionFactory. If you don't specify any,
            // the default properties will be taken.
            if (properties != null) {
                  connectionSpec = new com.sap.mw.jco.jra.JRA.ConnectionSpecImpl(properties);
                  connection = connectionfactory.getConnection(connectionSpec);
            }else connection = connectionfactory.getConnection(connectionSpec);
            // If property != null, then the SAPJRA proprietary class JRA.ConnectionSpecImpl
            // will be loaded. To allow it you may need to make a reference (applied for SAP J2EE Engine) from
            // your application (ear-file) to the Resource Adapter.

            // to use JRA.ConnectionSpecImpl(Properties) see in api what kind of
            // properties are allowed

            // You can also use following constructor to create a connectionSpec instance:
            // ConnectionSpec connectionSpec =
            // new JRA.ConnectionSpecImpl( "000",      // SAP client
            //                            "johndoe",  // userid
            //                            "*****",    // password
            //                            "EN");       // language

            result="connection created successful";
            System.out.println("connection created");
            System.out.println("Product Version ist: "+connection.getMetaData().getEISProductVersion());
        }
        catch (Exception e)
        {
            result="connection failed";
            System.out.println("connection failed");
            e.printStackTrace();
        }
        finally
        {
            if (connection != null)
            {
                try
                {
                    connection.close();
                }
                catch(Exception exception1) { }
            }
        }
        return result;
    }
}