April 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Consuming and Binding EJBs and Data in Creator

  • April 4, 2005
  • By Dick Wall
  • Send Email »
  • More Articles »

This article is the third and final in a series detailing the integration of Java Studio Creator into an existing software development process. In the example that has formed the basis for these articles, the architecture uses an EJB API that can be deployed to the Sun Application Server embedded in Creator and then consumed within Java Creator Studio itself. The previous two articles, Integrating Sun Java Studio Creator Into Your Development Process and Deploying and Consuming EJBs in Java Studio Creator, covered first the creation of suitable EJB archives for use in Creator, and then the deployment and consumption within Creator.

This final article details how you can use the EJBs and the data returned from them to bind into on-page controls within Creator, thus seeing the fruits of your labor. There are a number of basic approaches covered for binding in the data, and some specific details and notes on that usage within Creator.

This article refers to example code that is available from the NewEnergy Associates Web page at the following locations:

The EJB example project was constructed using Oracle's JDeveloper 10.1.2, available free for educational use after registration from http://www.oracle.com/technology/products/jdev/index.html. Make sure you get the 10.1.2 version to compile this project, not the newer early access version of 10.1.3 because it is untested with that version.

Because the aim here is to illustrate the EJB binding into Creator, the example jar is kept purposely simple and has no persistence and no error checking (it will be easy to cause NPEs in this demo, but I prefer clarity of the code in this case—persistence and error trapping are left as an exercise for the reader). Instead of persistent data, the EJB jar file uses static data to return results.

Preparation

Download at least the EJB EAR file and Client Jar from the link given above and unzip the file. You should find one EAR and one JAR inside.

The deployment process to the app server and the consumption in Creator are detailed in the previous article, but a quick recap of the steps is:

  1. Start Creator, and from within Creator, start the Deployment Server.
  2. Run the deploy tool from the SunAppServer8/bin directory under your Creator install directory.
  3. Open the EAR file downloaded above in the deploy tool.
  4. Deploy (Tools->Deploy) the EAR into the Creator App Server (the localhost:14848) choice. Check the Return Client Jar if you want, but I have included the client jar file created by that in the zip file you downloaded.
  5. When the EAR has been deployed successfully, close the deploy tool and in Creator, import the client jar file. (Right-click Enterprise Java Beans and select Add Set of Session EJBs.)
  6. In the dialog, fill in the values and add the client jar file. Remember to set the port to 13700, and fill in something meaningful for the name. An example screenshot is provided below:


  7. Click here for a larger image.

  8. After you click OK, you should see a new CreatorDemo entry in the list of Enterprise Java Beans section. Expand this new entry and further expand each of the three EJBs in there; you should see several methods on each EJB, like this:
  9. If you see the above, the EJBs are ready for use in Creator, so you can use them.
  10. The final step: Create a new project from the Creator welcome screen. This will be your demo application, so name it what makes sense to you.

Simple Data Usage

First, examine the simplest level of data binding and EJB usage within Creator. The example page you will set up will allow the user to type in a number (between 1 and 3 because those are the company IDs I have provided; anything else will result in a error). After the number is accepted, the name and annual income for that company will be displayed on the screen as text labels.

Create a new page in Creator and call it something like SimpleBinding. Then, do the following:

  1. Drag an Output Text on to the page and change the text to read "Enter company ID."
  2. Drag a Text Field next to it, and rename the text field to companyId. (It's good practice to rename any controls you will manipulate from the java page bean because it makes it easier to figure out which control to use.)
  3. Right-click on the new text field and check the "Auto submit on change" option.
  4. Add two more Output Text controls, and rename them to companyNameText and companyIncomeText respectively. These will be the output fields.
  5. Your page should look something like this:
  6. You need to make a reference to the EJB you are going to use. Drag and drop the SimpleDemoEJB from the Enterprise Java Beans section in the server navigator onto the page somewhere. You should see a new reference, called SimpleDemoEJBClient1, appear at the bottom of the page.
  7. Now, double-click on the companyId Text Field. This should drop you through to the page bean in a new method called companyId_processValueChange. You are going to use this method to update the output fields on the page using the EJB reference you just created.
  8. In the body of the newly created method, add the following code:
  9. // first get the company ID typed in by the user
    String id = (String)this.getCompanyId().getValue();
    
    // use the EJB to look up the name and income for the company
    try {
       String name = this.simpleDemoEJBClient1.getCompanyNameForID(id);
       double income = this.simpleDemoEJBClient1.
                       getAnnualIncomeForCompanyId(id);
    
       this.getCompanyNameText().setValue(name);
       this.getCompanyIncomeText().setValue(new Double(income));
    }
    catch(RemoteException rex) {
       throw new FacesException(rex);
    }
    catch(NamingException nex) {
       throw new FacesException(nex);
    }
    catch(CreateException cex) {
       throw new FacesException(cex);
    }
    
  10. Some notes about the code above: In this case, I wanted to demonstrate the minimum exceptions that must be caught for an EJB remote method; these are RemoteException, NamingException and CreateException. This pattern can be put into a code clip (see the tips section), or you could choose to catch a more general exception and handle it instead of handling the three separately. Note also that you just use the simpleDemoEJBClient1 reference. All lookup and narrowing of the EJB has been handled for you by Creator. It makes life with EJBs very easy.
  11. When you paste in the code above, you may get several lines that do not compile because of classes that are not imported. Position the cursor in the underlined word and right-click; then choose the fast import option. This will give you the option of importing the class or the entire package. (I tend to import the package mostly because if I use one class from a package, I normally need several from the same package.) This will add the import statements automatically to the beginning of the file. Keep using this option until all of the underlined classes are gone.
  12. Next, compile the project, and then run it. You should get a screen that accepts a number; type in 1, 2 or 3 and you should see details come back about the company with the ID you typed in.

This is an example of the simplest way of using and binding data from an EJB API into Creator. In fact, depending on how you look at it, you are not really binding anything, merely using the results of the EJB methods to set values on the page. In essence, you are providing your own binding to the page, setting values, and potentially reading values back in and applied through other EJB calls (beyond the scope of this article, but I am sure you can work out the details).

You are also dealing with the easiest kind of data, scalars, and simple classes like String. What about if you want to return and bind more sophisticated data?

Tips
  • Fast Import: When coding, quite often you reference a class that you have not imported yet. When this happens, right-click on the underlined class and select the Fast Import option. You then can chose the class or package to import from a selection list.
  • Code Clips: Code that is often repeated is a prime candidate to make a snippet. To create a snippet, select the area of code in question and then drag it across to the code-clips pane in the palette. This will create a new code clip with a generic name. Right-click on it to change the name. Also, if there is non-general code in the code clip that you want to remove, right-click on the clip and edit it. For example, the try...catch block used for EJB calls is a prime candidate for a code clip. Make the clip, and then pull out the body of the try { } block because that will change for each usage. Doing this will save time on EJB calls by not having to write all of the catch clauses each time.
  • Start Page: When developing a page, it is convenient to jump straight to that page when running the app. To do this, right-click on the page jsp in the project navigator and select the "Set as Start Page" option.




Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel