##### Example 2: EJB deployed to BEA WebLogic Server 5.1

In this example, you will be shown how to use an EJB that is deployed to a BEA WebLogic Server 5.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. Deploying the EJB to WebLogic
3. Importing the EJB using the Model Wizard
4. Importing the ODF file into a catalog

##### Installation

You are required to install the following before you can begin this example:

- Install JReport Designer. In this example, we installed JReport Designer to `C:\JReport\Designer`.
- Install BEA WebLogic Server. To download BEA WebLogic server, go to the BEA WebLogic website: http://www.weblogic.com/licbeta.html, and install and configure your server. Here, we install the BEA WebLogic Server to `D:\Weblogic`.

  After installation, you will find weblogicaux.jar in `<weblogic_install_root>\lib`. Add weblogicaux.jar into the class path of the batch files of both JReport Designer and Model Wizard.

  In this example, we use the property value `Context.INITIAL_CONTEXT_FACTORY = weblogic.jndi.WLInitialContextFactory` to create the connection, and the class weblogic.jndi.WLInitialContextFactory is located in `D:\Weblogic\classes`. So you should also add `D:\Weblogic\classes` into the class path of the batch files that start both JReport Designer and Model Wizard.

- Install Jaxp1.1. You can download it from http://java.sun.com/xml/download.html. After installation, there will be three jars xalan.jar, crimson.jar and jaxp.jar in the installroot of Jaxp1.1. Add these three jars into the class path of the batch files that start both JReport Designer and Model Wizard.
- Add some system property values into the batch files. In this example, we set `-Djava.naming.factory.initial="weblogic.jndi.WLInitialContextFactory" -Djava.naming.provider.url="t3://localhost:7001"` to connect to BEA WebLogic Server.

After performing these steps, the batch file that starts the Model Wizard should look something like this:

    C:\jdk1.6.0_17\jre\bin\java.exe" "-Dinstall.root=C:\jreport\designer"
    -classpath "C:\jreport\designer\lib\report.jar;C:\jreport\designer\lib\resource_en_US.jar;
    C:\jreport\designer\lib\JREngine.jar;C:\jreport\designer\lib\mail.jar;
    C:\jreport\designer\lib\activation.jar;C:\jreport\designer\lib\unicode.zip;
    C:\TEMP";"C:\jreport\designer";D:\Weblogic510\lib\weblogicaux.jar;D:\Weblogic510\classes;
    w:\sf\lib\xalan.jar;w:\sf\lib\crimson.jar;w:\sf\lib\jaxp.jar;
    D:\weblogic510\myserver\ejb_basic_beanManaged.jar
    -Dreporthome="C:\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

##### Deploying the EJB to WebLogic

To deploy the EJB to a BEA WebLogic Server, take the following steps:

1. Make the EJB include methods to return Collection or JCollection.

   After you have installed BEA WebLogic server, you will find several EJBs. In this example, the entity bean examples.ejb.basic.beanManaged.AccountBean will be used. The JNDI name is beanManaged.AccountHome. The remote interface is examples.ejb.basic.beanManaged.Account.class. The home interface is examples.ejb.basic.beanManaged.AccountHome.class. Before deploying it, you will have to provide a method which returns java.util.Collection or com.jinfonet.jdbc.obj.JCollection. JCollection is an interface coded by Jinfonet.

   In the Model Wizard, you will need to inform the OOJDBC driver of the method with which to use to fetch data. The OOJDBC driver can only accept a method which returns java.util.Collection or com.jinfonet.jdbc.obj.JCollection.

   In this example, we change AccountBean.java to include the following method:

       /**
       * 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);
       }
       }

   And we add a method in AccountHome.java:

       /**
       * Finds all EJBeans.
       * Returns a 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;

2. Create the ejb-jar file.

   After calling javac.exe of JDK 6 to compile EJB classes, create a standard EJB jar file, including XML deployment descriptors. Then use the tool WebLogic EJB Deployer to create a deployable Jar file.

3. Deploy the EJB.

   In Step 2, the Jar ejb_basic_beanManaged.jar has been created. Now put the Jar into the class path of the WebLogic Server, edit the weblogic.properties file to include the new EJB. After stopping and restarting the WebLogic Server, you can access the EJB in a client. To do this, first create 20 accounts, and then add the Jar ejb_basic_beanManaged.jar into the class path both in the batch file of JReport Designer and that of Model Wizard.

##### Importing the EJB using the Model Wizard

Make an EJB initializing class that is coded by Jinfonet and is for looking up the EJBHome by the JNDI name.

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

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

1. Run the modified batch file ModelWizard.bat in `<designer_install_root>`. The Object Source Wizard will then appear.

2. Click Add Table and define a table named Account, and then click the Parse button. The Add Table dialog will then appear.

3. 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 text box, fill in com.jinfonet.jdbc.ejb.EJBInitializer, and then click the Parse button. For DefaultValue, enter JNDI name beanManaged.AccountHome, and select CastTo as examples.ejb.basic.beanManaged.AccountHome.

      Note: After you fill in DefaultValue, you should press Enter in your keyboard, or an error will occur. Also, before you set CastTo, you should start the BEA WebLogic server. Choose first, followed with the down arrow. These actions will connect to the server.

   2. Check the option Is Dynamic 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 the SQL is executed.
   3. Click Next, and then select the method findAllAccounts as the method name.
   4. Click OK, and the table Account will now have been added.
4. In the Object Source Wizard, select 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:

- If you use dynamic parameters, that is, you check the option Is Dynamic , the parameter will be passed to JReport Designer. After you create a connection in the Catalog Browser, you will find the parameter in the Parameters node.
- The same ODF file cannot exist in both `<designer_install_root>\lib` and the directory where the catalog is located.

##### 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 relative system property values. In this example, the batch file for starting JReport Designer is as follows:

    C:\jdk1.6.0_17\jre\bin\java.exe;-Dinstall.root=C:\JReport
    -classpath :\JReport\Designer\lib;C:\jreport\designer\lib\report.jar;
    C:\JReport\Designer\lib\resource_en_US.jar;C:\JReport\Designer\lib\JREngine.jar;
    C:\JReport\Designer\lib\mail.jar;C:\JReport\Designer\lib\activation.jar;
    C:\JReport\Designer\lib\unicode.zip;C:\TEMP";"C:\JReport\Designer";
    D:\Weblogic\lib\weblogicaux.jar;D:\Weblogic\classes;
    W:\sf\lib\xalan.jar;W:\sf\lib\crimson.jar;W:\sf\lib\jaxp.jar;
    D:\weblogic\myserver\ejb_basic_beanManaged.jar
    -Dreporthome=C:\JReport -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, take the following steps:

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. Enter com.jinfonet.jdbc.obj.ObjectDriver in the Driver text field.
   2. 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:@weblogic.

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