Using session beans with more than one business interface

A session bean can use more than one business interface. In this recipe we will use multiple interfaces to illustrate how a subset of methods can be exposed to a client while providing a larger set of methods to the beans of the application. An account EJB is used exposing the discount rates based on the type of client.

Getting ready

The process of adding more than one business interface involves two steps:

  1. Defining the interfaces
  2. Implementing the interfaces in the EJB.

    Each of these steps is not difficult. However, the implementation of the business methods can be a different matter.

How to do it...

Start by creating a Java EE application called MultipleInterfacesExample. In the EJB module add a packt package with two interfaces and one class:

  • AccountBeanRemote interface
  • AccountBeanLocal interface
  • AccountBean stateless session bean

In the WAR module add an index.jsp file if your IDE does not automatically add one for you. When the file is created, select a JavaServer Faces (JSF) page or later add a JSF framework to the module. Failure to do this may result in a run-time exception.

Create an AccountBeanLocal interface that exposes getters and setters for both a corporate and a non-profit discount rate. Use the @Local annotation to designate this as a local interface.

@Local
public interface AccountBeanLocal {
public float getCorporateDiscount();
public void setCorporateDiscount(float corporateDiscount);
public float getNonProfitDiscount();
public void setNonProfitDiscount(float nonProfitDiscount);
}

Next, create an AccountBeanRemote interface that exposes only get methods for the discounts. This can be used to restrict access to the implementing class. Use the @Remote annotation to designate this as a remote interface.

@Remote
public interface AccountBeanRemote {
public float getCorporateDiscount();
public float getNonProfitDiscount();
}

The next step is to implement these interfaces. Add the AccountBean such that it implements both of these interfaces. The implementation below is simple and uses different percentages for the two types of discounts.

@Stateless
@Named("account")
public class AccountBean implements AccountBeanRemote, AccountBeanLocal {
private float corporateDiscount;
private float nonProfitDiscount;
@PostConstruct
public void initialize() {
corporateDiscount = 0.15f;
nonProfitDiscount = 0.25f;
}
public float getCorporateDiscount() {
return corporateDiscount;
}
public void setCorporateDiscount(float corporateDiscount) {
this.corporateDiscount = corporateDiscount;
}
public float getNonProfitDiscount() {
return nonProfitDiscount;
}
public void setNonProfitDiscount(float nonProfitDiscount) {
this.nonProfitDiscount = nonProfitDiscount;
}
}

To demonstrate the use of these interfaces, create an index.jsp file that displays the corporate and non-profit discount rates. Notice this implementation uses JSF. When creating this file, or once it has been created, make sure it is associated with a JSF framework. From NetBeans this can be accomplished by modifying the properties of the WAR module and adding a JSF framework.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Account</title>
</head>
<body>
<h1>Discount Rates</h1>
<h:form>
<h:outputText value="Corporate Discount: #{account.corporateDiscount}"/><br/>
<h:outputText value="Non-Profit Discount: #{account.nonProfitDiscount}"/><br/>
</h:form>
</body>
</html>
</f:view>

The following screenshot shows the two discount rates available:

How to do it...

How it works...

Notice the use of the @Named annotation with the AccountBean. It was used to associate the name "account" with this EJB when used within a JSF page. In the index.jsp page, this name was followed by a period and either corporateDiscount or nonProfitDiscount. This had the effect of invoking the corresponding getCorporateDiscount and getNonProfitDiscount methods of the AccountBean. There was no need to use the get prefix as this is a feature of JSF.

In this application, a client that is not part of the application is only able to access the getter methods. This effectively limits its access to the full capabilities of the EJB. As we will see in Chapter 7, EJB Security, there are other techniques based on the credentials of the actual user, which can be used to limit access to an EJB's methods. These techniques can be used to provide a finer grained way of limiting access to an EJB's methods.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.42.116