Deploying BC4J as an EJB Session to JBoss

The instructions in the following sections describe deploying a BC4J application as an EJB Session Bean. The information in this topic is also available from http://otn.oracle.com/products/jdev/howtos/appservers/deploy_bc4j_to_jboss.html.

Note:Before you start a BC4J project to be deployed as an EJB session to JBoss, you must select a connection type and the corresponding SQL dialect, type maps or domains. For instructions, see Understanding Connections, SQL Dialect, Type Maps, and Domains.

Creating an Oracle Data Source in JBoss

To create a data source for the Oracle JDBC driver, edit the jboss.jcml file which is located in the JBoss/conf/<configuration> directory. For example:
C:\<JBoss_install>\jboss\conf\catalina

There is one modification, and one addition to make to this file.

  1. Replace this entry:

    <mbean code="org.jboss.jdbc.JdbcProvider" name="DefaultDomain:service=JdbcProvider">
           <attribute name="Drivers">org.hsqldb.jdbcDriver</attribute>
           </mbean>

    With this entry:

    <mbean code="org.jboss.jdbc.JdbcProvider" name="DefaultDomain:service=JdbcProvider">
           <attribute name="Drivers">oracle.jdbc.driver.OracleDriver,org.hsqldb.jdbcDriver</attribute>
           </mbean>

  2. Add the following entry just below the </mbean> that was just modified. Change the properties of the URL, JDBCUser, and Password to match your database configuration.

    <mbean code="org.jboss.jdbc.XADataSourceLoader" 
               name="DefaultDomain:service=XADataSource,name=OracleDS">
               <attribute name="PoolName">OracleDS</attribute>
               <attribute name="DataSourceClass">
               org.jboss.pool.jdbc.xa.wrapper.XADataSourceImpl
               </attribute>
               <attribute name="URL">
               jdbc:oracle:thin:@rcleveng-sun:1521:RCLEVENG
               </attribute>
               <attribute name="JDBCUser">scott</attribute>
               <attribute name="Password">tiger</attribute>
               <attribute name="MinSize">0</attribute>
               <attribute name="MaxSize">10</attribute>
               </mbean>

The first modification allows the Oracle JDBC driver to be loaded and thus registered with the JDBC DriverManager class.

The second modification creates a JDBC data source bound to the JNDI name of java:/OracleDS.

Creating a BC4J configuration for JBoss

The JDeveloper deployment profile wizards create all the necessary code to deploy business components as a J2EE EJB JAR module to the target server. This section covers creating a deployment profile for the BC4J application module for an Oracle Application Server deployment which will be copied to JBoss.

  1. Create a deployment profile for BC4J EJBs.
    1. For the EJB type, choose Session Facade (BMT) from the Available to the Selected list.
  2. In the Navigator, select the application_module_icon BC4J application module icon.
  3. Right-click application_module_icon and choose Configurations.
  4. Select the appropriate <YourApplicationModule>9iAS configuration.
  5. Select Copy. A copy of the configuration is created and displayed below the Names list.
  6. Select this configuration and click Edit.
  7. In the Business Component Configuration Name field, rename the configuration to a name you will recognize as the JBoss configuration. For example: JBoss.
  8. In the Connection Type list, choose JDBC DataSource.
  9. Enter a DataSource Name. For example: java:/OracleDS

Creating a JSP to Access Your Application Module as a EJB.

To test the business components, create a new JSP application which will use them.

  1. Create a new empty project.
  2. Using the Business Components JSP Application wizard, create a default JSP application for the BC4J objects just created.
  3. In step 2 of 4, be sure to select the JBoss Configuration
  4. From the main menu, Choose File | Save All.
  5. In the Navigator, right-click the webxml_icon web.xml icon and choose Settings.
  6. Ensure that there is an EJB reference to your application module from your BC4J project.
  7. In the Navigator, select the deployment profile icon WAR_dep_profile_icon <myappwar>.deploy.
  8. Right-click this icon and choose Deploy to EAR file. You must deploy this application to an EAR file and not a WAR file, as JBoss will not add the EJB references under the java:comp/env/ JNDI namespace for a WAR file.
  9. Copy this EAR file to your <JBoss_install>\deploy directory. For example: C:\<JBoss_install>\jboss\deploy.

You now have a fully functional BC4J JSP application deployed as a web module. You may now invoke the application in your web browser by entering the application URL similar to the following:

http://localhost:8080/<context root>

where <context root> is the J2EE context root for your project containing the BC4J JSP application. For example, Workspace1-Project1-context-root/.

Creating an Application Test Client for Your Application Module

Apart from the JSP client, a standalone Java application can access your BC4J application module which is deployed as an EJB Session Bean.

We will create a new class to talk to the BC4J application module deployed as a session bean. This will be a standalone Java application which will lookup the session bean and then use an ApplicationModuleProxy class to obtain a reference to the application module.

  1. In the Navigator, select the project_icon <projectname>.jpr in which you want to create the class file.
  2. Choose File | New to open the New Gallery.
  3. In the Categories tree, expand General and select Simple Files.
  4. In the Items list, double-click Java Class.
  5. In the New Class dialog, click OK to accept the default settings.
  6. Add the following import statements to the newly created Java source file:
    import oracle.jbo.*;
    import javax.naming.*;
    import java.util.*;
    import mypackage2.common.ejb.beanmanaged.Mypackage2ModuleHome;
    import oracle.jbo.client.remote.ejb.ApplicationModuleProxy;
    
  7. Add the following main method to the class:
      public static void main(String[] args)
      {
        try
        {
          Hashtable env = new Hashtable();      
          env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");      
          env.put(Context.PROVIDER_URL, "localhost");      
          env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces" );      
          Context ctx = new InitialContext(env);      
    
          Mypackage2ModuleHome beanHome  = (Mypackage2ModuleHome )ctx.lookup("mypackage2.Mypackage2Module");
    
          // Create the proxy. If you have exported methods then you can cast it to your common am interface.
          ApplicationModule am = ApplicationModuleProxy.create(beanHome, env);
    
          //
          // Note: Change this connect string to the connect string you use to connect to your database
          //
          am.getTransaction().connectToDataSource( null, "java:/OracleDS", null, null, false );
          System.out.println( am.getClass() );
          String[] voNames = am.getViewObjectNames();
          ViewObject vo = am.findViewObject("DeptView");
    
          Row row;
          while ( ( row = vo.next() ) != null )
          {
            int numAttrs = row.getAttributeCount();
            for ( int i=0; i < numAttrs; i++ )
            {
              System.out.println( "Name= " + vo.getAttributeDef(i).getName() +
                                  " Value= " + row.getAttribute(i) );
            }
            System.out.println("");
          }
          am.getTransaction().disconnect();
        }
        catch ( Exception ex )
        {
          ex.printStackTrace();
        }
      }

Testing the BC4J EJB Application Client

  1. Edit your project settings, and remove the J2EE and Oracle9iAS libraries, and add the JBoss library to your project.
  2. Run the test client.
  3. Verify that the contents of the DEPT database table are displayed in the Log window of your application.

Note: You may need to choose View | Log Window from the main menu if the message log is not already visible.

References