Example 3: EJB deployed to BEA WebLogic Server 6.1

This example shows how to use an EJB that is deployed to BEA WebLogic Server 6.1 as a data source and import it to a JReport catalog via OOJDBC.

Follow the steps below to achieve your goal:

  1. Installation
  2. Modifying and rebuilding the EJB
  3. Setting up the demo data source
  4. Importing the EJB using the Model Wizard
  5. Importing the ODF file into a catalog
Installation

You are required to install the following before you can start developing reports using EJB 2.0:

Modifying and rebuilding the EJB

The demo EJB Account.java, AccountBean.java and AccountHome.java will be used in this example. They are located in D:\bea\wlserver6.1_beta\samples\ejb20\basic\beanManaged.

Take the following steps to modify an rebuild the EJB:

  1. Modify the source code of the EJB.
    1. Modify AccountBean.java by adding the following two functions:
      
      /*This function is used to display the primary key in your report
                  ( a limitation to the java.lang.Object return type). */ 
      public String getPrimaryKeyString() 
      { 
          // return (String) ctx.getPrimaryKey();
          return accountId; 
      }
      /** 
      * Finds all EJBeans.
      * @return Enumeration EJBean Primary Keys
      * @exception javax.ejb.EJBException 
      * if there is a communications or systems failure
      */ 
      public Collection ejbFindAllAccounts() 
      { 
          log("getAllAccounts ()"); 
          HashSet col = new HashSet(); 
          Connection con = null; 
          PreparedStatement ps = null;
          try 
      	{
              con = getConnection(); 
              ps = con.prepareStatement("select id from ejbAccounts"); 
              ps.executeQuery(); 
              ResultSet rs = ps.getResultSet();
              String pk; 
              while (rs.next()) 
      		{
                  pk = rs.getString(1);
                  col.add(pk); 
              } 
              return (Collection)col; 
          } catch (SQLException sqe) 
      	{ 
              log("SQLException: " + sqe); 
              throw new EJBException (sqe); 
          } finally 
      	{
              cleanup(con, ps); 
          } 
      }
      

      Note: Remember to add import java.util.HashSet; into the AccountBean.java file.

    2. Modify AccountHome.java by adding the following function:
      /**
      * Finds all EJBeans. 
      * Returns an Collection of found EJBean primary keys.
      * 
      * @return Collection of Account 
      * @exception javax.ejb.FinderException
      if there is an error while accessing the persistent storage 
      * @exception java.rmi.RemoteException if there is a communications
      * or systems failure 
      * @see examples.ejb.basic.beanManaged.AccountBean
      */ 
      public Collection findAllAccounts()
          throws FinderException, RemoteException;
    3. Modify Account.java by adding the following function:

      /*used to display the primary key in reports*/
      public String getPrimaryKeyString() throws RemoteException;

    4. Modify Client.java which is used to create EJB objects. The comments included below are the statements used to remove the EJB objects:
      
      log("Removing beans..."); 
      for (int i=0; i<numBeans; i++) 
      { 
          accounts[i].remove();
      }
      
  2. Modify the file build.cmd in D:\bea\wlserver6.1_beta\samples\examples\ejb20\basic\beanManaged as follows:
    *** Begin of build.cmd *** 
    
    @REM Copyright (c) 2000-2001 BEA Systems, Inc. All Rights Reserved. 
    @REM You must set your environment by running the setExamplesEnv shell script
    @REM prior to executing this script.
    SET CLASSPATH=D:\jdk1.6.0_17\jre\lib\rt.jar;D:\downloads\jdk\EJB\2.0\EJB.JAR;
    D:\bea\wlserver6.1_beta\lib\weblogic.jar 
    SET WL_HOME=D:\bea\wlserver6.1_beta
    SET APPLICATIONS=D:\bea\wlserver6.1_beta\config\mydomain\applications
    SET EX_WEBAPP_CLASSES=D:\bea\wlserver6.1_beta\config\examples\
    applications\examplesWebApp 
    SET CLIENT_CLASSES=D:\bea\wlserver6.1_beta\config\examples\clientclasses
    @REM Create the build directory, and copy the deployment descriptors into it
    mkdir build build\META-INF 
    copy *.xml build\META-INF
    @REM Compile EJB classes into the build directory (jar preparation)
    D:\jdk1.6.0_17\bin\javac -classpath %CLASSPATH%
    -d build Account.java AccountHome.java ProcessingErrorException.java 
    AccountBean.java
    @REM Make a EJB jar file, including XML deployment descriptors
    cd build 
    D:\bea\jdk130\bin\jar cv0f std_ejb20_basic_beanManaged.jar META-INF examples 
    cd ..
    @REM Run EJBC on jar file
    D:\jdk1.6.0_17\bin\java -classpath %WL_HOME%/lib/weblogic_sp.jar;
    %WL_HOME%/lib/weblogic.jar;%CLASSPATH% 
    eblogic.ejbc -compiler D:\jdk1.6.0_17\bin\javac build\std_ejb20_basic_beanManaged.jar
    %APPLICATIONS%\ejb20_basic_beanManaged.jar
    @REM Compile servlets and EJB interfaces into
    WEB-INF/classes directory of the Examples Web App
    @REM This ensures that the EJB can be accessed by JSPs
    and servlets of the Examples Web App
    D:\jdk1.6.0_17\bin\javac -classpath %CLASSPATH% 
    -d %EX_WEBAPP_CLASSES% Servlet.java Account.java AccountHome.java
    ProcessingErrorException.java 
    @REM Compile EJB interfaces & client app into the clientclasses directory 
    D:\jdk1.6.0_17\bin\javac -classpath %CLASSPATH% -d %CLIENT_CLASSES%
    Account.java AccountHome.java ProcessingErrorException.java Client.java
    
    *** End of file build.cmd ***
  3. Execute build.cmd to build up ejb20_basic_beanManaged.jar.
Setting up the demo data source

Set up the demo data source following the steps below:

  1. Copy cloudscap.jar in D:\bea\wlserver6.1_beta\samples\eval\cloudscape\lib\ to D:\bea\wlserver6.1_beta\lib
  2. Modify startWeblogic.cmd in D:\bea\wlserver6.1_beta\config\mydomain to include cloudscape.jar in the class path and set the home of cloudscape as D:\bea\wlserver6.1_beta\samples\eval\cloudscape\data (location of the database):

    set CLASSPATH=.;...D:\bea\wlserver6.1_beta\lib\cloudscape.jar;

    echo on

    %JAVA_HOME%\bin\java" -hotspot -ms64m -mx64m -classpath
    %CLASSPATH% -Dweblogic.Domain=mydomain
    -Dweblogic.Name=myserver "-Dbea.home=d:\bea"
    -Dcloudscape.system.home=D:\bea\wlserver6.1_beta\samples\eval\cloudscape\data
    "-Djava.security.policy==D:\bea\wlserver6.1_beta\lib\weblogic.policy"
    -Dweblogic.management.password=%WLS_PW% -Dreporthome=D:\jns520 weblogic.Server
  3. Start the WebLogic Server from the Start menu.
  4. Use the web browser to connect to the console through http://localhost:7001/console.
  5. Expand Service > JDBC and select the Connection Pool node. From the right panel, click on the link Create a new JDBC Connection Pool.
  6. Define the Connection Pool as follows:

    Name: demoPool
    URL: jdbc:cloudscape:demo
    Driver Classname: COM.cloudscape.core.JDBCDriver
    Properties: autocomit=true

  7. Click on the Create button to create the connection pool.
  8. Click the Target tab, and add myserver to the Chosen list. Then click on the Apply button.
  9. In the Monitoring tab, click the link Monitor all Active Pools to check if the pool is active.
  10. Click Data Source from the left pane in JDBC node.
  11. Click Configure a new JDBC Data Source to create a new data source.
  12. In the Configuration tab, input the following information:

    Name: demoPool
    JNDI Name: examples-dataSource-demoPool
    Pool Name: demoPool

  13. Click the Create button to create this data source.
  14. Click the Targets tab to add myserver to the right text field, and then click the Apply button to apply these changes. Now your demo data source for the beans is set up.
  15. Run the class Client.class to create EJB objects. The class has been compiled when you run build.cmd, and will be outputted to D:\bea\wlserver6.1_beta\config\examples\clientclasses directory. The command should be as follows:
    D:\jdk1.6.0_17\bin\java -classpath D:\jdk1.6.0_17\jre\lib\rt.jar;
    D:\downloads\jdk\EJB\2.0\EJB.JAR;D:\bea\wlserver6.1_beta\lib\weblogic.jar;
    D:\bea\wlserver6.1_beta\config\examples\clientclasses
    examples.ejb20.basic.beanManaged.Client t3://localhost:7001
Importing the EJB using the Model Wizard

The JNDI name of the entity bean examples.ejb20.basic.beanManaged.AccountBean is ejb20-beanManaged-AccountHome. The remote interface is examples.ejb20.basic.beanManaged.Account.class. The home interface is examples.ejb20.basic.beanManaged.AccountHome.class.

To import the EJB using the Model Wizard, take the following steps:

  1. Modify ModelWizard.bat in D:\jreport\designer\bin as follows:
    "D:\jdk1.6.0_17\jre\bin\java.exe" "-Dinstall.root=D:\jreport\designer" 
    -classpath D:\jreport\designer\lib\report.jar;D:\jreport\designer\lib\resource.jar;
    D:\jreport\designer\lib\JREngine.jar;D:\jreport\designer\lib\mail.jar;
    D:\jreport\designer\lib\activation.jar;D:\jreport\designer\lib\unicode.zip;
    C:\TEMP";"D:\jreport\designer";D:\bea\wlserver6.1_beta\lib\weblogic.jar;
    D:\bea\wlserver6.1_beta\config\mydomain\applications\ejb20_basic_beanManaged.jar;
    D:\jaxp1.1\jaxp.jar-Dreporthome="D:\jreport\designer"
    -Djava.naming.factory.initial="weblogic.jndi.WLInitialContextFactory"
    -Djava.naming.provider.url="t3://localhost:7001" 
    com.jinfonet.jdbc.model.wizard.ModelWizard %1 %2 %3 %4 %5 %6 %7 %8 %9
  2. Start the Model Wizard using the modified batch file. The Object Source Wizard will then appear.
  3. Click the Add Table button and define a table named Account, and then click the Parse button. The Add Table dialog will then appear. In this method, only the balance value is stored while creating EJB objects in Client.java, so you can remove the columns deposit, withdraw, and isIdentical, leaving only the columns balance and PrimaryKeyString.
  4. Click Direct Access Path in the Add Table dialog to define the accessing path of the table in the Access Method dialog.
    1. In the Root Class Name field, enter com.jinfonet.jdbc.ejb.EJBInitializer, and then click the Parse button. In DefaultValue, enter JNDI name ejb20-beanManaged-AccountHome, check CastTo and select examples.ejb20.basic.beanManaged.AccountHome.

      Note: After you fill in DefaultValue, you should click Enter on your keyboard, or the error will occur. Also, before you set CastTo, you should start the BEA WebLogic Server. You should also choose first. These actions will connect you to the server.

    2. Check the option IsDynamic if you want to use dynamic parameters. The syntax of the method is: An array of Boolean values to indicate which parameter is dynamic at runtime. If the parameter is dynamic, the value should be prompted and initialized before executing SQL.
    3. Click Next. Then select the function findAllAccounts as the method name.
    4. Click OK and the table Account will now have been added.
  5. In the Object Source Wizard, click File > Save. You will then be prompted to save the ODF file. You should save it to <designer_install_root>\lib or the directory where the catalog will be. Here we save it as weblogic.odf.

Notes:

Importing the ODF file into a catalog

Here, you will be shown how to load the table Account into a catalog file, and create reports with it. Before starting JReport Designer, make sure that the class path includes the relative Jar files and path, and has set the relative system property values.

In this example, the batch file of JReport Designer is as follows:

"D:\jdk1.6.0_17\jre\bin\java.exe" "-Dinstall.root=D:\jreport\designer"
-classpath "g:\temp\jdbc\d.zip;D:\jreport\designer\lib\report.jar;
D:\jreport\designer\lib\resource.jar;D:\jreport\designer\lib\JREngine.jar;
D:\jreport\lib\mail.jar;D:\jreport\designer\lib\activation.jar;
D:\jreport\designer\lib\unicode.zip;C:\TEMP";"D:\jreport\designer";
D:\bea\wlserver6.1_beta\lib\weblogic.jar;
D:\bea\wlserver6.1_beta\config\mydomain\applications\ejb20_basic_beanManaged.jar;
D:\jaxp1.1\jaxp.jar;D:\jaxp1.1\xalan.jar;D:\jaxp1.1\crimson.jar;
D:\bea\wlserver6.1_beta\lib\ejb20.jar;D:\bea\wlserver6.1_beta\lib\wlepool.jar 
-Dreporthome="D:\jreport\designer"
-Djava.naming.factory.initial="weblogic.jndi.WLInitialContextFactory"
-Djava.naming.provider.url="t3://localhost:7001"
jet.report.ide.JReport %1 %2 %3 %4 %5 %6 %7 %8 %9

To import the ODF file into a JReport catalog:

  1. Start JReport Designer with the modified batch file.
  2. Click File > Catalog Management > New Catalog.
  3. In the Input New Catalog Name dialog, specify the catalog name, the data source name and the path. The catalog should be located in the directory where you saved the ODF file. For example, if you saved the ODF file in C:\odf, the new catalog should be created also in C:\odf.
  4. In the Catalog Browser, create a new connection with the Get JDBC Connection Information dialog as follows:
    1. Uncheck Use ODBC Data Source.
    2. Check the Driver checkbox, then enter com.jinfonet.jdbc.obj.ObjectDriver in the text field that follows.
    3. Enter jdbc:jinfonet:object:@File name in the URL text field. @File name should be the .odf name you saved. In the example, it is jdbc:jinfonet:object:@weblogic61.
    4. Click OK. The Add Table dialog appears. Select a table to be added, then click Done.

Now, the table will have been added into the catalog. You can use it to develop reports as required.