Pramati Technologies

J2EE Coding Aids

Overview

New features such as online validation and J2EE refactoring have made J2EE coding easier in Pramati Studio. J2EE code is validated during code generation. Studio checks for Java, EJB, and J2EE errors, simultaneously. Violation of EJB or J2EE specification or the bean provider contract is treated as error. If a bean has something wrong within it, Studio suggests a correction if possible.

EJBs are usual Java classes and J2EE coding aids such as rename refactoring, and code completions are available in Studio. EJB dependencies are considered as Java class inheritances. EJB class, component interface, home interface dependencies are treated as EJB class implementing corresponding interface. For example, if a new method is introduced in an EJB class, and the caret is on the method declaration, Studio underlines it using a red squiggly. Press Alt + Enter to view the suggested correction. In this case, Studio suggests to insert the current method in a bean interface. Press Enter to implement the suggestion.

Intelligent Features of J2EE Assistant

Some of the intelligent features that the J2EE Assistant provides are:

List of errors validated in Studio

Studio considers violation of the following rules to be errors:

Using online validation of J2EE code in Studio

Online validation of J2EE code in Studio is based on J2EE, EJB1.1, and EJB 2.0 specifications. Bean provider contract given in EJB specification should be adhered to when a bean code is being written. Any deviation from a specification, is highlighted in the editor by red squiggly for errors and gray squiggly for warnings. Placing the mouse at the squiggly displays a description of the error.

If Studio can take any action to correct the error, it prompts to press Alt + Enter. Select the appropriate action from the list of suggestions and press Enter to perform the action. For example, according to the EJB specifications, the method create() in Home or LocalHome should throw CreateException. If CreateException is not thrown, the method is highlighted with a squiggly.

Pressing Alt + Enter displays the tip, "Throw CreateException".

Press Enter to include javax.ejb.CreateException in the throws clause.

J2EE refactoring in Studio

Studio provides J2EE aware refactoring in addition to Java refactoring. All references to the renamed element is updated across the AST, in the ejb-jar.xml, web.xml, and application.xml. For example, if ejbCreateCustomer in a bean class is renamed to ejbCreateNewCustomer, in addition to Java refactoring the corresponding create method is renamed to createNewCustomer in Home and LocalHome. All references in the desk and the deployment descriptor are updated.

The following usages in Studio are J2EE aware:

For example, if method getName() for a CMP 2.0 is renamed to getName1(), the following changes are made by Studio:

For more details on refactoring, read `Refactoring Java Code in Studio'.

For this example, rename lineitem CMR in OrderBook.dsk. Open bean properties for SalesOrderBean to view the existing type of relationship between SalesOrderBean and LineitemBean. Open LineitemBean in Editor and follow these steps:

    1. Place the cursor at lineitem CMR and select Refactor > Rename from the menu bar or right click on the concerned Java file and select Refactor > Rename.
  1. Type the new name for the member to be refactored.

  2. Select the Preview usages before refactoring option to view all usages of the selected member in the output panel. A dialog notifies you about the action to be performed.

    Click Close.
  3. The Preview tab in output panel displays the usages to be effected due to this refactoring.

  4. To continue, click Refactor button in the Preview toolbar. Express development takes care of the necessary modifications, recompilation etc. All usages of lineitem is renamed as newlineitem.

Note: Select the Search in comments and literals option in the Rename dialog to correct the usages of the refactored member in all comments and strings.
Using J2EE Assistant

The J2EE Assistant in Studio helps the developer to add references to the Bean classes, Bean Browser, JSP Pages and Servlets, writing design patterns. The three different types of references that can be added are EJB references, resource references, resource environment references.

Using the J2EE Assistant to add these references, generates the lookup code at the cursor position, based on the inputs provided by the developer, adds these entries to the required XML files, imports the relevant classes (in the bean class only), and also initializes the required variables. Using the J2EE Assistant minimizes the number of errors that are committed, by the developer, and also removes any oversights of the developer.

Adding EJB References

EJB References are used for the declaration of a reference to an enterprise bean home. The declaration consists of:

To add the EJB Reference, position the cursor at the place where the reference is to be added, and then click on J2EE Assistant > Add EJB Reference from the main menu. The shortcut key that may be used is Ctrl + Alt + Q. This brings up the Add EJB Reference panel, where the following information should be entered:

Clicking on OK after filling in all the required details adds the lookup code at the cursor position, adds the required import statement for the home/local class being looked up, declares the initial context variable, ic to the code, and therefore the classpath of the bean class file must contain the classes of the beans looked up.

Adding Resource References

Resource reference contains a declaration of enterprise bean or web component reference to an external resource. It consists of an optional description, the resource manager connection factory reference name, the indication of the resource manager connection factory type expected by the code, the type of authentication (Application or Container), and an optional specification of the shareability of connections obtained from the resource (Shareable or Unshareable).

To add the Resource Reference, select Ejb-jar.xml > References > Resource Tab.

Using the J2EE Assistant, position the cursor at the place where the reference is to be added, and then click on J2EE Assistant > Add Resource Reference from the main menu. The shortcut key that may be used is Ctrl + Alt + W. This brings up the Add Resource Reference panel, where the following information needs to be filled:

Clicking on OK adds the lookup code at the place where the cursor was positioned, adds the required import statements for the javax.sql.DataSource, declares the initial context variable - ic.

Adding Resource Environment References

The resource environment reference contains a declaration of an enterprise bean reference to an administered object associated with a resource in the enterprise bean or the web component environment.

To add the Resource Environment Reference, position the cursor at the place where the reference is to be added, and then click on J2EE Assistant > Add Resource Environment Reference from the main menu. The shortcut key that may be used is Ctrl + Alt + E. This brings up the Add Resource Environment Reference panel, where the following information needs to be filled:

Clicking on OK adds the lookup code at the place where the cursor was positioned, adds the required import statements, declares the initial context variable - ic, and therefore the classpath of the bean class file must contain the classes of the beans being looked up.

Bean Browser

Use this feature to browse for different types of enterprise beans in the desk. Click on J2EE Assistant > Bean Browser from the main menu or press on Ctrl + Alt + B on the keyboard. This pops up the Bean Browser dialog, which by default, displays all the beans in the desk.

To view beans after sorting them using the different criterion, use the following:

Selecting the option, all Modules from the combo box displays all the beans in the desk. Selecting a specific module name from the combo box displays only those beans that exist in that module

The OK button is disabled till a bean is selected. Select the bean from the list, and click on OK to open the bean class file in the Display Panel.

Session Facade

Use this feature to select beans and the type of interface (remote or local) that are to be included in facade. Click on J2EE Assistant > Session Facade from the main menu, or press Ctrl + Alt + S on the keyboard. This pops up the Create Session Facade dialog. The dialog is divided in two parts:

Check the options - Home and Local Home against the bean name to include them in the facade. If the interface type is not available for a bean, then that specific checkbox displays the text NA (not available). The OK button is disabled till a bean is selected. Check against the bean name, the interface type for that bean, and the session bean to create the facade and then click OK. This generates the lookup code for all the beans selected in the setSessionContext method of the selected session facade bean. Selecting beans that have already been declared in the Create Session Facade dialog, does not generate the look up code for these beans. The list of variables is initialized for the remote/local interfaces of the beans being looked up, and their classes are included in the classpath of the module of the facade bean. If the beans being looked up belong to a package other than the session facade bean, the beans are imported in the facade beans.

Selecting a bean as the Facade Bean field does allow the same bean to be included in the Facade. Similarly, selecting a CMP 1.1 bean as a Facade bean does not allow the local interface of any bean to be included in the Facade.

Value Object

Value Object is an object that contains the fields that represent the EJB accessor methods. Use this J2EE Assistant feature to create a value object for a specific CMP. This feature supports both CMP 1.1 and CMP 2.0 type of bean. Click on J2EE Assistant > Create Value Object on the main menu, or right click on the bean properties file in the Explore Panel and select Create Value Object. This pops up the Value Object Dialog. The menu item is enabled only when CMP bean properties in the Explore Panel.

Creating a Value Object for a CMP Bean includes creation of the following:

  1. Value Object Class, which represents the CMP Fields chosen by the user.
  2. Getters and Setters for the CMP Fields in the Value Object
  3. A default constructor in the Value Object, which takes all the fields
  4. Getter and Setter for Value Objects in the Remote/Local Interface as specified by the user
  5. Creating a Value Object also updates the bean code to have Getter and Setter methods.

The Create Value Object dialog that pops up has the following fields that the user fills as per the requirement:

To create the getter and setter methods for the value object in the bean interfaces, check the options provided:

Selecting the getter/setter options creates the getter/setter for the Value Object in the corresponding interface. The bean code is also updated automatically.

Click OK after filling in all the details in the dialog. This button is disabled when none of the checkbox are selected. To cancel the command, click on Cancel.


Pramati Technologies  © Copyright   TOCPREVNEXTINDEX