Sample Client Code for an EJB Client Deployed to OC4J

Rather than using the Business Components Browser, you can also choose to test your Business Components project with the following two client code samples for connecting to an AppModule deployed as an EJB session bean to an OC4J instance:

Note: Be sure to include the correct libraries for running the client. You may need to add that information to these samples.

Sample 1

package mypackage;

import oracle.jbo.*;
import oracle.jbo.client.Configuration;
import java.util.Hashtable;

public class sampleClient
{
  public sampleClient()
  {
  }
  public static void main(String arg[]) {

  String _am = "mypackage.MypackageModule"; //App Module name
  String _cf = "MypackageModuleOracleAS"; // EJB Config name
  String voMemberName = "DeptView"; // Name of the View Object

 //Use _wcf if you are accessing Business Components application deployed as
 //as EJB session bean in weblogic

  ApplicationModule myam =
    (ApplicationModule)Configuration.createRootApplicationModule(_am,_cf);

  // Find the viewobject included in the appmodule
    ViewObject vo = myam.findViewObject(voMemberName);

  // Iterate over the viewobject to get the rows

        Row r = vo.first();
         while (r != null)
         {
            // Iterate over the current row and get 
            // all the attributes
            for (int i = 0; i  vo.getAttributeCount(); i++)
            {
               String attrName = vo.getAttributeDef(i).getName();
               String attrVal  = r.getAttribute(i).toString();
               System.out.println(attrName + " = " + attrVal);
            }
            r = vo.next();             
         }


  Configuration.releaseRootApplicationModule(myam, true);

  }
}

    

Sample 2

import javax.naming.*;
import oracle.jbo.*;
import oracle.jbo.client.*;

/***
 ** Sample client code for connecting to an appmdoule deployed 
 ** as an EJB session bean to Oracle Application Server.
 ***/
public class SampleEJBClient 
{
   public static void main(String[] args)  
   {
      /**
       ** Change the following String's to match your appmodule name and the 
       ** name of the viewobject included in that appmodule.
       **
       */
      String amDefName = "package1.Package1Module";
      String voMemberName = "DeptView";

      /**
       ** Change the following to point to the datasource name
       ** (defined in Oracle Application Server). 
       ** to your database. 
       **/
      String dataSourceName = "jdbc/OracleCoreDS";

      /**
       ** Change the following to point to the hostname where the 
       ** appmodule is deployed i.e. the host where Oracle Application Server is running. 
       **/
      String applicationServerHost = "localhost";

      /**
       ** Change the following username and password 
       ** to be used for connecting to the Oracle Application Server. 
       **/
      String iasUserName = "admin";
      String iasUserPasswd = "welcome";

      /**
       ** Change the following to match the name of the J2EE application 
       ** containing the deployed appmodule session bean.
       **/
      String applicationName = "Workspace1_Project1_ejb";

      ApplicationModule am = null;
      try
      {
         // Setup JNDI environment for looking up 
         // the appmodule
         Hashtable env = new Hashtable();
         env.put(Context.INITIAL_CONTEXT_FACTORY,JboContext.JBO_CONTEXT_FACTORY);
         env.put(JboContext.DEPLOY_PLATFORM, JboContext.PLATFORM_EJB_IAS);
         env.put(JboContext.HOST_NAME, applicationServerHost);
         env.put(JboContext.SECURITY_PRINCIPAL, iasUserName);
         env.put(JboContext.SECURITY_CREDENTIALS, iasUserPasswd);
         env.put(JboContext.APPLICATION_PATH, applicationName);
         Context ctx = new InitialContext(env);

         // Lookup appmodule home
         ApplicationModuleHome amHome = (ApplicationModuleHome)ctx.lookup(amDefName);

         // Create an appmodule instance
         am = amHome.create();

         // Connect to the database using the datasource
         am.getTransaction().connectToDataSource(null, 
                                                 dataSourceName,
                                                 false);

         // Find the viewobject included in the appmodule
         ViewObject vo = am.findViewObject(voMemberName);

         // Iterate over the viewobject to get the rows
         Row r = vo.first();
         while (r != null)
         {
            // Iterate over the current row and get 
            // all the attributes
            for (int i = 0; i  vo.getAttributeCount(); i++)
            {
               String attrName = vo.getAttributeDef(i).getName();
               String attrVal  = r.getAttribute(i).toString();
               System.out.println(attrName + " = " + attrVal);
            }
            r = vo.next();             
         }
      }
      catch (NamingException ex)
      {
         System.out.println("NamingException " + ex.getMessage());
         ex.printStackTrace();
      }
      catch (ApplicationModuleCreateException ex)
      {
         System.out.println("Unable to create application module: " + ex.getMessage());
         ex.printStackTrace();
      }
      catch (JboException ex)
      {
         System.out.println("JboException: " + ex.getMessage());
         ex.printStackTrace();
      }
      catch (Exception ex)
      {
         System.out.println("Exception: " + ex.getMessage());
         ex.printStackTrace();

      }
      finally
      {
         if (am != null)
         {
            am.getTransaction().disconnect();
            am.remove();
         }
      }
   }

}    

Note: When redeploying an EJB session facade bean (BMT) that is not client-enabled, the client code must call appmoudle.remove() or appmodule.getTransaction().disconnect() to close the JDBC connection. Otherwise an exception occurs.


Related topics

Working with the OC4J Application Servers

 

Copyright © 1997, 2004, Oracle. All rights reserved.